#include <bits/stdc++.h>
using namespace std;
int find(vector<char>& s, vector<char>& t, int n) {
  int com[100] = {0};
  for (int i = 0; i < n; ++i) {
    int j = i;
    int k = 0;
    while (j < n && k < n) {
      if (t[j] == s[k]) {
        ++com[i];
        ++j;
        ++k;
      } else {
        ++k;
      }
    }
  }
  int m = 0;
  for (int i = 0; i < n; ++i) {
    m = max(m, com[i]);
  }
  return m;
}
int main() {
  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int n;
    string s, t;
    cin >> n;
    cin >> s;
    cin >> t;
    vector<char> sc;
    for (auto c : s) sc.push_back(c);
    vector<char> tc;
    for (auto c : t) tc.push_back(c);
    vector<char> sc2 = sc;
    vector<char> tc2 = tc;
    sort(sc2.begin(), sc2.end());
    sort(tc2.begin(), tc2.end());
    int p = 1;
    for (int i = 0; i < n; ++i) {
      if (sc2[i] != tc2[i]) {
        p = 0;
      }
    }
    if (p == 0) {
      cout << "-1\n";
    } else {
      int m = find(sc, tc, n);
      cout << n - m << "\n";
    }
  }
}
