#include <bits/stdc++.h>
using namespace std;
int num1[30], num2[30];
bool check(string a, string b) {
  memset(num1, 0, sizeof(num1));
  memset(num2, 0, sizeof(num2));
  for (int i = 0; i < a.length(); i++) {
    num1[a[i] - 'a']++;
    num2[b[i] - 'a']++;
  }
  for (int i = 0; i < 30; i++) {
    if (num1[i] != num2[i]) return false;
  }
  return true;
}
int main() {
  int T;
  cin >> T;
  while (T--) {
    int n;
    cin >> n;
    string a, b;
    cin >> b >> a;
    int len = a.length();
    if (!check(a, b)) {
      cout << -1 << '\n';
      continue;
    }
    int maxn = 0;
    for (int i = 0; i < len; i++) {
      int begin = 0;
      for (int j = 0; i + j < len; j++) {
        int t = i + j;
        while (a[t] != b[begin]) {
          begin++;
          if (begin == len) break;
        }
        if (begin == len || begin == len - 1) {
          maxn = max(maxn, j + len - begin);
          break;
        } else if (i + j == len - 1) {
          maxn = max(maxn, j + 1);
        }
        begin++;
      }
    }
    cout << len - maxn << '\n';
  }
  return 0;
}
