#include <bits/stdc++.h>
using namespace std;
void dfs(long long int i, vector<set<long long int>>& gra,
         long long int vis[]) {
  vis[i] = 1;
  for (auto j : gra[i]) {
    if (!vis[j]) dfs(j, gra, vis);
  }
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int t;
  cin >> t;
  while (t--) {
    long long int n;
    cin >> n;
    string a, b;
    cin >> a >> b;
    vector<set<long long int>> gra(27);
    long long int vis[27] = {0}, flag = 0;
    for (long long int i = 0; i < n; i++) {
      if (b[i] < a[i]) {
        flag = 1;
        break;
      }
      if (a[i] != b[i]) gra[a[i] - 'a'].insert(b[i] - 'a');
      gra[b[i] - 'a'].insert(a[i] - 'a');
    }
    if (flag == 1) {
      cout << -1 << "\n";
      continue;
    }
    long long int ans = 20;
    for (long long int i = 0; i < 20; i++) {
      if (!vis[i]) {
        dfs(i, gra, vis);
        ans--;
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
