#include <bits/stdc++.h>
using namespace std;
vector<int> v[30];
bool visited[30];
int grp[30];
int group = 0;
long long bin(long long a, long long b, long long mod) {
  if (b == 0) return 1;
  if (b % 2 == 0) return bin((a * a) % mod, b / 2, mod) % mod;
  return ((a % mod) * (bin((a * a) % mod, b / 2, mod) % mod)) % mod;
}
void dfs(int i) {
  visited[i] = true;
  for (auto j : v[i]) {
    if (visited[j] == false) {
      dfs(j);
    }
  }
}
void solve() {
  for (int i = 0; i < 30; i++) {
    v[i].clear();
    visited[i] = false;
    grp[i] = 0;
  }
  group = 0;
  int n;
  cin >> n;
  string s1, s2;
  cin >> s1 >> s2;
  for (int i = 0; i < n; i++) {
    if (s1[i] > s2[i]) {
      cout << "-1\n";
      return;
    }
    if (s1[i] == s2[i]) {
      continue;
    }
    v[s1[i] - 'a'].push_back(s2[i] - 'a');
    v[s2[i] - 'a'].push_back(s1[i] - 'a');
    grp[s1[i] - 'a'] = 1;
    grp[s2[i] - 'a'] = 1;
  }
  for (int i = 0; i < 26; i++) {
    if (visited[i] == false && grp[i] == 1) {
      dfs(i);
      group++;
    }
  }
  int sum = 0;
  for (int i = 0; i < 26; i++) {
    sum += grp[i];
  }
  cout << sum - group << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  int t;
  t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
