#include <bits/stdc++.h>
using namespace std;
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    int n;
    scanf("%d", &n);
    string s, t;
    cin >> s >> t;
    int ans = s.size();
    for (int i = 0; i < n; ++i) {
      int k = i, sum = 0;
      for (int j = 0; j < n; ++j) {
        if (k < n && t[k] == s[j]) {
          k++;
          sum++;
        }
      }
      ans = min(ans, n - sum);
    }
    sort(s.begin(), s.end());
    sort(t.begin(), t.end());
    if (s != t) {
      cout << -1 << endl;
      continue;
    }
    cout << ans << endl;
  }
  return 0;
}
