#include <bits/stdc++.h>
using namespace std;
long long int binary_expo(long long int base, long long int pw) {
  long long int res = 1;
  while (pw) {
    if (pw % 2) {
      res *= base;
      res %= 1000000007;
      pw -= 1;
    } else {
      base *= base;
      base %= 1000000007;
      pw /= 2;
    }
  }
  return res;
}
long long int mod_inv(long long int k) {
  return binary_expo(k, 1000000007 - 2);
}
int t;
int dfs(int node, vector<set<int>>& graph, vector<int>& vis) {
  if (vis[node] || graph[node].size() == 0) {
    return 0;
  }
  vis[node] = 1;
  int mm = 1;
  set<int>::iterator it;
  for (it = graph[node].begin(); it != graph[node].end(); it++) {
    int k = *it;
    mm = mm + dfs(k, graph, vis);
  }
  return mm;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    string a, b;
    cin >> a;
    cin >> b;
    n = a.length();
    bool poss = 1;
    vector<set<int>> pos(26);
    for (int i = 0; i < n; i++) {
      if (a[i] > b[i]) {
        poss = 0;
        break;
      }
    }
    if (!poss)
      cout << -1 << endl;
    else {
      vector<int> vis(26, 0);
      for (int i = 0; i < n; i++) {
        if (a[i] != b[i]) {
          pos[a[i] - 'a'].insert(b[i] - 'a');
          pos[b[i] - 'a'].insert(a[i] - 'a');
        }
      }
      int ans = 0;
      for (int i = 0; i < 26; i++) {
        if (!vis[i] && pos[i].size() > 0) ans += (dfs(i, pos, vis) - 1);
      }
      cout << ans << endl;
    }
  }
  return 0;
}
