#include <bits/stdc++.h>
using namespace std;
const int mxn = 2001, k = 26;
int n;
int a[2][mxn];
int second[2][mxn][k];
int dp[mxn][mxn];
void answer() {
  cin >> n;
  for (int i = 0; i < 2; i++)
    for (int j = 1; j <= n; j++) {
      char c;
      cin >> c;
      a[i][j] = c - 'a';
      memcpy(second[i][j], second[i][j - 1], sizeof(second[i][j]));
      second[i][j][a[i][j]]++;
    }
  if (memcmp(second[0][n], second[1][n], sizeof(second[0][n]))) {
    cout << -1 << '\n';
    return;
  }
  for (int i = 1; i <= n; i++)
    for (int j = i; j <= n; j++) {
      dp[i][j] = dp[i - 1][j] + 1;
      if (a[0][i] == a[1][j]) dp[i][j] = min(dp[i][j], dp[i - 1][j - 1]);
      if (second[0][i][a[1][j]] < second[1][j][a[1][j]])
        dp[i][j] = min(dp[i][j], dp[i][j - 1]);
    }
  cout << dp[n][n] << '\n';
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) answer();
  return 0;
}
