#include <bits/stdc++.h>
using namespace std;
void Fast_IO() {
  ios::sync_with_stdio(0);
  cin.tie(0);
}
void dfs(vector<bool>& vis, vector<vector<long long> >& adj, long long u) {
  vis[u] = true;
  for (auto v : adj[u])
    if (!vis[v]) dfs(vis, adj, v);
}
int main() {
  Fast_IO();
  int n_testcases;
  cin >> n_testcases;
  for (int n_testcase = 1; n_testcase <= n_testcases; n_testcase++) {
    long long n;
    cin >> n;
    string s, t;
    cin >> s >> t;
    bool imp = false;
    vector<vector<long long> > adj(20);
    for (long long i = (long long)0; i <= (long long)n - 1; i++) {
      if (s[i] > t[i])
        imp = true;
      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 (imp) {
      cout << -1 << '\n';
      continue;
    }
    vector<bool> vis(20, false);
    long long cc = 0;
    for (long long i = (long long)0; i <= (long long)19; i++)
      if (!vis[i]) dfs(vis, adj, i), cc++;
    cout << 20 - cc << '\n';
  }
  return 0;
}
