#include <bits/stdc++.h>
using namespace std;
vector<int> edge[20];
int n, t;
string a, b;
bitset<20> used;
void DFS(int h) {
  used[h] = true;
  for (auto &i : edge[h])
    if (!used[i]) DFS(i);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> t;
  while (t--) {
    cin >> n;
    cin >> a >> b;
    for (auto &i : edge) i.clear();
    bool check = false;
    for (int i = 0; i <= n - 1; i++) {
      if (a[i] == b[i]) continue;
      if (a[i] > b[i]) {
        check = true;
        break;
      }
      edge[a[i] - 'a'].push_back(b[i] - 'a');
      edge[b[i] - 'a'].push_back(a[i] - 'a');
    }
    if (check) {
      cout << -1 << '\n';
      continue;
    }
    used.reset();
    int ans = 20;
    for (int i = 0; i <= 19; i++) {
      if (!used[i]) {
        DFS(i);
        ans--;
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
