#include <bits/stdc++.h>
using namespace std;
const int N = 20;
vector<int> g[N];
bool vis[N];
void dfs(int i) {
  vis[i] = 1;
  for (int x : g[i]) {
    if (!vis[x]) dfs(x);
  }
}
void solve() {
  int n;
  cin >> n;
  string s, t;
  cin >> s >> t;
  bool ok = 1;
  memset(vis, 0, sizeof(vis));
  for (int i = 0; i < N; ++i) g[i].clear();
  for (int i = 0; i < n; ++i) {
    int c1 = s[i] - 'a';
    int c2 = t[i] - 'a';
    if (c1 > c2) ok = 0;
    g[c1].push_back(c2);
    g[c2].push_back(c1);
  }
  if (!ok) {
    cout << -1 << "\n";
    return;
  }
  int cnt = 0;
  for (int i = 0; i < 20; ++i) {
    if (!vis[i]) {
      cnt++;
      dfs(i);
    }
  }
  cout << 20 - cnt << "\n";
}
int32_t main() {
  int tt;
  cin >> tt;
  while (tt--) solve();
  return 0;
}
