#include <bits/stdc++.h>
using namespace std;
signed main() {
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    string s1, s2;
    cin >> s1 >> s2;
    long long a[26] = {0};
    char c = 'a';
    for (long long i = 0; i < n; i++) {
      a[s1[i] - c]++;
    }
    for (long long i = 0; i < n; i++) {
      a[s2[i] - c]--;
    }
    bool flag = 0;
    for (long long i = 0; i < 26; i++) {
      if (a[i] != 0) {
        cout << -1 << '\n';
        flag = 1;
        break;
      }
    }
    if (flag) {
      continue;
    }
    long long ans = 0;
    long long mx = -1;
    for (long long j = 0; j < n; j++) {
      long long cur = s2[j];
      long long ind = j;
      long long cnt = 0, done = 0;
      for (long long k = 0; k < n; k++) {
        if (s1[k] == s2[ind]) {
          ind++;
          cnt++;
          if (ind == n) {
            break;
          }
        }
      }
      mx = max(mx, cnt);
    }
    cout << n - mx << '\n';
  }
}
