#include <bits/stdc++.h>
using namespace std;
int base[27];
int main() {
  int cases;
  int n;
  scanf("%d", &cases);
  while (cases--) {
    memset(base, 0x00, sizeof(base));
    string a, b;
    cin >> n;
    cin >> a;
    cin >> b;
    for (int i = 0; i < n; i++) base[a[i] - 'a']++;
    for (int i = 0; i < n; i++) base[b[i] - 'a']--;
    int flag = 1;
    for (int i = 0; i <= 25; i++)
      if (base[i]) flag = 0;
    if (!flag) {
      puts("-1");
      continue;
    }
    int maxi = 0;
    for (int i = 0; i < n; i++) {
      int l = i, r = 0;
      while (l < n) {
        while (a[r] != b[l] && r < n) r++;
        if (r == n) break;
        l++;
        r++;
      }
      maxi = max(maxi, l - i);
    }
    cout << n - maxi << endl;
  }
  return 0;
}
