#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
const long long int N = 200043;
const long long int MOD = 1000000007;
long long int add(long long int x, long long int y) {
  x += y;
  while (x >= MOD) x -= MOD;
  while (x < 0) x += MOD;
  return x;
}
long long int mul(long long int x, long long int y) {
  return (x * 1ll * y) % MOD;
}
long long int binpow(long long int x, long long int y) {
  long long int z = 1;
  while (y) {
    if (y & 1) z = mul(z, x);
    x = mul(x, x);
    y >>= 1;
  }
  return z;
}
long long int inv(long long int x) { return binpow(x, MOD - 2); }
long long int divide(long long int x, long long int y) {
  return mul(x, inv(y));
}
vector<long long int> v[20];
long long int visited[20] = {0};
void dfs(long long int c) {
  visited[c] = 1;
  for (long long int x : v[c])
    if (visited[x] == 0) dfs(x);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int t, i, j;
  cin >> t;
  while (t--) {
    for (i = 0; i <= 20; i++) v[i].clear();
    long long int n;
    cin >> n;
    string str, ptr;
    cin >> str >> ptr;
    memset(visited, 0, sizeof(visited));
    long long int ans = 0;
    set<long long int> s;
    for (i = 0; i < n; i++) {
      if (ptr[i] < str[i]) {
        ans = 1;
        s.insert(str[i] - 'a');
      }
    }
    if (ans) {
      cout << -1 << endl;
      continue;
    }
    ans = 20;
    for (i = 0; i < n; i++)
      v[str[i] - 'a'].push_back(ptr[i] - 'a'),
          v[ptr[i] - 'a'].push_back(str[i] - 'a');
    for (i = 0; i < 20; i++) {
      if (visited[i] == 0) {
        ans--;
        dfs(i);
      }
    }
    cout << ans << endl;
  }
}
