#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1e9 + 7;
long long int power(long long int x, long long int y, long long int p) {
  long long int res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
long long int modinv(long long int k) { return power(k, MOD - 2, MOD); }
long long int fact(long long int n);
long long int nCr(long long int n, long long int k) {
  long long int C[n + 1][k + 1];
  long long int i, j;
  for (i = 0; i <= n; i++) {
    for (j = 0; j <= min(i, k); j++) {
      if (j == 0 || j == i)
        C[i][j] = 1;
      else
        C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]);
    }
  }
  return C[n][k];
}
long long int fact(long long int n) {
  long long int res = 1;
  for (long long int i = 2; i <= n; i++) res = res * i;
  return res;
}
map<char, set<char>> adj;
map<char, bool> vis;
int cnt;
void dfs(char c) {
  vis[c] = true;
  cnt++;
  for (auto i : adj[c]) {
    if (!vis[i]) {
      dfs(i);
    }
  }
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    string a, b;
    cin >> a >> b;
    set<char> s;
    int fl = 0;
    for (int i = 0; i < n; i++) {
      if (a[i] == b[i])
        continue;
      else if (a[i] > b[i]) {
        fl = 1;
        break;
      } else {
        s.insert(a[i]);
        adj[a[i]].insert(b[i]);
        adj[b[i]].insert(a[i]);
      }
    }
    if (fl) {
      cout << -1 << "\n";
      continue;
    }
    int ans = 0;
    for (auto i : s) {
      if (!vis[i]) {
        cnt = 0;
        dfs(i);
        ans += cnt - 1;
      }
    }
    cout << ans << "\n";
    adj.clear();
    vis.clear();
  }
}
