#include <bits/stdc++.h>
using namespace std;
vector<vector<long long int> > tree(20);
vector<long long int> used(20);
void dfs(long long int nd) {
  used[nd] = 1;
  for (long long int i = 0; i < (long long int)tree[nd].size(); i++) {
    long long int nxt = tree[nd][i];
    if (!used[nxt]) dfs(nxt);
  }
}
int main() {
  long long int T;
  cin >> T;
  while (T--) {
    long long int n;
    cin >> n;
    string s, t;
    cin >> s >> t;
    bool bo = true;
    tree = vector<vector<long long int> >(20);
    used = vector<long long int>(20);
    for (long long int i = 0; i < n; i++) {
      if (s[i] > t[i]) bo = false;
      tree[s[i] - 'a'].push_back(t[i] - 'a');
      tree[t[i] - 'a'].push_back(s[i] - 'a');
    }
    long long int ans = 20;
    for (long long int i = 0; i < 20; i++) {
      if (!used[i]) {
        ans--;
        dfs(i);
      }
    }
    if (bo)
      cout << ans << "\n";
    else
      cout << -1 << "\n";
  }
}
