#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    string first, sec;
    cin >> first >> sec;
    long long arr[35][35];
    memset(arr, 0, sizeof(arr));
    long long flag = 0;
    for (long long i = 0; i < n; ++i) {
      if (first[i] > sec[i]) {
        flag = 1;
      }
      ++arr[first[i] - 'a'][sec[i] - 'a'];
    }
    if (flag == 1) {
      cout << -1 << endl;
      continue;
    }
    long long ans = 0;
    for (long long i = 0; i < 20; ++i) {
      for (long long j = i + 1; j < 20; ++j) {
        if (arr[i][j] != 0) {
          ++ans;
          for (long long k = j + 1; k < 20; ++k) {
            arr[j][k] += arr[i][k];
            arr[i][k] = 0;
          }
        }
      }
    }
    cout << ans << endl;
  }
}
