#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  int T;
  cin >> T;
  while (T--) {
    int n;
    cin >> n;
    string s, t;
    cin >> s >> t;
    string ss = s;
    string tt = t;
    sort(ss.begin(), ss.end());
    sort(tt.begin(), tt.end());
    if (ss != tt) {
      cout << -1 << '\n';
      continue;
    }
    int ans = n;
    for (int i = 0; i <= n - 1; i++) {
      int pt = 0;
      for (int j = i; j <= n - 1; j++) {
        while (pt < n && s[pt] != t[j]) pt++;
        if (pt == n) break;
        ans = min(ans, n - (j - i + 1));
        pt++;
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
