#include <bits/stdc++.h>
using namespace std;
int main() {
  int T;
  cin >> T;
  while (T--) {
    int n;
    string s, t;
    cin >> n >> s >> t;
    string s1 = s, s2 = t;
    sort(s1.begin(), s1.end());
    sort(s2.begin(), s2.end());
    if (s1 != s2)
      printf("-1\n");
    else {
      int cnt1[n + 1][26], cnt2[n + 1][26], dp[n + 1][n + 1];
      memset(cnt1, 0, sizeof cnt1);
      memset(cnt2, 0, sizeof cnt2);
      memset(dp, 0, sizeof dp);
      int l = 0, r = n - 1;
      while (s[l] == t[l] && l < n) l++;
      while (s[r] == t[r] && r >= 0) r--;
      for (int i = r; i >= l; i--) {
        cnt1[i][s[i] - 'a']++;
        cnt2[i][t[i] - 'a']++;
        for (int j = 0; j < 26; j++) {
          cnt1[i][j] += cnt1[i + 1][j];
          cnt2[i][j] += cnt2[i + 1][j];
        }
      }
      if (l <= r) {
        for (int i = l; i <= r; i++) {
          for (int j = l; j <= r; j++) {
            if (i > 0) dp[i][j] = dp[i - 1][j] + 1;
            if (j > 0 && cnt1[i + 1][t[j] - 'a'] > cnt2[j + 1][t[j] - 'a'])
              dp[i][j] = min(dp[i][j], dp[i][j - 1]);
            if (i > 0 && j > 0 && s[i] == t[j])
              dp[i][j] = min(dp[i][j], dp[i - 1][j - 1]);
          }
        }
        printf("%d\n", dp[r][r]);
      } else
        printf("0\n");
    }
  }
}
