#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5, M = 50 + 5;
const long long INF = 1e18 + 5;
inline long long read() {
  long long sum = 0, fh = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') fh = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    sum = sum * 10 + c - '0';
    c = getchar();
  }
  return sum * fh;
}
inline int read2() {
  char c = getchar();
  while (c != '0' && c != '1') c = getchar();
  return c == '0' ? 0 : 1;
}
inline int read3() {
  char c = getchar();
  while (c < 'a' || c > 'z') {
    c = getchar();
  }
  return c - 'a';
}
inline void write(long long x) {
  if (x < 0) putchar('-'), x = -x;
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
}
inline int gcd(int x, int y) { return y == 0 ? x : gcd(y, x % y); }
inline long long ab(long long x) { return x < 0 ? -x : x; }
inline long long fpow(long long qwe, long long asd, long long zxc) {
  if (asd < 0) return 0;
  long long a = qwe, b = 1, c = asd;
  while (c) {
    if (c & 1) b = b * a % zxc;
    a = a * a % zxc;
    c >>= 1;
  }
  return b;
}
struct miaow {
  int f, t, l;
  long long c;
  miaow(int f = 0, int t = 0, long long c = 0, int l = 0)
      : f(f), t(t), c(c), l(l) {}
} imap[N * 2 + 5];
int str[N * 2 + 5] = {0}, cou = 2;
inline void jb(int f, int t, long long c = 0) {
  imap[cou] = miaow(f, t, c, str[f]);
  str[f] = cou++;
  imap[cou] = miaow(t, f, c, str[t]);
  str[t] = cou++;
}
set<int> a[31];
string qwe, asd;
int main() {
  int t = read();
  while (t) {
    --t;
    int n = read();
    cin >> qwe >> asd;
    for (int i = 0; i < 26; ++i) a[i].clear();
    int kx = 1;
    for (int i = 0; i < n; ++i) {
      if (qwe[i] != asd[i]) {
        if (qwe[i] > asd[i]) {
          kx = 0;
          break;
        }
        a[qwe[i] - 'a'].insert(asd[i] - 'a');
      }
    }
    if (!kx) {
      puts("-1");
      continue;
    }
    int ans = 0;
    for (int i = 0; i < 26; ++i) {
      if (!a[i].size()) continue;
      ans += 1;
      int x = *a[i].begin();
      a[i].erase(x);
      for (set<int>::iterator j = a[i].begin(); j != a[i].end(); ++j) {
        a[x].insert(*j);
      }
    }
    write(ans), putchar('\n');
  }
}
