#include <bits/stdc++.h>
using namespace std;
int solve(int N, string& S, string& T) {
  vector<int> cnt(26, 0);
  int min_j = -1;
  vector<int> dp(N + 1), nx(N + 1);
  for (int i = 0; i < N; i++) {
    --cnt[S[i] - 'a'];
    while (cnt[S[i] - 'a'] < 0) {
      ++min_j;
      if (min_j == N) {
        return -1;
      }
      ++cnt[T[min_j] - 'a'];
    }
    int cur_best = N + 1;
    for (int j = min_j; j <= N; j++) {
      cur_best = min(cur_best, dp[j] + 1);
      nx[j] = cur_best;
      if (j < N && T[j] == S[i]) {
        cur_best = min(cur_best, dp[j]);
      }
    }
    swap(dp, nx);
  }
  return dp[N];
}
int main() {
  ios::sync_with_stdio(false);
  int T;
  cin >> T;
  while (T--) {
    int N;
    cin >> N;
    string S, T;
    cin >> S >> T;
    int ans = solve(N, S, T);
    cout << ans << '\n';
  }
}
