#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    string s, t;
    cin >> s >> t;
    string a1(s), a2(t);
    sort((a1).begin(), (a1).end());
    sort((a2).begin(), (a2).end());
    bool v = 1;
    for (int i = 0; i < n; i++) v &= a1[i] == a2[i];
    if (!v) {
      cout << "-1\n";
      continue;
    }
    int out = 0;
    for (int i = 0; i < n; i++) {
      int c = 0, k = 0;
      for (int j = i; j < n; j++) {
        while (k < n && s[k] != t[j]) k++;
        if (k >= n) break;
        c++;
        k++;
      }
      out = max(out, c);
    }
    cout << n - out << "\n";
  }
}
