#include <bits/stdc++.h>
using namespace std;
int n, l, dp[2010][2010], nums[2010][226], numt[2010][226];
string s, t;
void prep() {
  reverse(s.begin(), s.end());
  s = "0" + s;
  reverse(t.begin(), t.end());
  t = "0" + t;
  for (int i = 0; i <= l; i++)
    for (int j = 0; j <= l; j++) dp[i][j] = 1e5;
  for (int i = 0; i <= l; i++) dp[i][0] = i;
}
int count_and_judge() {
  for (int i = 1; i <= l; i++)
    for (int j = 'a'; j <= 'z'; j++) {
      nums[i][j] = nums[i - 1][j] + (s[i] == j);
      numt[i][j] = numt[i - 1][j] + (t[i] == j);
    }
  for (int i = 'a'; i <= 'z'; i++)
    if (nums[l][i] != numt[l][i]) return 0;
  return 1;
}
int work() {
  if (count_and_judge() == 0) return -1;
  for (int i = 1; i <= l; i++)
    for (int j = 1; j <= i; j++) {
      int flag = 0;
      for (int k = 'a'; k <= 'z'; k++)
        if (nums[i][k] < numt[j][k]) flag = 1;
      if (flag) continue;
      if (dp[i - 1][j] < 1e5) dp[i][j] = min(dp[i][j], dp[i - 1][j] + 1);
      if (dp[i][j - 1] < 1e5) dp[i][j] = min(dp[i][j], dp[i][j - 1]);
      if (s[i] == t[j] && dp[i - 1][j - 1] < 1e5)
        dp[i][j] = min(dp[i][j], dp[i - 1][j - 1]);
    }
  return dp[l][l];
}
int main() {
  cin >> n;
  while (n--) {
    cin >> l >> s >> t;
    prep();
    cout << work() << endl;
  }
  return 0;
}
