#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long num = 0, neg = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-') neg = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    num = (num << 3) + (num << 1) + c - '0';
    c = getchar();
  }
  return num * neg;
}
int T, n, fail, ans;
char a[100010], b[100010];
int main() {
  T = read();
  while (T--) {
    n = read(), fail = ans = 0;
    vector<int> pos[20];
    scanf("%s", a + 1);
    scanf("%s", b + 1);
    for (int i = 1; i <= n; i++) pos[a[i] - 'a'].push_back(i);
    for (int i = 0; i < 20; i++) {
      int m = pos[i].size(), to = 20;
      for (int j = 0; j < m; j++) {
        if (a[pos[i][j]] > b[pos[i][j]]) fail = 1;
        if (a[pos[i][j]] < b[pos[i][j]]) to = min(to, b[pos[i][j]] - 'a');
      }
      if (to != 20) ans++;
      for (int j = 0; j < m; j++) {
        if (a[pos[i][j]] < b[pos[i][j]])
          a[pos[i][j]] = to + 'a', pos[to].push_back(pos[i][j]);
      }
    }
    if (fail)
      printf("-1\n");
    else
      printf("%d\n", ans);
  }
  return 0;
}
