#include <bits/stdc++.h>
using namespace std;
const int N = 110;
char s[N], t[N];
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    int n;
    scanf("%d", &n);
    scanf("%s %s", s + 1, t + 1);
    vector<int> cnt(26);
    for (int i = 1; i <= n; i++) cnt[s[i] - 'a']++, cnt[t[i] - 'a']--;
    bool ok = true;
    for (int i = 0; i < 26; i++)
      if (cnt[i] != 0) ok = false;
    if (!ok)
      printf("-1\n");
    else {
      int mi = 1e9;
      for (int i = 1; i <= n; i++) {
        for (int j = i, k = 1; j <= n && k <= n; j++) {
          while (k <= n && s[k] != t[j]) k++;
          if (k <= n && s[k] == t[j]) {
            mi = min(mi, i - 1 + n - j);
            k++;
          }
        }
      }
      printf("%d\n", mi);
    }
  }
  return 0;
}
