#include <bits/stdc++.h>
using namespace std;
int n, a[2005], b[2005][2005], t, d[2005][35], g, d2[2005][35], p[2005], ans;
int main() {
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
      memset(d[i], 0, sizeof(d[i]));
      memset(d2[i], 0, sizeof(d2[i]));
    }
    for (int i = 1; i <= n; i++) {
      char c = getchar();
      while (c > 'z' || c < 'a') c = getchar();
      a[i] = c - 'a' + 1;
      for (int j = 1; j <= 26; j++) {
        d[i][j] = d[i - 1][j] + (a[i] == j);
      }
      b[0][i] = 0;
      for (int j = 1; j <= n; j++) {
        b[i][j] = 999999999;
      }
    }
    for (int i = 1; i <= n; i++) {
      char c = getchar();
      while (c > 'z' || c < 'a') c = getchar();
      p[i] = c - 'a' + 1;
      for (int j = 1; j <= 26; j++) {
        d2[i][j] = d2[i - 1][j] + (p[i] == j);
      }
    }
    int k = 0;
    for (int i = 1; i <= 26; i++) {
      if (d[n][i] != d2[n][i]) {
        puts("-1");
        k = 1;
        break;
      }
    }
    if (k == 1) continue;
    for (int i = 1; i <= n; i++) {
      for (int j = i; j <= n; j++) {
        b[i][j] = min(b[i][j], b[i - 1][j] + 1);
        if (a[i] == p[j]) b[i][j] = min(b[i][j], b[i - 1][j - 1]);
        if (d[i][p[j]] < d2[j][p[j]]) b[i][j] = min(b[i][j], b[i][j - 1]);
      }
    }
    printf("%d\n", b[n][n]);
  }
  return 0;
}
