#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const int MOD = 1000000007;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n, ans = 1000000000;
    string a, b, A, B;
    cin >> n >> a >> b;
    A = a, B = b;
    sort((A).begin(), (A).end()), sort((B).begin(), (B).end());
    if (A != B)
      cout << -1 << "\n";
    else {
      for (int i = 0; i < n; ++i) {
        int h = i;
        for (int j = 0; j < n; ++j)
          if (a[j] == b[h]) h++;
        ans = min(ans, n - (h - i));
      }
      cout << ans << "\n";
    }
  }
  return 0;
}
