#include <bits/stdc++.h>
using namespace std;
const int NMAX = 105;
char s[NMAX];
char t[NMAX];
int vfs[30];
int vft[30];
int main() {
  int T;
  scanf("%d", &T);
  while (T > 0) {
    T--;
    int n;
    scanf("%d\n", &n);
    scanf("%s", s + 1);
    scanf("%s", t + 1);
    for (int i = 1; i <= n; i++) {
      vfs[s[i] - 'a']++;
    }
    for (int i = 1; i <= n; i++) {
      vft[t[i] - 'a']++;
    }
    bool ok = 0;
    for (int i = 0; i <= 28; i++) {
      if (vfs[i] != vft[i]) {
        ok = 1;
      }
      vfs[i] = vft[i] = 0;
    }
    if (ok == 1) {
      printf("-1\n");
    } else {
      int sol = 0;
      for (int i = 1; i <= n; i++) {
        int p = i;
        int nr = 0;
        for (int j = 1; p <= n && j <= n; j++) {
          if (t[p] == s[j]) {
            p++;
            nr++;
          }
        }
        sol = max(sol, nr);
      }
      printf("%d\n", n - sol);
    }
  }
  return 0;
}
