#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int dp[26][26];
    memset(dp, 0, sizeof(dp));
    int n, flag = 0;
    cin >> n;
    string a, b;
    cin >> a >> b;
    for (int i = 0; i < n; i++) {
      if (b[i] < a[i]) {
        cout << -1 << "\n";
        flag = 1;
        break;
      } else {
        if (a[i] != b[i]) dp[a[i] - 'a'][b[i] - 'a']++;
      }
    }
    if (!flag) {
      int ct = 0;
      for (int i = 0; i < 26; i++) {
        for (int j = 0; j < 26; j++) {
          if (dp[i][j] != 0) {
            ct++;
            dp[i][j] = 0;
            for (int k = j + 1; k < 26; k++) {
              dp[j][k] += dp[i][k];
              dp[i][k] = 0;
            }
            break;
          }
        }
      }
      cout << ct << "\n";
    }
  }
}
