#include <bits/stdc++.h>   using namespace std;   const int N = 2e3 + 7;   const int inf = 1e9 + 7;   int T, n, dp[N][N], cnt[N], qzs[N][26], qzt[N][26];   char s[N], t[N];   void Min(int &x, int y) {     if (x > y) x = y;   }   void Main() {     fill(cnt, cnt + 26, 0);     scanf( %d , &n);     for (int i = 0, iE = n; i <= iE; i++)       for (int j = 0, jE = n; j <= jE; j++) dp[i][j] = inf;     scanf( %s%s , s + 1, t + 1);     for (int i = 1, iE = n; i <= iE; i++) cnt[s[i] -  a ]++, cnt[t[i] -  a ]--;     for (int i = 0, iE = 25; i <= iE; i++)       if (cnt[i]) return puts( -1 ), void();     for (int i = 1, iE = n; i <= iE; i++)       for (int j = 0, jE = 25; j <= jE; j++)         qzs[i][j] = qzs[i - 1][j] + (s[i] ==  a  + j),         qzt[i][j] = qzt[i - 1][j] + (t[i] ==  a  + j);     for (int i = 0, iE = n; i <= iE; i++) dp[0][i] = 0;     for (int i = 1, iE = n; i <= iE; i++)       for (int j = i, jE = n; j <= jE; j++) {         Min(dp[i][j], dp[i - 1][j] + 1);         if (s[i] == t[j]) Min(dp[i][j], dp[i - 1][j - 1]);         if (qzs[i][t[j] -  a ] < qzt[j][t[j] -  a ]) Min(dp[i][j], dp[i][j - 1]);       }     printf( %d n , dp[n][n]);   }   int main() {     scanf( %d , &T);     while (T--) Main();     return 0;   }   