#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cout.tie(0);
  cin.tie(0);
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    string s, t;
    cin >> s >> t;
    string cs = s;
    string ct = t;
    sort(cs.begin(), cs.end());
    sort(ct.begin(), ct.end());
    if (cs != ct) {
      cout << -1 << '\n';
      continue;
    }
    int l = 0;
    for (int i = 0; i < n; ++i) {
      int cl = 0;
      for (int j = 0; j < n; ++j) {
        if (t[i + cl] == s[j]) {
          cl++;
        }
      }
      l = max(l, cl);
    }
    cout << (n - l) << '\n';
  }
  cout << endl;
  return 0;
}
