#include <bits/stdc++.h>
using namespace std;
int ans[200][200];
int lcs[200];
int main() {
  int T;
  cin >> T;
  while (T--) {
    int n;
    cin >> n;
    string s, t;
    cin >> s >> t;
    string ss = s;
    string tt = t;
    sort(ss.begin(), ss.end());
    sort(tt.begin(), tt.end());
    if (ss != tt) {
      cout << "-1\n";
      continue;
    }
    int cnt = 0;
    int temp;
    for (int i = 0; i < n; i++) {
      cnt = 0;
      lcs[0] = 0;
      for (int j = 0; j < n; j++) {
        if (s[j] == t[i]) {
          lcs[j] = 1;
        } else {
          lcs[j] = lcs[j - 1];
        }
      }
      ans[i][i] = lcs[n - 1];
      for (int j = i + 1; j < n; j++) {
        temp = ans[i][j - 1];
        int ind = 0;
        for (int k = 0; k < n; k++) {
          if (lcs[k] == temp) {
            ind = k;
            break;
          }
        }
        if (temp > 0) ind++;
        int flag = 0;
        for (int k = ind; k < n; k++) {
          if (flag) {
            lcs[k] = temp + 1;
          } else {
            if (s[k] == t[j]) {
              flag = 1;
              lcs[k] = temp + 1;
            } else {
              lcs[k] = temp;
            }
          }
        }
        ans[i][j] = lcs[n - 1];
      }
    }
    int flag = 0;
    for (int i = 0; i < n; i++) {
      int out = 0;
      for (int j = 0; j <= i; j++) {
        if (ans[j][n - 1 - (i - j)] + i == n) {
          out = i;
          flag = 1;
          break;
        }
      }
      if (flag) {
        cout << out << "\n";
        break;
      }
    }
    if (flag == 0) cout << "-1\n";
  }
}
