#include <bits/stdc++.h>
using namespace std;
const int maxn = 1010;
char a[200001], b[200001];
int mp[maxn];
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    bool flag = 1;
    scanf("%s", a);
    scanf("%s", b);
    for (int i = 0; i < n; i++) {
      if (a[i] > b[i]) {
        flag = 0;
        break;
      }
    }
    if (flag == 0) {
      cout << "-1" << endl;
      continue;
    }
    char x = 'a';
    int ans = 0;
    for (char x = 'a'; x <= 't'; x++) {
      queue<int> q;
      char y = 'u';
      for (int i = 0; i < n; i++) {
        if (a[i] == x && a[i] != b[i]) {
          q.push(i);
          if (b[i] < y) y = b[i];
        }
      }
      if (!q.empty()) ans++;
      while (!q.empty()) {
        a[q.front()] = y;
        q.pop();
      }
    }
    cout << ans << endl;
  }
}
