#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  int n;
  while (t--) {
    string str;
    string str1;
    cin >> n;
    cin >> str;
    int val;
    cin >> str1;
    int temp;
    int mink;
    int z = 0;
    int cnt = 0;
    int arr[23] = {0};
    int i;
    for (i = 0; i < str.length(); i++) {
      if (str[i] > str1[i]) {
        cout << "-1" << endl;
        z++;
        break;
      }
      if (str[i] == str1[i]) {
        continue;
      }
      int p = str1[i] - 'a';
      while (arr[p]) {
        p = arr[p];
      }
      if (arr[str[i] - 'a'] == 0) {
        arr[str[i] - 'a'] = p;
      } else if (arr[str[i] - 'a'] > p) {
        temp = arr[str[i] - 'a'];
        arr[str[i] - 'a'] = p;
        arr[p] = temp;
      } else if (arr[str[i] - 'a'] < p) {
        val = arr[str[i] - 'a'];
        while (val < p) {
          mink = val;
          val = arr[val];
          if (val == 0) {
            break;
          }
        }
        if (val > p) {
          temp = val;
          arr[mink] = p;
          arr[p] = temp;
        } else if (val == 0) {
          arr[mink] = p;
        }
      }
    }
    for (i = 0; i < 20; i++) {
      if (arr[i] > 0) {
        cnt++;
      }
    }
    if (z == 0) {
      cout << cnt << endl;
    }
    z = 0;
    cnt = 0;
  }
}
