#include <bits/stdc++.h>
using namespace std;
const long long N = 3000;
vector<vector<long long>> mem(3000, vector<long long>(3000, -1));
string s, t;
vector<vector<long long>> sufs(26, vector<long long>(3000)), suft = sufs;
long long cnt = 0;
long long sol(long long i, long long j) {
  if (j == 0) return 0;
  long long &ans = mem[i][j];
  if (ans != -1) return ans;
  ans = 1e9;
  cnt++;
  if (i > 0) {
    ans = 1 + sol(i - 1, j);
    if (s[i - 1] == t[j - 1]) {
      ans = min(ans, sol(i - 1, j - 1));
    }
  }
  long long c = t[j - 1] - 'a';
  if (sufs[c][i + 1] > suft[c][j + 1]) {
    ans = min(ans, sol(i, j - 1));
  }
  return ans;
}
signed main() {
  long long tt;
  cin >> tt;
  while (tt--) {
    long long n;
    cnt = 0;
    cin >> n >> s >> t;
    for (long long i = 0; i <= n; i++) {
      for (long long j = 0; j <= n; ++j) {
        mem[i][j] = -1;
      }
    }
    for (long long i = 0; i < 26; ++i) {
      for (long long j = 0; j <= n + 1; ++j) {
        sufs[i][j] = 0;
        suft[i][j] = 0;
      }
    }
    for (long long i = n; i >= 1; --i) {
      for (long long j = 0; j < 26; ++j) {
        sufs[j][i] = sufs[j][i + 1];
        suft[j][i] = suft[j][i + 1];
      }
      sufs[s[i - 1] - 'a'][i]++;
      suft[t[i - 1] - 'a'][i]++;
    }
    long long res = sol(n, n);
    cout << (res > 1e9 ? -1 : res) << '\n';
  }
  return 0;
}
