#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    string a, b;
    cin >> a >> b;
    int arr[n];
    int brr[n];
    int flag = 1;
    for (int i = 0; i < n; i++) {
      int k1, k2;
      k1 = (int)a[i] - (int)'a' + 1;
      k2 = (int)b[i] - (int)'a' + 1;
      if (k1 > k2) flag = 0;
      arr[i] = k1;
      brr[i] = k2;
    }
    if (flag == 0)
      cout << "-1" << endl;
    else {
      int ans = 0;
      for (int i = 1; i <= 19; i++) {
        int temp = 100;
        for (int j = 0; j < n; j++) {
          if (arr[j] != i) continue;
          if (brr[j] > i) {
            temp = min(temp, brr[j]);
          }
        }
        if (temp != 100) {
          ans++;
          for (int j = 0; j < n; j++) {
            if (arr[j] == i) arr[j] = temp;
          }
        }
      }
      cout << ans << endl;
    }
  }
  return 0;
}
