#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline T read() {
  T s = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    s = (s << 3) + (s << 1) + ch - 48;
    ch = getchar();
  }
  return s * f;
}
constexpr int mod = 1e9 + 9;
constexpr int N = 55;
int f[30];
int found(int x) {
  if (f[x] == x) return x;
  return f[x] = found(f[x]);
}
void unit(int x, int y) {
  x = found(x), y = found(y);
  if (x == y) return;
  f[y] = x;
}
void solve() {
  for (int i = 1; i <= 26; ++i) f[i] = i;
  int n = read<int>();
  string a, b;
  cin >> a >> b;
  bool flag = true;
  for (int i = 0; i < n; ++i) {
    if (a[i] > b[i]) {
      flag = false;
      break;
    }
    unit(a[i] - 'a' + 1, b[i] - 'a' + 1);
  }
  if (!flag) {
    puts("-1");
    return;
  }
  int cnt = 0;
  for (int i = 0; i < 26; ++i) {
    if (f[i] == i) ++cnt;
  }
  cout << 26 - cnt << endl;
}
int main() {
  int _ = read<int>();
  while (_--) solve();
}
