#include <bits/stdc++.h>
using namespace std;
int t, n;
string a, b;
int main() {
  cin >> t;
  while (t--) {
    cin >> n >> a >> b;
    bool ok = true;
    for (int i = 0; i < n; i++) {
      if (a[i] > b[i]) {
        ok = false;
        break;
      }
    }
    if (!ok)
      cout << "-1\n";
    else {
      int cnt = 0;
      for (int c = (int)'a'; c <= (int)'t'; c++) {
        bool need = false;
        set<int> ids = set<int>();
        char minc = 'z';
        for (int i = 0; i < n; i++) {
          if (a[i] < b[i] && a[i] == c) {
            need = true;
            ids.insert(i);
            minc = min(minc, b[i]);
          }
        }
        if (need) {
          cnt++;
          for (auto id : ids) {
            a[id] = minc;
          }
        }
      }
      cout << cnt << '\n';
    }
  }
}
