#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 1;
int f[26];
int find(int x) { return (f[x] == x ? x : f[x] = find(f[x])); }
void solve() {
  int n;
  string a, b;
  cin >> n;
  cin >> a;
  cin >> b;
  for (int i = 0; i <= 25; ++i) {
    f[i] = i;
  }
  int dem = 0;
  for (int i = 0; i < n; ++i) {
    if (a[i] > b[i]) {
      cout << "-1" << endl;
      return;
    } else {
      if (a[i] < b[i]) {
        int x = find(a[i] - 'a'), y = find(b[i] - 'a');
        if (x != y) {
          ++dem;
          f[x] = y;
        }
      }
    }
  }
  cout << dem << endl;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
}
