#include <bits/stdc++.h>
#pragma GCC diagnostic ignored "-Wshadow"
#pragma GCC diagnostic ignored "-Wsign-compare"
using namespace std;
const long long inf = 1e18 + 9;
const long long N = 1000005;
const long long mod = 998244353;
long long n, m, k = 0, x, y, z, l, r, w, d, q, ct = 0, ans = 0, f = 0;
string s, p;
set<long long> si;
map<long long, long long> mi;
vector<long long> freq[26];
string t;
long long get_val(long long pos) {
  long long res = 0;
  long long j = -1;
  for (long long i = pos; i < n; i++) {
    auto it = upper_bound(freq[t[i] - 'a'].begin(), freq[t[i] - 'a'].end(), j);
    if (it == freq[t[i] - 'a'].end() || *it <= j) break;
    j = *it;
    res++;
  }
  return res;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cerr.tie(NULL);
  long long to;
  cin >> to;
  while (to--) {
    ans = 0;
    for (long long i = 0; i < 26; i++) freq[i].clear();
    cin >> n >> s >> t;
    string p = s;
    string q = t;
    sort(p.begin(), p.end());
    sort(q.begin(), q.end());
    if (p != q) {
      cout << -1 << " ";
      cout << "\n";
      continue;
    }
    for (long long i = 0; i < n; i++) freq[s[i] - 'a'].push_back(i);
    for (long long i = 0; i < 26; i++) sort(freq[i].begin(), freq[i].end());
    for (long long i = 0; i < n; i++) {
      ans = max(ans, get_val(i));
    }
    cout << n - ans << "\n";
  }
}
