#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
using namespace std;
const int mod = 998244353;
const int nag = 2e5 + 5;
template <typename T>
void add(T& a, T b) {
  a += b;
  while (a >= mod) a -= mod;
  while (a < 0) a += mod;
}
template <typename T>
void mul(T& a, T b) {
  a = a * b % mod;
}
template <typename T>
void up_self(T& a, T b) {
  a = max(a, b);
}
template <typename T>
void min_self(T& a, T b) {
  a = min(a, b);
}
string find_largest_pref_palindrome(const string& a) {
  string second = a;
  reverse(second.begin(), second.end());
  second = a + "%" + second;
  int c = 0;
  vector<int> pref(2 * ((int)a.size()) + 2);
  for (int i = 1; i < second.size(); i++) {
    while (c != 0 && second[i] != second[c]) c = pref[c - 1];
    if (second[i] == second[c]) c++;
    pref[i] = c;
  }
  return second.substr(0, c);
}
long long int binexpomodulo(long long int x, long long int y) {
  long long int res = 1;
  x %= mod;
  if (!x) return 0;
  while (y) {
    if (y & 1) {
      mul(res, x);
    }
    mul(x, x);
    y >>= 1;
  }
  return res;
}
long long int nCrInOr(long long int n, long long int r) {
  long long int res = 1;
  if (r > n - r) r = n - r;
  long long int rin = 1;
  for (long long int i = 1; i <= r; i++) rin = (rin * i) % mod;
  rin = binexpomodulo(rin, mod - 2);
  for (long long int i = 1; i <= r; i++) res = (res * (n - i + 1)) % mod;
  res = (res * rin) % mod;
  return res;
}
vector<long long int> fac, ifac;
void precompute(long long int n) {
  fac.resize(n + 1, 1);
  ifac.resize(n + 1);
  for (long long int i = 2; i <= n; i++) {
    fac[i] = (fac[i - 1] * i) % mod;
  }
  ifac[n] = binexpomodulo(fac[n], mod - 2);
  for (long long int i = n - 1; i >= 0; i--)
    ifac[i] = (ifac[i + 1] * (i + 1)) % mod;
}
long long int nCr(long long int n, long long int r) {
  return fac[n] * (ifac[n - r]) % mod * (ifac[r]) % mod;
}
int msb_pos(long long int n) {
  int ans;
  for (int i = 0; i < 64; i++)
    if (n & (1LL << i)) ans = i + 1;
  return ans;
}
long long int totient(long long int n) {
  long long int ans = n;
  for (long long int i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      ans -= (ans / i);
      while (n % i == 0) {
        n /= i;
      }
    }
  }
  if (n >= 2) {
    ans -= ans / n;
  }
  return ans;
}
int lis(vector<long long int> a, int n) {
  vector<long long int> dp(n + 1, 1e18);
  dp[0] = -1e18;
  for (int i = 0; i < n; i++) {
    int p = (int)(lower_bound(dp.begin(), dp.end(), a[i]) - dp.begin());
    if (dp[p - 1] < a[i] && a[i] < dp[p]) dp[p] = a[i];
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (dp[i] != 1e18) ans = i;
  }
  return ans;
}
bool is_prime(int n) {
  if (n == 2) return true;
  if (n & 1 ^ 1) return false;
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0) return false;
  }
  return true;
}
long long int get_(vector<long long int>& a, int pos) {
  long long int res = 0;
  for (; pos >= 0; pos = (pos & (pos + 1)) - 1) {
    res += a[pos];
  }
  return res;
}
void upd(vector<long long int>& a, int pos, int val) {
  for (; pos < a.size(); pos = (pos | (pos + 1))) a[pos] += val;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    string a, b;
    cin >> a >> b;
    bool cant = false;
    vector<bitset<20>> changes(20);
    for (int i = 0; i < n; i++) {
      if (a[i] > b[i]) cant = true;
      if (a[i] != b[i]) changes[a[i] - 'a'].set(b[i] - 'a');
    }
    if (cant) {
      cout << -1 << "\n";
      continue;
    }
    int ans = 0;
    bitset<20> final;
    for (int i = 0; i < 20; i++) {
      for (int j = i + 1; j < 20; j++) {
        if (changes[i][j]) {
          ans++;
          for (int k = j + 1; k < 20; k++)
            changes[j][k] = max(changes[j][k], changes[i][k]);
          break;
        }
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
