#include <bits/stdc++.h>
using namespace std;
const int inf = 2e9;
const long long INF = 8e18;
const int maxn = 2e3 + 5;
int n;
string s, t;
int dp[maxn][maxn], suf[maxn][26], suf2[maxn][26];
bool vis[maxn][maxn];
inline int dfs(int i, int j) {
  if (vis[i][j]) return dp[i][j];
  vis[i][j] = 1;
  if (s[i - 1] == t[j - 1]) dp[i][j] = min(dp[i][j], dfs(i - 1, j - 1));
  if (suf[i + 1][t[j - 1] - 'a'] > suf2[j + 1][t[j - 1] - 'a'])
    dp[i][j] = min(dp[i][j], 1 + dfs(i, j - 1));
  if (i > 0) dp[i][j] = min(dp[i][j], dfs(i - 1, j));
  return dp[i][j];
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int T;
  cin >> T;
  while (T--) {
    cin >> n >> s >> t;
    for (int i = 0; i < n + 5; ++i)
      for (int j = 0; j < n + 5; ++j) vis[i][j] = 0, dp[i][j] = inf;
    for (int i = 0; i < n + 5; ++i)
      for (int j = 0; j < 26; ++j) suf[i][j] = suf2[i][j] = 0;
    for (int i = n; i >= 1; --i) {
      for (int j = 0; j < 26; ++j) {
        suf[i][j] = suf[i + 1][j];
        suf2[i][j] = suf2[i + 1][j];
      }
      ++suf[i][s[i - 1] - 'a'];
      ++suf2[i][t[i - 1] - 'a'];
    }
    vis[0][0] = 1, dp[0][0] = 0;
    dfs(n, n);
    if (dp[n][n] == inf)
      cout << -1 << '\n';
    else
      cout << dp[n][n] << '\n';
  }
  return 0;
}
