#include <bits/stdc++.h>
using namespace std;
string a, b;
int N, T;
int chk[26][2];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> T;
  while (T--) {
    int ans = 1 << 30;
    cin >> N >> a >> b;
    memset(chk, 0, sizeof(chk));
    for (int i = 0; i < N; i++) {
      chk[a[i] - 'a'][0]++;
      chk[b[i] - 'a'][1]++;
    }
    bool flag = 0;
    for (int i = 0; i < 26; i++) {
      if (chk[i][0] != chk[i][1]) {
        cout << "-1\n";
        flag = 1;
        break;
      }
    }
    if (flag) continue;
    for (int i = 0; i < N; i++) {
      int tmp = 0;
      int s = -1;
      for (int j = i; j < N; j++) {
        bool f = 0;
        for (int k = s + 1; k < N; k++) {
          if (a[k] == b[j]) {
            s = k;
            f = 1;
            break;
          }
        }
        if (!f) break;
        tmp++;
      }
      ans = min(ans, N - tmp);
    }
    cout << ans << '\n';
  }
}
