#include <bits/stdc++.h>
using namespace std;
int t, n, mp[21][21];
char a[100010], b[100010];
int main() {
  cin >> t;
  while (t--) {
    cin >> n;
    scanf("%s%s", a + 1, b + 1);
    int flg = 0;
    memset(mp, 0, sizeof(mp));
    for (int i = 1; i <= n; i++) {
      if (a[i] == b[i]) {
        continue;
      }
      mp[a[i] - 'a'][b[i] - 'a']++;
      if (a[i] > b[i]) {
        flg = 1;
        cout << "-1\n";
        break;
      }
    }
    if (flg) {
      continue;
    }
    int ans = 0;
    for (int i = 1; i < 20; i++) {
      for (int j = 0; j < i; j++) {
        if (mp[j][i] > 0) {
          ans++;
          for (int k = i + 1; k < 20; k++) {
            mp[i][k] += mp[j][k];
            mp[j][k] = 0;
          }
          mp[j][i] = 0;
        }
      }
    }
    cout << ans << "\n";
  }
}
