#include <bits/stdc++.h>
int check(char *aa, char *bb, int n) {
  static int kk[26];
  int i, c;
  memset(kk, 0, sizeof kk);
  for (i = 0; i < n; i++) kk[aa[i] - 'a']++;
  for (i = 0; i < n; i++) kk[bb[i] - 'a']--;
  for (c = 0; c < 26; c++)
    if (kk[c]) return 0;
  return 1;
}
int main() {
  int q;
  scanf("%d", &q);
  while (q--) {
    static char aa[100 + 1], bb[100 + 1];
    int n, h, i, j, max;
    scanf("%d%s%s", &n, aa, bb);
    if (!check(aa, bb, n)) {
      printf("-1\n");
      continue;
    }
    max = 0;
    for (i = 0; i < n; i++) {
      for (j = i, h = 0; j < n; j++, h++) {
        while (h < n && aa[h] != bb[j]) h++;
        if (h == n) break;
      }
      if (max < j - i) max = j - i;
    }
    printf("%d\n", n - max);
  }
  return 0;
}
