#include <bits/stdc++.h>
using namespace std;
void solve();
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long t = 1;
  cin >> t;
  while (t--) solve();
}
void solve() {
  long long n;
  cin >> n;
  string a, b;
  cin >> a >> b;
  string c = a, d = b;
  sort(c.begin(), c.end());
  sort(d.begin(), d.end());
  if (c != d) {
    cout << -1 << '\n';
    return;
  }
  long long ans[n], mx = n;
  for (long long l = 0; l < n; l++)
    for (long long k = 0; k < n; k++) {
      ans[l] = n;
      long long i = l, j = k;
      while (j != n && b[j] != a[i]) j++;
      if (j != n) {
        ans[l]--;
        i++;
        j++;
      }
      while (i != n && j != n) {
        while (i != n && j != n && a[i] != b[j]) i++;
        if (i != n) ans[l]--, j++, i++;
      }
      mx = min(ans[l], mx);
    }
  cout << mx << '\n';
}
