#include <bits/stdc++.h>
using namespace std;
const long long int N = 100005;
struct dsu {
  vector<long long int> par, sz;
  long long int comp;
  void init(long long int n) {
    par.resize(n);
    sz.resize(n, 1);
    for (long long int i = 0; i < n; i++) par[i] = i;
    comp = n;
  }
  long long int get_par(long long int x) {
    if (par[x] == x) return x;
    return par[x] = get_par(par[x]);
  }
  bool unite(long long int x, long long int y) {
    long long int super_x = get_par(x);
    long long int super_y = get_par(y);
    if (super_x != super_y) {
      par[super_x] = super_y;
      sz[super_y] += sz[super_x];
      sz[super_x] = 0;
      comp--;
      return true;
    }
    return false;
  }
} dsu;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int t;
  cin >> t;
  while (t--) {
    long long int i, j, k, n, m, ans = 0, cnt = 0, sum = 0;
    cin >> n;
    dsu.init(20);
    string a, b;
    cin >> a >> b;
    bool fl = 1;
    for (i = 0; i < n; i++) {
      if (a[i] > b[i]) {
        fl = 0;
        break;
      }
      dsu.unite(a[i] - 'a', b[i] - 'a');
    }
    if (!fl) {
      cout << -1 << '\n';
      continue;
    }
    ans = 20 - dsu.comp;
    cout << ans << '\n';
  }
  return 0;
}
