#include <bits/stdc++.h>
using namespace std;
vector<long long> gr[26];
vector<bool> vis(26);
void dfs(int node) {
  vis[node] = true;
  for (auto i : gr[node]) {
    if (!vis[i]) {
      dfs(i);
    }
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    string a, b;
    cin >> a >> b;
    bool valid = true;
    for (int i = 0; i < 26; i++) {
      gr[i].clear();
      vis[i] = false;
    }
    for (int i = 0; i < n; i++) {
      if (a[i] > b[i]) {
        valid = false;
        break;
      }
    }
    if (valid) {
      for (int i = 0; i < n; i++) {
        if (a[i] != b[i]) {
          gr[int(a[i] - 'a')].push_back(int(b[i] - 'a'));
          gr[int(b[i] - 'a')].push_back(int(a[i] - 'a'));
        }
      }
      long long ans = 20;
      for (int i = 0; i < 20; i++) {
        if (!vis[i]) {
          dfs(i);
          ans--;
        }
      }
      cout << ans << endl;
    } else {
      cout << -1 << endl;
    }
  }
}
