#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  char ch = getchar();
  long long p = 1, data = 0;
  while (ch < '0' || ch > '9') {
    if (ch == '-') p = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    data = data * 10 + (ch ^ 48);
    ch = getchar();
  }
  return p * data;
}
inline long long qpow(long long a, long long b) {
  long long r = 1;
  while (b) {
    if (b & 1) r = a * r;
    a = a * a;
    b >>= 1;
  }
  return r;
}
inline long long gcd(long long a, long long b) {
  if (!a || !b) return (!a) ? b : a;
  while (b ^= a ^= b ^= a %= b)
    ;
  return a;
}
inline long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
void exgcd(long long a, long long b, long long &x, long long &y) {
  if (!b)
    x = 1, y = 0;
  else {
    exgcd(b, a % b, y, x);
    y -= x * (a / b);
  }
}
const int mod = 1e9 + 7, maxn = 1e5 + 5;
char s[maxn], p[maxn];
int main() {
  int T = read();
  while (T--) {
    int n = read();
    scanf("%s%s", s + 1, p + 1);
    int ok = 1, ans = 0;
    set<int> v[21];
    for (int i = 1; i <= n; i++) {
      if (s[i] > p[i]) {
        ok = 0;
        break;
      } else if (s[i] < p[i])
        v[s[i] - 'a'].insert(p[i] - 'a');
    }
    if (!ok) {
      puts("-1");
      continue;
    }
    for (int i = 0; i < 20; i++) {
      if (v[i].empty()) continue;
      ans++;
      int mi = 22;
      for (auto x : v[i]) mi = min(mi, x);
      for (auto x : v[i])
        if (mi != x) v[mi].insert(x);
    }
    printf("%d\n", ans);
  }
  return 0;
}
