it=int(input()) for i in range(it): size=int(input()) a =input() b =input() t = 0 i = 0 j = 0 while a != b: t = b.find(a[i]) b = b[t:] + b[i:t-1] i += 1 print(j)