#include <bits/stdc++.h>
using namespace std;
const int maxn = 111;
inline int read() {
  int x = 0, f = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') f |= ch == '-', ch = getchar();
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  return f ? -x : x;
}
int q, n, nxt[maxn][26];
char s[maxn], t[maxn], tmps[maxn], tmpt[maxn];
int main() {
  q = read();
  while (q--) {
    n = read();
    scanf("%s%s", s + 1, t + 1);
    for (int i = (1); i <= (n); i++) tmps[i] = s[i], tmpt[i] = t[i];
    sort(tmps + 1, tmps + n + 1);
    sort(tmpt + 1, tmpt + n + 1);
    bool flag = true;
    for (int i = (1); i <= (n); i++)
      if (tmps[i] != tmpt[i]) {
        flag = false;
        break;
      }
    if (!flag) {
      puts("-1");
      continue;
    }
    for (int i = (0); i <= (25); i++) nxt[n][i] = n + 1;
    for (int i = (n - 1); i >= (0); i--)
      for (int j = (0); j <= (25); j++)
        nxt[i][j] = (s[i + 1] == j + 'a') ? i + 1 : nxt[i + 1][j];
    int ans = n;
    for (int i = (1); i <= (n); i++) {
      int cur = 0;
      for (int j = (i); j <= (n); j++) {
        cur = nxt[cur][t[j] - 'a'];
        if (cur > n) break;
        ans = min(ans, i - 1 + n - j);
      }
    }
    printf("%d\n", ans);
  }
}
