#include <bits/stdc++.h>
using namespace std;
long long int power(long long int x, long long int y, long long int m) {
  if (y == 0) return 1;
  long long int p = power(x, y / 2, m) % m;
  p = (p * p) % m;
  return (y % 2 == 0) ? p : (x * p) % m;
}
long long int nCr(long long int n, long long int r, long long int m) {
  if (r > n) return 0;
  long long int a = 1, b = 1, i;
  for (i = 0; i < r; i++) {
    a = (a * n) % m;
    --n;
  }
  while (r) {
    b = (b * r) % m;
    --r;
  }
  return (a * power(b, m - 2, m)) % m;
}
vector<vector<int>> sufs, suft, ans;
string s, t;
int dp(int a, int b) {
  if (b == 0) return 0;
  if (ans[a][b] != -1) return ans[a][b];
  ans[a][b] = 1e5;
  if (a > 0) ans[a][b] = min(ans[a][b], 1 + dp(a - 1, b));
  if (s[a - 1] == t[b - 1] && a > 0)
    ans[a][b] = min(ans[a][b], dp(a - 1, b - 1));
  int ch = t[b - 1] - 'a';
  if (sufs[a][ch] - suft[b][ch] > 0) ans[a][b] = min(ans[a][b], dp(a, b - 1));
  return ans[a][b];
}
void solve() {
  int n;
  cin >> n;
  cin >> s >> t;
  sufs = suft = vector<vector<int>>(n + 1, vector<int>(26, 0));
  for (int i = n - 1; i >= 0; --i) {
    sufs[i][s[i] - 'a']++;
    suft[i][t[i] - 'a']++;
    for (int j = 0; j < 26; ++j) {
      sufs[i][j] += sufs[i + 1][j];
      suft[i][j] += suft[i + 1][j];
      if (i == 0 && sufs[i][j] != suft[i][j]) {
        cout << -1 << '\n';
        return;
      }
    }
  }
  ans = vector<vector<int>>(n + 1, vector<int>(n + 1, -1));
  cout << dp(n, n);
  cout << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
