#include <bits/stdc++.h>
using namespace std;
int main() {
  int q, n, len, ans;
  char s[105], t[105];
  string ss, tt;
  scanf("%d", &q);
  while (q--) {
    scanf("%d %s %s", &n, s, t);
    ss = s;
    tt = t;
    sort(ss.begin(), ss.end());
    sort(tt.begin(), tt.end());
    if (ss != tt) {
      puts("-1");
      continue;
    }
    ans = 0;
    for (int i = 0; i < n; i++) {
      len = 0;
      for (int j = 0; j < n; j++)
        if (t[i + len] == s[j]) len++;
      ans = max(ans, len);
    }
    printf("%d\n", n - ans);
  }
  return 0;
}
