#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
string s, t;
int q, n, ans, par_sz[N];
vector<int> adj[30];
int get_par(int u) { return par_sz[u] >= 0 ? get_par(par_sz[u]) : u; }
bool merge(int u, int v) {
  u = get_par(u), v = get_par(v);
  if (u == v) return false;
  if (par_sz[u] > par_sz[v]) swap(u, v);
  par_sz[u] += par_sz[v];
  par_sz[v] = u;
  return true;
}
int find_mn(string s, string t) {
  int res = 0;
  for (int i = 0; i < n; i++) adj[s[i] - 'a'].push_back(i);
  for (int i = 0; i < 26; i++)
    if (adj[i].size())
      for (auto u : adj[i]) res += merge(t[u] - 'a', i);
  return res;
}
bool check(string s, string t) {
  for (int i = 0; i < n; i++)
    if (s[i] > t[i]) return false;
  return true;
}
void input() { cin >> n >> s >> t; }
void solve() {
  if (!check(s, t)) {
    ans = -1;
    return;
  }
  ans = find_mn(s, t);
}
void output() { cout << ans << endl; }
void clear() {
  for (int i = 0; i < 26; i++) par_sz[i] = -1, adj[i].clear();
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  memset(par_sz, -1, sizeof par_sz);
  cin >> q;
  while (q--) input(), solve(), output(), clear();
  return 0;
}
