#include <bits/stdc++.h>
using namespace std;
int const N = 2e5 + 5, M = 2e5 + 5, MOD = 1e9 + 7, OO = 0x3f3f3f3f, p = 1e4,
          X = 53;
long double const ESP = 1e-11;
int t, n;
string a, b;
vector<vector<int>> adj;
vector<bool> vis;
void DFS(int u) {
  vis[u] = 1;
  for (int v : adj[u])
    if (!vis[v]) DFS(v);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> t;
  while (t--) {
    cin >> n >> a >> b;
    adj = vector<vector<int>>(20);
    vis = vector<bool>(20, 0);
    bool ans = 1;
    for (int i = 0; i < n && ans; i++) {
      if (a[i] < b[i]) {
        adj[a[i] - 'a'].push_back(b[i] - 'a');
        adj[b[i] - 'a'].push_back(a[i] - 'a');
      } else if (a[i] > b[i])
        ans = 0;
    }
    if (!ans) {
      cout << "-1\n";
      continue;
    }
    int res = 20;
    for (int i = 0; i < 20; i++) {
      if (!vis[i]) {
        DFS(i);
        res--;
      }
    }
    cout << res << '\n';
  }
  return 0;
}
