#include <bits/stdc++.h>
using namespace std;
const int MAXN = 102020;
char A[MAXN], B[MAXN];
void solve() {
  int n, ans = 0;
  scanf("%d%s%s", &n, A, B);
  set<int> to[26];
  for (int i = 0; i < n; ++i)
    if (A[i] != B[i]) {
      if (B[i] < A[i]) {
        puts("-1");
        return;
      }
      to[A[i] - 'a'].insert(B[i] - 'a');
    }
  for (int i = 0; i < 26; ++i)
    if (to[i].size()) {
      int z = *to[i].begin();
      for (auto g : to[i])
        if (g != z) to[z].insert(g);
      ans++;
    }
  printf("%d\n", ans);
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) solve();
  return 0;
}
