#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  string s, t;
  cin >> s >> t;
  string scpy = s;
  string tcpy = t;
  sort(scpy.begin(), scpy.end());
  sort(tcpy.begin(), tcpy.end());
  for (int q = 0; q < n; q++) {
    if (scpy[q] != tcpy[q]) {
      cout << "-1\n";
      return;
    }
  }
  int ans = 0;
  for (int q = 0; q < n; q++) {
    int tptr = q;
    int sptr = 0;
    int ct = 0;
    while (sptr < n && tptr < n) {
      if (s[sptr] == t[tptr]) {
        tptr++;
        ct++;
      }
      sptr++;
    }
    ans = max(ans, ct);
  }
  cout << n - ans << "\n";
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
