#include <bits/stdc++.h>
const int MaxN = 1e5 + 5;
using namespace std;
long long int t, n, m, k, ans = 0;
string a, b;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  cin >> t;
  while (t--) {
    cin >> n;
    cin >> a >> b;
    set<char> st[20];
    int flag = 0;
    for (int i = 0; i < n; i++) {
      if (b[i] > a[i]) {
        st[a[i] - 'a'].insert(b[i]);
      }
      if (b[i] < a[i]) {
        flag = 1;
        break;
      }
    }
    if (flag) {
      cout << -1 << "\n";
      continue;
    }
    ans = 0;
    for (int i = 0; i < 20; i++) {
      char firstchr;
      if (st[i].size() == 0) {
        continue;
      }
      firstchr = *st[i].begin();
      st[i].erase(firstchr);
      ans++;
      for (auto x : st[i]) {
        st[firstchr - 'a'].insert(x);
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
