#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  long long q;
  cin >> q;
  for (int e = 0; e < q; e++) {
    long long n;
    string s, t;
    cin >> n >> s >> t;
    vector<char> a, b;
    for (int i = 0; i < n; i++) {
      a.push_back(s[i]);
      b.push_back(t[i]);
    }
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    if (a != b) {
      cout << -1 << endl;
      continue;
    }
    long long ans = 0;
    for (int i = 0; i < n; i++) {
      for (int j = i; j < n; j++) {
        string r = t.substr(i, j - i + 1);
        long long ind = 0;
        long long cur = 0;
        while (cur < n && ind < r.size() && s[cur] == r[ind]) {
          cur++;
          ind++;
        }
        while (ind < r.size() && cur < n && s[cur] != r[ind]) {
          cur++;
          while (cur < n && ind < r.size() && s[cur] == r[ind]) {
            cur++;
            ind++;
          }
        }
        if (ind == r.size()) {
          ans = max(ans, (long long)j - i + 1);
        }
      }
    }
    cout << n - ans << endl;
  }
}
