#include <bits/stdc++.h>
using namespace std;
using namespace std;
const int INF = 2147483647;
const int INF2 = 0x3f3f3f3f;
const long long INF64 = 3e18;
const double INFD = 1e30;
const double EPS = 1e-10;
const long double PI = acosl(-1);
const int MOD = 998244353;
template <typename T>
inline T read() {
  T X = 0, w = 0;
  char ch = 0;
  while (!isdigit(ch)) {
    w |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
  return w ? -X : X;
}
const int MAXN = 100005;
const int MAXV = 200001;
int n, m, k;
int dr[4] = {1, 0, -1, 0};
int dc[4] = {0, 1, 0, -1};
int dr2[8] = {1, 1, 1, -1, -1, -1, 0, 0};
int dc2[8] = {1, 0, -1, 1, 0, -1, 1, -1};
int CASE = 1;
char A[MAXN], B[MAXN];
bool M[30][30];
void solve() {
  scanf("%d", &n);
  scanf("%s", A);
  scanf("%s", B);
  memset(M, 0, sizeof(M));
  for (int i = 0; i < n; i++) {
    if (A[i] != B[i]) {
      M[A[i] - 'a'][B[i] - 'a'] = true;
      if (A[i] > B[i]) {
        printf("-1\n");
        return;
      }
    }
  }
  int c = 0;
  for (int i = 0; i < 20; i++) {
    for (int j = i + 1; j < 20; j++) {
      if (M[i][j]) {
        c++;
        for (int k = j + 1; k < 20; k++) {
          if (M[i][k]) M[j][k] = true;
        }
        break;
      }
    }
  }
  printf("%d\n", c);
}
int main() {
  int T = read<int>();
  while (T--) {
    solve();
  }
  return 0;
}
