#include <bits/stdc++.h>
using namespace std;
const long double PI = acos(-1);
const long double eps = 1e-9;
const int seed = 131;
const int M = 1e2 + 7;
char s[M], t[M];
int ns[M], nt[M];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    cin >> s >> t;
    int ans = 0;
    for (int i = 0; i < n; i++) {
      int tp = i, now = 0;
      for (int j = 0; j < n; j++)
        if (tp < n && s[j] == t[tp]) tp++, now++;
      ans = max(now, ans);
    }
    sort(s, s + n);
    sort(t, t + n);
    if (strcmp(s, t) != 0)
      cout << "-1" << endl;
    else
      cout << n - ans << endl;
  }
  return 0;
}
