#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
template <typename T, typename U>
inline void amin(T& x, U y) {
  if (y < x) x = y;
}
template <typename T, typename U>
inline void amax(T& x, U y) {
  if (x < y) x = y;
}
template <typename T>
inline void write(T x) {
  int i = 20;
  char buf[21];
  buf[20] = '\n';
  do {
    buf[--i] = x % 10 + '0';
    x /= 10;
  } while (x);
  do {
    putchar(buf[i]);
  } while (buf[i++] != '\n');
}
template <typename T>
inline T readInt() {
  T n = 0, s = 1;
  char p = getchar();
  if (p == '-') s = -1;
  while ((p < '0' || p > '9') && p != EOF && p != '-') p = getchar();
  if (p == '-') s = -1, p = getchar();
  while (p >= '0' && p <= '9') {
    n = (n << 3) + (n << 1) + (p - '0');
    p = getchar();
  }
  return n * s;
}
int find(int* s, int* t, int n) {
  int com[100] = {0};
  for (int i = 0; i < n; ++i) {
    int j = i;
    int k = 0;
    while (j < n && k < n) {
      if (t[j] == s[k]) {
        ++com[i];
        ++j;
        ++k;
      } else {
        ++k;
      }
    }
  }
  int m = 0;
  for (int i = 0; i < n; ++i) {
    m = max(m, com[i]);
  }
  return m;
}
int main() {
  int q = readInt<int>();
  int t[100];
  int s[100];
  for (int i = 0; i < q; ++i) {
    int p = 1;
    int n = readInt<int>();
    int cnt[26] = {0};
    for (int j = 0; j < n; ++j) {
      char c;
      scanf("%c", &c);
      s[j] = (int)(c - 'a');
      cnt[s[j]]++;
    }
    char c;
    scanf("%c", &c);
    for (int j = 0; j < n; ++j) {
      char c;
      scanf("%c", &c);
      t[j] = (int)(c - 'a');
      cnt[t[j]]--;
      if (cnt[t[j]] < 0) {
        p = 0;
      }
    }
    scanf("%c", &c);
    if (p == 0) {
      printf("-1\n");
    } else {
      int common = find(s, t, n);
      printf("%d\n", n - common);
    }
  }
  return 0;
}
