#include <bits/stdc++.h>
using namespace std;
const int N = 2e7;
const long long inf = 1e18;
const long long mod = 1e9 + 7;
long long mypower(long long x, long long y) {
  if (y == 0) return 1;
  if (y == 1) return x;
  long long ret = mypower(x, y / 2);
  ret = (ret * ret) % mod;
  if (y % 2) ret = (ret * x) % mod;
  return ret;
}
long long t;
string a, b;
vector<long long> v[200];
bool vis[200];
void dfs(long long x) {
  if (vis[x]) return;
  vis[x] = 1;
  for (int i = 0; i < v[x].size(); i++) {
    if (!vis[v[x][i]]) {
      dfs(v[x][i]);
    }
  }
  return;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> t;
  while (t--) {
    bool is = 0;
    long long n;
    cin >> n;
    cin >> a >> b;
    for (int i = 0; i < 20; i++) {
      v[i].clear();
    }
    for (int i = 0; i < n; i++) {
      v[a[i] - 'a'].push_back(b[i] - 'a');
      v[b[i] - 'a'].push_back(a[i] - 'a');
      if (a[i] > b[i]) is = 1;
    }
    if (is) {
      cout << -1 << endl;
      continue;
    }
    for (int i = 0; i < 20; i++) {
      vis[i] = 0;
    }
    long long ans = 20;
    for (int i = 0; i < 20; i++) {
      if (!vis[i]) {
        dfs(i);
        ans--;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
