#include <bits/stdc++.h>
using namespace std;
string a, b;
const int INF = 1e9;
int dp[2005][2005], chk[2005][2005];
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  int t, cnt[26] = {};
  cin >> t;
  while (t--) {
    int n;
    cin >> n >> a >> b;
    for (int i = 0; i <= ((int)b.size()); ++i)
      for (int j = 0; j <= ((int)a.size()); ++j) dp[i][j] = INF, chk[i][j] = 0;
    fill(dp[((int)b.size())], dp[((int)b.size())] + ((int)a.size()) + 1, 0);
    fill(chk[((int)b.size())], chk[((int)b.size())] + ((int)a.size()) + 1, 1);
    fill(cnt, cnt + 26, 0);
    for (int i = ((int)b.size()) - 1; i >= 0; --i) {
      ++cnt[b[i] - 'a'];
      int fin = 0, cnt2[26] = {};
      for (int j = 0; j < 26; ++j)
        if (cnt[j] == 0) ++fin;
      for (int j = ((int)a.size()) - 1; j >= 0; --j) {
        ++cnt2[a[j] - 'a'];
        if (cnt2[a[j] - 'a'] == cnt[a[j] - 'a']) ++fin;
        if (fin == 26) chk[i][j] = 1;
      }
    }
    if (chk[0][0] != 1) {
      cout << "-1\n";
      continue;
    }
    for (int i = ((int)b.size()) - 1; i >= 0; --i)
      for (int j = ((int)a.size()) - 1; j >= 0; --j) {
        dp[i][j] = dp[i][j + 1];
        if (b[i] == a[j]) dp[i][j] = min(dp[i][j], dp[i + 1][j + 1]);
        if (chk[i][j]) dp[i][j] = min(dp[i][j], dp[i + 1][j] + 1);
      }
    cout << dp[0][0] << "\n";
  }
}
