#include <bits/stdc++.h>
using namespace std;
int dp[2005][2005];
char s[2005], x[2005];
int cnt1[2005][26], cnt2[2005][26];
int n;
int dfs(int i, int j) {
  if (i > n && j > n) return 0;
  if (i > n) return 1e9;
  if (dp[i][j] != -1) return dp[i][j];
  int ans = 1e9;
  if (i <= n && j <= n) {
    if (s[i] == x[j]) ans = min(ans, dfs(i + 1, j + 1));
    if (cnt1[n][x[j] - 'a'] - cnt1[i][x[j] - 'a'] > 0)
      ans = min(ans, dfs(i, j + 1) + 1);
  }
  if (i < j && cnt2[j - 1][s[i] - 'a'] - cnt1[i - 1][s[i] - 'a'] > 0)
    ans = min(ans, dfs(i + 1, j));
  return dp[i][j] = ans;
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    memset(cnt1, 0, sizeof(cnt1));
    memset(cnt2, 0, sizeof(cnt2));
    scanf("%d", &n);
    scanf("%s%s", s + 1, x + 1);
    for (int i = 1; i <= n; i++) {
      cnt1[i][s[i] - 'a']++;
      cnt2[i][x[i] - 'a']++;
      for (int j = 0; j < 26; j++) {
        cnt1[i][j] += cnt1[i - 1][j];
        cnt2[i][j] += cnt2[i - 1][j];
      }
    }
    bool ok = 1;
    for (int i = 0; i < 26; i++) {
      if (cnt1[n][i] != cnt2[n][i]) ok = 0;
    }
    if (!ok) {
      printf("-1\n");
      continue;
    }
    for (int i = 1; i <= n + 1; i++)
      for (int j = 1; j <= n + 1; j++) dp[i][j] = -1;
    printf("%d\n", dfs(1, 1));
  }
}
