#include <bits/stdc++.h>
using namespace std;
int n, q;
string s, t;
int main() {
  cin >> q;
  while (q--) {
    cin >> n >> s >> t;
    int ans = n + 1;
    for (int k, tt, i = 0; i < n; ++i) {
      k = i, tt = 0;
      for (int j = 0; j < s.size(); ++j) {
        if (k < n && t[k] == s[j]) ++k, ++tt;
      }
      ans = min(ans, n - tt);
    }
    sort(t.begin(), t.end());
    sort(s.begin(), s.end());
    if (s != t) ans = -1;
    cout << ans << '\n';
  }
}
