#include <bits/stdc++.h>
using namespace std;
int q, n;
string s, t;
int main() {
  ios::sync_with_stdio(false);
  cin >> q;
  while (q--) {
    cin >> n >> s >> t;
    string s1 = s, t1 = t;
    sort(s1.begin(), s1.end());
    sort(t1.begin(), t1.end());
    if (s1 != t1) {
      cout << -1 << "\n\n";
      continue;
    }
    bool dp[n + 5][n + 5];
    for (int i = 0; i < n + 5; ++i)
      for (int j = 0; j < n + 5; ++j) dp[i][j] = false;
    for (int i = 0; i < n; ++i) {
      int k = i;
      for (int j = 0; j < n; ++j)
        if (s[j] == t[k] && k < n) k++;
      for (int j = i; j < k; ++j) dp[i][j] = true;
    }
    int ans = 1e9;
    for (int i = 0; i < n; ++i)
      for (int j = i; j < n; ++j)
        if (dp[i][j] == true) ans = min(ans, i + n - j - 1);
    if (ans == 1e9) ans = -1;
    cout << ans << "\n\n";
  }
  return 0;
}
