#include <bits/stdc++.h>
using namespace std;
int64_t ans, n, k, m, d, p, co[26];
vector<int> v[26];
char s[105], t[105];
int ch() {
  int a[26], b[26];
  for (int i = 0; i <= 25; i++) {
    a[i] = 0;
    b[i] = 0;
  }
  for (int i = 0; i <= n - 1; i++) {
    a[s[i] - 'a']++;
    b[t[i] - 'a']++;
  }
  for (int i = 0; i <= 25; i++)
    if (a[i] != b[i]) return 1;
  return 0;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int64_t tt;
  cin >> tt;
  while (tt--) {
    cin >> n;
    cin >> s;
    cin >> t;
    for (int i = 0; i <= n - 1; i++) v[s[i] - 'a'].push_back(i);
    if (ch()) {
      cout << "-1\n";
      for (int i = 0; i <= 25; i++) v[i].clear();
      continue;
    }
    ans = 0;
    for (int i = 0; i <= n - 1; i++) {
      for (int j = 0; j < 26; j++) co[j] = 0;
      p = -1;
      for (m = i; m < n; m++) {
        d = (t[m] - 'a');
        while (co[d] != v[d].size() && p > v[d][co[d]]) ++co[d];
        if (co[d] == v[d].size())
          break;
        else {
          p = v[d][co[d]];
          ++co[d];
        }
      }
      ans = max(ans, m - i);
    }
    cout << n - ans << endl;
    for (int i = 0; i <= 25; i++) v[i].clear();
  }
  return 0;
}
