#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const double pi = 3.1415926535897932384626;
inline long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  return x * f;
}
const int maxn = 1e2 + 5;
int t, n;
char s1[maxn], s2[maxn];
int main() {
  t = read();
  while (t--) {
    n = read();
    scanf("%s%s", s1, s2);
    int minv = inf;
    for (int i = 0; i < n; ++i) {
      int k = i;
      for (int j = 0; j < n; ++j) {
        if (k < n && s1[j] == s2[k]) ++k;
        minv = min(minv, n - k + i);
      }
    }
    sort(s1, s1 + n);
    sort(s2, s2 + n);
    if (strcmp(s1, s2))
      printf("-1\n");
    else
      printf("%d\n", minv);
  }
  return 0;
}
