#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
const int N = 1e5 + 9, M = 2e6;
const int Maxn = 1000010;
const long long inf = 0x3f3f3f3f3f3f3f3f;
const int mod = 998244353;
long long qp(long long x, long long y) {
  long long ans = 1;
  x %= mod;
  while (y) {
    if (y & 1) ans = ans * x % mod;
    x = x * x % mod;
    y >>= 1;
  }
  return ans;
}
long long inv(long long x) { return qp(x, mod - 2); }
int _, n;
int a[N];
char b[N], c[N];
struct node {
  int x, y;
  friend bool operator<(node a, node b) {
    if (a.x == b.x) return a.y < b.y;
    return a.x < b.x;
  }
} s[N];
int mp[30];
int findx(int x) {
  if (mp[x] == x) return x;
  return mp[x] = findx(mp[x]);
}
int main() {
  for (scanf("%d", &_); _; _--) {
    scanf("%d", &n);
    scanf("%s", b + 1);
    scanf("%s", c + 1);
    int flag = 1;
    for (int i = 1; i <= n; i++) {
      int x = b[i] - 'a' + 1, y = c[i] - 'a' + 1;
      s[i].x = x, s[i].y = y;
      if (y < x) flag = 0;
    }
    if (!flag) {
      puts("-1");
      continue;
    }
    sort(s + 1, s + n + 1);
    int ans = 0;
    for (int i = 0; i <= 20; i++) mp[i] = i;
    for (int i = 1; i <= n; i++) {
      int x = findx(s[i].x), y = findx(s[i].y);
      if (x != y) {
        mp[x] = min(y, x);
        mp[y] = min(y, x);
        ans++;
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}
