#include <bits/stdc++.h>
#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3, "Ofast", "inline")
using namespace std;
const int maxn = 2e6 + 7;
const int INF = 1e9;
const long long INFF = 1e18;
char s1[maxn], s2[maxn];
int t, n;
set<int> S[30];
int main() {
  scanf("%d", &t);
  while (t--) {
    for (int i = 0; i <= 28; i++) S[i].clear();
    bool mark = true;
    int ans = 0;
    scanf("%d", &n);
    scanf("%s", s1);
    scanf("%s", s2);
    for (int i = 0; i <= n - 1; i++) {
      if (s1[i] == s2[i])
        continue;
      else if (s1[i] > s2[i])
        mark = false;
      else if (S[s1[i] - 'a'].find(s2[i] - 'a') == S[s1[i] - 'a'].end())
        S[s1[i] - 'a'].insert(s2[i] - 'a');
    }
    for (int i = 0; i <= 25; i++) {
      if (S[i].size() == 0) continue;
      set<int>::iterator it;
      ans++;
      int x = *S[i].begin();
      for (it = S[i].begin(); it != S[i].end(); it++) {
        if (it == S[i].begin()) continue;
        if (S[x].find(*it) == S[x].end()) S[x].insert(*it);
      }
    }
    if (!mark)
      printf("%d\n", -1);
    else
      printf("%d\n", ans);
  }
  return 0;
}
