#include <bits/stdc++.h>
using namespace std;
const int N = 2e3 + 5, C = 26 + 4;
int tst, n, dp[N], cnt[C];
vector<int> pos[C];
string s, t;
void read_input() {
  cin >> n >> s >> t;
  for (int i = 0; i < n; i++) pos[t[i] - 'a'].push_back(i);
}
void solve() {
  int mx = -1;
  for (auto c : s) {
    int x = c - 'a';
    if (pos[x].size() == cnt[x]) {
      cout << -1;
      return;
    }
    mx = max(mx, pos[x][cnt[x]++]);
    for (int i = n - 1, j = pos[x].size() - 1; i >= 0; i--) {
      while (~j && pos[x][j] > i) j--;
      if (~j && pos[x][j] >= mx)
        dp[i] = max(dp[i], dp[max(pos[x][j] - 1, 0)] + 1);
      else if (!~j)
        dp[i] = -n;
    }
  }
  cout << n - dp[n - 1];
}
void reset() {
  memset(dp, 0, sizeof dp);
  memset(cnt, 0, sizeof cnt);
  for (int i = 0; i < C; i++) pos[i].clear();
  cout << endl;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  for (cin >> tst; tst--; reset()) read_input(), solve();
  return 0;
}
