#include <bits/stdc++.h>
using namespace std;
int main() {
  int q;
  cin >> q;
  for (int _ = 0; _ < q; ++_) {
    int n;
    cin >> n;
    int ret = n;
    string s, t;
    cin >> s >> t;
    string a(s), b(t);
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    if (a != b) {
      cout << -1 << endl;
      continue;
    }
    for (int i = 0; i < n; ++i) {
      int j = i, k = 0;
      while (j < n) {
        while (k < n && s[k] != t[j]) k++;
        if (k == n) break;
        k++;
        j++;
      }
      ret = min(ret, n - (j - i));
    }
    cout << ret << endl;
  }
  return 0;
}
