#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5;
char a[MAX + 5], b[MAX + 5];
bool num[26][26];
int pre[26];
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    int N;
    scanf("%d", &N);
    getchar();
    for (int i = 1; i <= N; i++) scanf("%c", a + i);
    getchar();
    for (int i = 1; i <= N; i++) scanf("%c", b + i);
    bool _ok = true;
    for (int i = 1; i <= N; i++) {
      if (b[i] < a[i]) {
        _ok = false;
        break;
      }
    }
    if (!_ok)
      printf("-1\n");
    else {
      for (int i = 0; i < 26; i++) {
        for (int j = 0; j < 26; j++) num[i][j] = false;
        pre[i] = i;
      }
      for (int i = 1; i <= N; i++) num[b[i] - 'a'][a[i] - 'a'] = true;
      int ans = 0;
      for (int i = 0; i < 26; i++) {
        for (int j = 0; j < i; j++) {
          if (num[i][j]) {
            if (pre[j] != j)
              num[i][pre[j]] = true;
            else
              ans++;
            pre[j] = i;
          }
        }
      }
      printf("%d\n", ans);
    }
  }
  return 0;
}
