#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 10, MN = 1e5 + 10;
int n;
string A, B;
bool mk[20];
bool adj[20][20];
int dfs(int s) {
  int ret = 1;
  mk[s] = true;
  for (int i = 0; i < 20; i++)
    if (adj[s][i] && !mk[i]) ret += dfs(i);
  return ret;
}
int main() {
  ios_base ::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  int Tst;
  cin >> Tst;
  while (Tst--) {
    cin >> n;
    cin >> A >> B;
    memset(adj, 0, sizeof adj);
    bool fail = false;
    for (int i = 0; i < n; i++) {
      if (A[i] > B[i]) fail = true;
      adj[A[i] - 'a'][B[i] - 'a'] = true;
      adj[B[i] - 'a'][A[i] - 'a'] = true;
    }
    if (fail) {
      cout << -1 << '\n';
      continue;
    }
    int ans = 0;
    memset(mk, 0, sizeof mk);
    for (int i = 0; i < 20; i++) {
      bool has = false;
      for (int j = 0; j < 20; j++)
        if (i != j && adj[i][j]) has = true;
      if (has && !mk[i]) ans += dfs(i) - 1;
    }
    cout << ans << endl;
  }
  return 0;
}
