#include <bits/stdc++.h>
using namespace std;
int n, mem[102][102];
char s1[102], s2[102];
int dp(int p1, int p2) {
  if (p1 == n || p2 == n) return 0;
  if (mem[p1][p2] != -1) return mem[p1][p2];
  mem[p1][p2] = 0;
  if (s1[p1] == s2[p2]) mem[p1][p2] = max(mem[p1][p2], 1 + dp(p1 + 1, p2 + 1));
  mem[p1][p2] = max(mem[p1][p2], dp(p1 + 1, p2));
  return mem[p1][p2];
}
int freq[26];
int main() {
  int ts;
  scanf("%d", &ts);
  while (ts--) {
    scanf("%d %s %s", &n, s1, s2);
    memset(freq, 0, sizeof freq);
    for (int i = 0; i < n; i++) freq[s1[i] - 'a']++, freq[s2[i] - 'a']--;
    bool f = 0;
    for (int i = 0; i < 26; i++) f |= (freq[i] != 0);
    if (f)
      printf("-1\n");
    else {
      memset(mem, -1, sizeof mem);
      int res = 1;
      for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++) res = max(res, dp(i, j));
      res = n - res;
      printf("%d\n", res);
    }
  }
  return 0;
}
