#include <bits/stdc++.h>
using namespace std;
int cd, cnt[2][300];
string s, t;
int main() {
  int test;
  cin >> test;
  while (test--) {
    cin >> cd;
    cin >> s;
    s = '0' + s;
    cin >> t;
    t = '0' + t;
    for (int i = 'a'; i <= 'z'; i++) {
      cnt[0][i] = cnt[1][i] = 0;
    }
    for (int i = 1; i <= cd; i++) {
      cnt[0][s[i]]++;
      cnt[1][t[i]]++;
    }
    int wa = 0;
    for (int i = 'a'; i <= 'z'; i++) {
      if (cnt[0][i] != cnt[1][i]) {
        wa = 1;
      }
    }
    if (wa == 1) {
      cout << -1 << '\n';
      continue;
    } else {
      t += '1';
      int res = 1e9;
      for (int i = 1; i <= cd; i++) {
        int st = i;
        int moved = 0;
        for (int e = 1; e <= cd; e++) {
          if (s[e] == t[st]) {
            st++;
          } else {
            moved++;
          }
        }
        res = min(res, moved);
      }
      cout << res << '\n';
    }
  }
}
