#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  vector<char> v[22];
  string s, t;
  cin >> n >> s >> t;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] > t[i]) {
      cout << -1 << endl;
      return;
    }
    v[s[i] - 'a'].push_back(t[i]);
  }
  int cnt = 0;
  for (int i = 0; i < 22; i++) {
    char mx = 'z';
    for (char ch : v[i]) {
      if (ch - 'a' > i) {
        mx = min(mx, ch);
      }
    }
    if (mx == 'z') continue;
    ++cnt;
    for (char ch : v[i]) {
      if (ch - 'a' > i) {
        v[mx - 'a'].push_back(ch);
      }
    }
  }
  cout << cnt << endl;
}
int main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int test, tc = 0;
  cin >> test;
  while (test--) {
    solve();
  }
}
