#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("avx,avx2,fma")
using namespace std;
long long power(long long a, long long b) {
  long long res = 1;
  a = a % 998244353;
  while (b > 0) {
    if (b & 1) {
      res = (res * a) % 998244353;
    }
    a = (a * a) % 998244353;
    b >>= 1;
  }
  return res;
}
long long fermat_inv(long long y) { return power(y, 998244353 - 2); }
long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); }
const long long dx[] = {-1, 0, 1, 0, -1, 1, 1, -1};
const long long dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
void solve() {
  long long n;
  cin >> n;
  string a, b;
  cin >> a >> b;
  for (long long i = 0; i < n; i++) {
    if (b[i] < a[i]) {
      cout << "-1\n";
      return;
    }
  }
  long long ans = 0;
  for (char aa = 'a'; aa <= 't'; aa++) {
    set<char> s;
    for (long long i = 0; i < n; i++) {
      if (b[i] == aa && a[i] != aa) {
        s.insert(a[i]);
        a[i] = b[i];
      }
    }
    for (long long i = 0; i < n; i++) {
      if (s.find(a[i]) != s.end() && aa < b[i]) a[i] = aa;
    }
    ans += (long long)s.size();
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long T = 1;
  cin >> T;
  while (T--) {
    solve();
  }
  return 0;
}
