#include <bits/stdc++.h>
using namespace std;
char a[100005], b[100005];
vector<char> v[22];
int main(void) {
  int T;
  scanf("%d", &T);
  while (T--) {
    for (int i = 0; i < 20; ++i) v[i].clear();
    int n;
    scanf("%d", &n);
    scanf("%s%s", a, b);
    bool flag = true;
    for (int i = 0; i < n; ++i) {
      if (a[i] > b[i]) {
        printf("-1\n");
        flag = false;
        break;
      }
      if (a[i] == b[i]) continue;
      int k = a[i] - 'a';
      v[k].push_back(b[i]);
    }
    if (!flag) continue;
    int cnt = 0;
    for (int i = 0; i < 20; ++i) {
      sort(v[i].begin(), v[i].end());
      int num = v[i].size();
      if (num == 0) continue;
      char y = v[i][0];
      int k = y - 'a';
      for (int j = 0; j < num; ++j) {
        if (v[i][j] != y) v[k].push_back(v[i][j]);
      }
      ++cnt;
    }
    printf("%d\n", cnt);
  }
  return 0;
}
