#include <bits/stdc++.h>
using namespace std;
char s[105], t[105];
int nxt[105][26], cnts[26], cntt[26];
int T, N;
int Solve() {
  memset(cnts, 0, sizeof(cnts));
  memset(cntt, 0, sizeof(cntt));
  for (int i = 1; i <= N; ++i) ++cnts[s[i] - 'a'];
  for (int i = 1; i <= N; ++i) ++cntt[t[i] - 'a'];
  for (int i = 0; i < 26; ++i)
    if (cnts[i] != cntt[i]) return -1;
  memset(nxt, 0, sizeof(nxt));
  for (int i = N; i >= 1; --i) {
    for (int j = 0; j < 26; ++j) nxt[i - 1][j] = nxt[i][j];
    nxt[i - 1][s[i] - 'a'] = i;
  }
  int res = 0, temp, p;
  for (int j = 1; j <= N; ++j) {
    p = 0;
    temp = 0;
    for (int i = j; i <= N; ++i) {
      if (nxt[p][t[i] - 'a']) {
        ++temp;
        p = nxt[p][t[i] - 'a'];
      } else
        break;
    }
    res = max(res, temp);
  }
  if (!res) return -1;
  return N - res;
}
int main() {
  scanf("%d", &T);
  while (T--) {
    scanf("%d", &N);
    scanf("%s", s + 1);
    scanf("%s", t + 1);
    printf("%d\n", Solve());
  }
  return 0;
}
