#include <bits/stdc++.h>
using namespace std;
void dfs(vector<long long> adj[], vector<long long> &vis, long long i) {
  vis[i] = 1;
  for (long long j = 0; j < adj[i].size(); j++) {
    if (!vis[adj[i][j]]) {
      dfs(adj, vis, adj[i][j]);
    }
  }
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long t = 1;
  cin >> t;
  while (t--) {
    long long n, res = 20;
    cin >> n;
    string s, t;
    cin >> s >> t;
    vector<long long> adj[20];
    for (long long i = 0; i < n; i++) {
      if (s[i] > t[i]) {
        res = -1;
        break;
      } else if (s[i] < t[i]) {
        adj[s[i] - 'a'].push_back(t[i] - 'a');
        adj[t[i] - 'a'].push_back(s[i] - 'a');
      }
    }
    if (res == -1) {
      cout << -1 << "\n";
      continue;
    }
    vector<long long> vis(20, 0);
    for (long long i = 0; i < 20; i++) {
      if (!vis[i]) {
        dfs(adj, vis, i);
        res--;
      }
    }
    cout << res << "\n";
  }
  return 0;
}
