#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n;
  cin >> n;
  string a, b;
  cin >> a >> b;
  for (int i = 0; i < n; ++i) {
    if (a[i] > b[i]) {
      cout << -1 << '\n';
      return;
    }
  }
  const long long m = 20;
  vector<vector<long long>> counts(m, vector<long long>(m));
  for (int i = 0; i < n; ++i) {
    counts[a[i] - 'a'][b[i] - 'a']++;
  }
  vector<bool> vis(m);
  long long ans = 0;
  for (int fr = 0; fr < m; ++fr) {
    if (vis[fr]) {
      continue;
    }
    queue<int> q;
    q.push(fr);
    vis[fr] = true;
    int size = 0;
    while (not q.empty()) {
      int pos = q.front();
      q.pop();
      ans++;
      for (int next = 0; next < m; ++next) {
        if (next == pos) {
          continue;
        }
        if (!vis[next] && (counts[min(pos, next)][max(pos, next)] > 0)) {
          q.push(next);
          vis[next] = true;
        }
      }
    }
    ans += size - 1;
  }
  cout << ans << '\n';
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long t;
  cin >> t;
  for (int i = 0; i < t; ++i) {
    solve();
  }
  return 0;
}
