#include <bits/stdc++.h>
using namespace std;
int T, n, dp[3003][3003], s1[3003][27], s2[3003][27];
char s[200006], t[200006];
int main() {
  scanf("%d", &T);
  while (T--) {
    scanf("%d%s%s", &n, s + 1, t + 1);
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j < 26; j++)
        s1[i][j] = s1[i - 1][j], s2[i][j] = s2[i - 1][j];
      s1[i][s[i] - 'a']++, s2[i][t[i] - 'a']++;
    }
    bool flag = true;
    for (int i = 0; i < 26; i++)
      if (s1[n][i] ^ s2[n][i]) {
        flag = false;
        break;
      }
    if (!flag) {
      puts("-1");
      continue;
    }
    for (int i = 0; i <= n; i++)
      for (int j = 0; j <= n; j++) dp[i][j] = 1e9;
    for (int i = 0; i <= n; i++) dp[0][i] = 0;
    for (int i = 1; i <= n; i++)
      for (int j = i; j <= n; j++) {
        dp[i][j] = 1e9;
        if (s[i] == t[j]) dp[i][j] = min(dp[i][j], dp[i - 1][j - 1]);
        dp[i][j] = min(dp[i][j], dp[i - 1][j] + 1);
        if (s1[i][t[j] - 'a'] < s2[j][t[j] - 'a'])
          dp[i][j] = min(dp[i][j], dp[i][j - 1]);
      }
    printf("%d\n", dp[n][n]);
  }
}
