#include <bits/stdc++.h>
using namespace std;
template <class T>
T abs(T x) {
  return x > 0 ? x : -x;
}
template <class T>
T gcd(T a, T b) {
  return a ? gcd(b % a, a) : b;
}
template <class T>
T sqr(T a) {
  return a * a;
}
template <class T>
T sgn(T a) {
  return a > 0 ? 1 : (a < 0 ? -1 : 0);
}
const double pi = acos(-1.0);
int n;
int m;
string s, t;
const int inf = 1e9;
vector<vector<int> > to, cnts, cntt, f;
void init() {
  f = vector<vector<int> >(n + 1, vector<int>(n + 1, -1));
  to = vector<vector<int> >(n + 1, vector<int>(128, -1));
  cnts = vector<vector<int> >(n + 1, vector<int>(128, 0));
  cntt = vector<vector<int> >(n + 1, vector<int>(128, 0));
  for (int i = n - 1; i >= 0; i--) {
    for (int c = 'a'; c <= 'z'; c++) {
      to[i][c] = to[i + 1][c];
    }
    to[i][t[i]] = i;
  }
  for (int i = 0; i < n; i++) {
    for (int c = 'a'; c <= 'z'; c++) {
      cnts[i + 1][c] = cnts[i][c];
      cntt[i + 1][c] = cntt[i][c];
    }
    cnts[i + 1][s[i]]++;
    cntt[i + 1][t[i]]++;
  }
}
int go(int i, int j) {
  if (i == n && j == n) return 0;
  if (i >= n) return inf;
  if (f[i][j] != -1) return f[i][j];
  int cnt1 = 0, cnt2 = 0;
  for (int k = 0; k < j; k++) {
    if (t[k] == s[i]) cnt1++;
  }
  for (int k = 0; k < i; k++) {
    if (s[k] == s[i]) cnt2++;
  }
  int res = inf;
  if (cnt1 > cnt2) res = min(res, go(i + 1, j) + 1);
  for (int k = j; k < n; k++) {
    if (t[k] == s[i]) {
      res = min(res, go(i + 1, k + 1));
      break;
    }
  }
  return f[i][j] = res;
}
int main() {
  int nt;
  cin >> nt;
  for (int tt = 1; tt <= nt; tt++) {
    cin >> n >> s >> t;
    init();
    int ans = go(0, 0);
    if (ans < inf) {
      cout << ans << endl;
    } else {
      cout << -1 << endl;
    }
  }
  return 0;
}
