#include <bits/stdc++.h>
using namespace std;
long long cnts[26], cntt[26];
signed main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(false);
  long long q;
  cin >> q;
  while (q--) {
    for (long long i = 0; i < 26; i++) cnts[i] = cntt[i] = 0;
    long long n;
    cin >> n;
    string s, t;
    cin >> s >> t;
    for (long long i = 0; i < n; i++) cnts[s[i] - 'a']++, cntt[t[i] - 'a']++;
    bool ok = 1;
    for (long long i = 0; i < 26; i++) {
      if (cnts[i] != cntt[i]) ok = 0;
    }
    if (!ok)
      cout << "-1\n";
    else {
      long long ans = -1;
      for (long long l = 0; l < n; l++) {
        for (long long i = 0; i < l + 1; i++) {
          vector<long long> rem(26, 0), req(26, 0);
          long long j = n - 1 + i - l;
          assert(i + (n - 1 - j) == l);
          long long pts = 0, ptt = i;
          while (ptt <= j and pts < n) {
            if (s[pts] == t[ptt]) {
              pts++;
              ptt++;
            } else {
              rem[s[pts] - 'a']++;
              pts++;
            }
          }
          while (pts < n) rem[s[pts++] - 'a']++;
          for (long long k = 0; k < i; k++) req[t[k] - 'a']++;
          for (long long k = j + 1; k < n; k++) req[t[k] - 'a']++;
          bool ok = 1;
          for (long long k = 0; k < 26; k++)
            if (rem[k] != req[k]) {
              ok = 0;
              break;
            }
          if (ok) {
            ans = l;
            break;
          }
        }
        if (ans != -1) break;
      }
      cout << ans << '\n';
    }
  }
}
