#include <bits/stdc++.h>
using namespace std;
int lcs(string &start, string &end, int c1, int c2) {
  if (c1 == start.length() || c2 == end.length()) {
    return 0;
  }
  int ret = 0;
  if (start[c1] == end[c2]) {
    return 1 + lcs(start, end, c1 + 1, c2 + 1);
  }
  return lcs(start, end, c1 + 1, c2);
}
void testCase() {
  int n;
  cin >> n;
  string start, end;
  cin >> start;
  cin >> end;
  int startFreq[26] = {0};
  int endFreq[26] = {0};
  for (int i = 0; i < n; i++) {
    startFreq[start[i] - 'a']++;
    endFreq[end[i] - 'a']++;
  }
  for (int i = 0; i < 26; i++) {
    if (startFreq[i] != endFreq[i]) {
      cout << -1 << endl;
      return;
    }
  }
  int longest = 0;
  for (int i = 0; i < end.size(); i++) {
    longest = max(longest, lcs(start, end, 0, i));
  }
  cout << (n - longest) << endl;
}
int main() {
  int n;
  cin >> n;
  while (n--) testCase();
  return 0;
}
