#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  std::cin >> t;
  while (t--) {
    string s1, s2;
    int n;
    std::cin >> n;
    std::cin >> s1;
    std::cin >> s2;
    int dp[n + 1][n + 1];
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= n; j++) dp[i][j] = 0;
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (s1[j - 1] == s2[i - 1])
          dp[i][j] = 1 + dp[i - 1][j - 1];
        else
          dp[i][j] = dp[i][j - 1];
      }
    }
    int ans = 0;
    for (int i = 1; i <= n; i++) ans = max(ans, dp[i][n]);
    int a[26] = {0};
    int b[26] = {0};
    for (int i = 0; i < n; i++) {
      a[(int)(s1[i] - 'a')]++;
      b[(int)(s2[i] - 'a')]++;
    }
    int flag = 0;
    for (int i = 0; i < 26; i++) {
      if (a[i] != b[i]) {
        flag = 1;
      }
    }
    if (flag == 1)
      cout << -1 << endl;
    else
      cout << n - ans << endl;
  }
  return 0;
}
