#include <bits/stdc++.h>
using namespace std;
map<char, int> dd;
vector<int> a[1000];
int f[1000];
pair<int, int> b[1000];
int n, t, cnt, maxx, dem;
string st2, st1, stt1, stt2;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(0);
  cin >> t;
  while (t--) {
    dd.clear();
    cnt = 0;
    maxx = -1;
    cin >> n;
    cin >> st1 >> st2;
    stt1 = st1;
    stt2 = st2;
    sort(stt1.begin(), stt1.end());
    sort(stt2.begin(), stt2.end());
    if (stt1 != stt2) {
      cout << -1 << endl;
      ;
      continue;
    }
    for (int i = 0; i < n; i++) {
      f[i + 1] = 0;
      if (dd[st2[i]] == 0) {
        dd[st2[i]] = ++cnt;
        a[dd[st2[i]]].clear();
      }
      a[dd[st2[i]]].push_back(i + 1);
    }
    for (int i = 0; i < n; i++) {
      if (dd[st1[i]] == 0) {
        maxx = -1;
        break;
      }
      dem = 0;
      for (auto x : a[dd[st1[i]]]) {
        if (f[x] < f[x - 1] + 1) b[++dem] = {x, f[x - 1] + 1};
      }
      for (int j = 1; j <= dem; j++) {
        f[b[j].first] = b[j].second;
        maxx = max(f[b[j].first], maxx);
      }
    }
    if (maxx < 0)
      cout << maxx << endl;
    else
      cout << n - maxx << '\n';
  }
}
