#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10, M = 30;
char a[N], b[N];
int n, ct[M][M], fa[M], x, y;
int find_f(int x) { return x == fa[x] ? x : fa[x] = find_f(fa[x]); }
int main() {
  int T;
  cin >> T;
  while (T--) {
    scanf("%d%s%s", &n, a + 1, b + 1);
    bool flg = 1;
    memset(ct, 0, sizeof(ct));
    for (int i = 1; i <= n; i++) {
      if (a[i] > b[i]) {
        flg = 0;
        break;
      }
      if (a[i] < b[i]) ct[a[i] - 'a'][b[i] - 'a'] = 1;
    }
    if (!flg) {
      puts("-1");
      continue;
    }
    int ans = 0;
    for (int i = 0; i <= 20; i++) fa[i] = i;
    for (int i = 0; i <= 20; i++)
      for (int j = i + 1; j <= 20; j++)
        if (ct[i][j]) {
          x = find_f(i);
          y = find_f(j);
          if (x != y) ans++, fa[x] = y;
        }
    cout << ans << endl;
  }
}
