#include <bits/stdc++.h>
using namespace std;
int w;
string ch(string s, char x, string b) {
  int n = s.size();
  bool cx = 0;
  char mina = 'z';
  for (int i = 0; i < n; i++) {
    if (s[i] == x && b[i] > s[i]) {
      cx = 1;
      mina = min(mina, b[i]);
    }
  }
  if (!cx) {
    return s;
  }
  w++;
  for (int i = 0; i < n; i++) {
    if (s[i] == x) s[i] = mina;
  }
  return s;
}
void solve() {
  w = 0;
  int n;
  cin >> n;
  string a, b;
  cin >> a >> b;
  for (int i = 0; i < n; i++) {
    if (b[i] < a[i]) {
      cout << "-1\n";
      return;
    }
  }
  for (int i = 0; i < 20; i++) {
    a = ch(a, ((char)i + 97), b);
  }
  cout << w << "\n";
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t) {
    t--;
    solve();
  }
}
