#include <bits/stdc++.h>
using namespace std;
const int N = 26 + 9, line = 1000000;
const long long mod = 1e9 + 7;
vector<int> adj[N];
int par[N];
int find_leader(int u) {
  if (par[u] == u)
    return u;
  else
    return par[u] = find_leader(par[u]);
}
void test() {
  int n;
  string a, b;
  cin >> n >> a >> b;
  for (int i = 0; i < N; i++) adj[i].clear(), par[i] = i;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    int u = a[i] - 'a', v = b[i] - 'a';
    if (u > v) ans = -1;
    adj[u].push_back(v);
  }
  for (int i = 0; i < N; i++) sort(adj[i].begin(), adj[i].end());
  if (ans == -1)
    printf("-1\n");
  else {
    for (int i = 25; i >= 0; i--) {
      for (auto child : adj[i]) {
        if (find_leader(i) != find_leader(child)) {
          par[find_leader(i)] = par[find_leader(child)];
          ans++;
        }
      }
    }
    printf("%d\n", ans);
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t = 1;
  cin >> t;
  for (int cas = 1; cas <= t; cas++) {
    test();
  }
}
