#include <bits/stdc++.h>
using namespace std;
const long long N = 1e6 + 7, mod = 1e9 + 7;
string s, t;
int test(int l) {
  int re = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == t[l]) {
      l++;
      re++;
    }
    if (l == s.size()) break;
  }
  return re;
}
void solve() {
  int n;
  cin >> n;
  cin >> s >> t;
  int ans = 1000;
  for (int l = 0; l < s.size(); l++) {
    ans = min(ans, n - test(l));
  }
  sort(s.begin(), s.end());
  sort(t.begin(), t.end());
  if (s != t) {
    cout << -1 << endl;
    return;
  }
  cout << ans << endl;
}
int32_t main() {
  int qwe;
  cin >> qwe;
  while (qwe--) {
    solve();
  }
}
