#include <bits/stdc++.h>
using namespace std;
int q, n;
string s, t;
int f(int dex) {
  for (char c : s)
    if (dex < n && c == t[dex]) dex++;
  return dex;
}
int solve() {
  cin >> n >> s >> t;
  string a = s, b = t;
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  if (a != b) return -1;
  int res = 0;
  for (int i = 0; i < n; i++) res = max(res, f(i) - i);
  return n - res;
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  cin >> q;
  while (q--) cout << solve() << '\n';
  return 0;
}
