#include <bits/stdc++.h>
using namespace std;
double start_moment = 0;
double get_runtime() { return 1.0 * clock() / CLOCKS_PER_SEC; }
void reset_timer() { start_moment = get_runtime(); }
double timer_time() { return get_runtime() - start_moment; }
void runtime() { cout << fixed << setprecision(5) << get_runtime() << endl; }
template <class T>
void read(vector<T> &a, int n) {
  T x;
  a.clear();
  for (int i = 0; i < n; i++) {
    cin >> x;
    a.push_back(x);
  }
}
template <class T>
void write(vector<T> &a) {
  for (T x : a) cout << x << ' ';
  cout << '\n';
}
const int N = 2003;
const int INF = 1000000000;
int T, len;
string second, t;
int cnt1[26], cnt2[26];
int have_s[26][N];
int have_t[26][N];
int get(int *have, int x) {
  if (x < 0) return 0;
  return have[x];
}
int get(int *have, int l, int r) { return get(have, r) - get(have, l - 1); }
int dp[N][N];
int go(int x, int y) {
  if (dp[x][y] != INF) return dp[x][y];
  if (second[x - 1] == t[y - 1]) dp[x][y] = min(dp[x][y], go(x - 1, y - 1));
  dp[x][y] = min(dp[x][y], go(x - 1, y) + 1);
  if (get(have_s[t[y - 1] - 'a'], x, len) > get(have_t[t[y - 1] - 'a'], y, len))
    dp[x][y] = min(dp[x][y], go(x, y - 1));
  return dp[x][y];
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> T;
  while (T--) {
    memset(cnt1, 0, sizeof(cnt1));
    memset(cnt2, 0, sizeof(cnt2));
    cin >> len;
    cin >> second >> t;
    for (char c : second) cnt1[c - 'a']++;
    for (char c : t) cnt2[c - 'a']++;
    bool bad = 0;
    for (int i = 0; i < 26; i++)
      if (cnt1[i] != cnt2[i]) bad = 1;
    if (bad) {
      cout << -1 << '\n';
      continue;
      ;
    }
    for (int i = 0; i <= len; i++)
      for (int j = 0; j <= len; j++) dp[i][j] = INF;
    memset(have_s, 0, sizeof(have_s));
    memset(have_t, 0, sizeof(have_t));
    for (char c = 'a'; c <= 'z'; c++) {
      for (int i = 0; i < len; i++) {
        have_s[c - 'a'][i] = (c == second[i]);
        have_t[c - 'a'][i] = (c == t[i]);
      }
      for (int i = 1; i < len; i++) {
        have_s[c - 'a'][i] += have_s[c - 'a'][i - 1];
        have_t[c - 'a'][i] += have_t[c - 'a'][i - 1];
      }
    }
    for (int i = 0; i <= len; i++) dp[0][i] = 0;
    cout << go(len, len) << '\n';
  }
}
