#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
char a[N], b[N];
bool vis[20], in[20];
int n, mp[20][20];
int dfs(int x) {
  vis[x] = true;
  int ret = 1;
  for (int y = 0; y < 20; y++)
    if (!vis[y] && mp[x][y]) ret += dfs(y);
  return ret;
}
int main() {
  int T;
  cin >> T;
  while (T--) {
    cin >> n >> a + 1 >> b + 1;
    memset(in, 0, sizeof in);
    memset(mp, 0, sizeof mp);
    bool flag = false;
    for (int i = 1; i <= n; i++)
      if (a[i] != b[i]) {
        in[a[i] - 'a'] = in[b[i] - 'a'] = true;
        mp[a[i] - 'a'][b[i] - 'a'] = mp[b[i] - 'a'][a[i] - 'a'] = 1;
        if (a[i] > b[i]) flag = true;
      }
    if (flag)
      cout << -1 << endl;
    else {
      memset(vis, 0, sizeof vis);
      int ans = 0;
      for (int i = 0; i < 20; i++)
        if (in[i] && !vis[i]) ans += dfs(i) - 1;
      cout << ans << endl;
    }
  }
  return 0;
}
