#include <bits/stdc++.h>
using namespace std;
long long n;
string s, t;
bool check(long long m) {
  for (long long start = 0; start <= m; start++) {
    long long i = start;
    for (long long j = 0; i < n && j < n; j++) {
      if (s[i] == t[j]) {
        i++;
      }
    }
    if (i == start + n - m) {
      return true;
    }
  }
  return false;
}
signed main() {
  cin.tie(0);
  cout.tie(0);
  ios_base ::sync_with_stdio(0);
  long long q;
  cin >> q;
  while (q--) {
    cin >> n;
    cin >> s >> t;
    string s_s = s, s_t = t;
    sort(s_s.begin(), s_s.end());
    sort(s_t.begin(), s_t.end());
    if (s_s != s_t) {
      cout << -1 << '\n';
      continue;
    }
    swap(s, t);
    long long l = 0, r = n;
    while (r - l > 1) {
      long long m = (l + r) / 2;
      if (check(m)) {
        r = m;
      } else {
        l = m;
      }
    }
    if (check(l)) {
      cout << l << '\n';
    } else if (check(r)) {
      cout << r << '\n';
    } else {
      cout << -1 << '\n';
    }
  }
  return 0;
}
