#include <bits/stdc++.h>
const int MAXN = 2e3 + 19;
int n, dp[MAXN][MAXN];
char s[MAXN], t[MAXN];
namespace substrings {
int cnt1[MAXN][26], cnt2[MAXN][26];
int main() {
  for (int i = 1; i <= n; ++i)
    for (char c = 'a'; c <= 'z'; ++c)
      cnt1[i][c - 'a'] = cnt1[i - 1][c - 'a'] + (s[i] == c),
                  cnt2[i][c - 'a'] = cnt2[i - 1][c - 'a'] + (t[i] == c);
  for (int i = 0; i < 26; ++i)
    if (cnt1[n][i] != cnt2[n][i]) {
      std::puts("-1");
      return 0;
    }
  if (std::strcmp(s + 1, t + 1) == 0) {
    std::puts("0");
    return 0;
  }
  std::memset(dp[1], 0x3f, (sizeof dp[1]) * (n + 1));
  for (int i = 1; i <= n; ++i) dp[0][i] = 0;
  for (int i = 1; i <= n; ++i)
    for (int j = i; j <= n; ++j) {
      dp[i][j] = dp[i - 1][j] + 1;
      if (s[i] == t[j])
        dp[i][j] = std::min(dp[i][j], dp[i - 1][j - 1]);
      else if (cnt1[i][t[j] - 'a'] < cnt2[j][t[j] - 'a'])
        dp[i][j] = std::min(dp[i][j], dp[i][j - 1]);
    }
  std::printf("%d\n", dp[n][n]);
  return 0;
}
}  // namespace substrings
int main() {
  int T;
  std::scanf("%d", &T);
  for (int i = 1; i <= T; ++i) {
    std::scanf("%d%s%s", &n, s + 1, t + 1);
    substrings::main();
  }
  return 0;
}
