#include <bits/stdc++.h>
using namespace std;
const long long mx = 20;
vector<long long> G[mx];
vector<bool> vis(mx);
void dfs(long long u) {
  vis[u] = 1;
  for (auto it : G[u]) {
    if (!vis[it]) dfs(it);
  }
}
int main() {
  long long t;
  cin >> t;
  while (t--) {
    for (int i = 0; i < mx; i++) G[i].clear(), vis[i] = 0;
    long long n;
    string a, b;
    cin >> n >> a >> b;
    long long F = 0;
    for (int i = 0; i < n; i++) {
      if (a[i] != b[i]) {
        if (a[i] > b[i]) {
          F = 1;
          cout << -1 << "\n";
          break;
        }
        G[a[i] - 'a'].push_back(b[i] - 'a');
        G[b[i] - 'a'].push_back(a[i] - 'a');
      }
    }
    if (F) continue;
    long long ans = 0;
    for (int i = 0; i < mx; i++) {
      if (!vis[i]) {
        dfs(i);
        ans++;
      }
    }
    cout << mx - ans << "\n";
  }
}
