#include <bits/stdc++.h>
using namespace std;
void dfs(vector<int> adj[], int st, int &len, int vis[]) {
  vis[st] = 1;
  len++;
  for (auto i : adj[st]) {
    if (vis[i] == 0) {
      dfs(adj, i, len, vis);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n, f = 1;
    cin >> n;
    string a, b;
    cin >> a >> b;
    vector<int> adj[20];
    for (int i = 0; i < n; i++) {
      if (a[i] > b[i]) {
        f = 0;
        break;
      }
      if (a[i] == b[i]) continue;
      adj[a[i] - 'a'].push_back(b[i] - 'a');
      adj[b[i] - 'a'].push_back(a[i] - 'a');
    }
    if (f == 0) {
      cout << "-1\n";
      continue;
    }
    int vis[20] = {0};
    int ans = 0;
    for (int i = 0; i < 20; i++) {
      if (vis[i] == 0) {
        int len = 0;
        dfs(adj, i, len, vis);
        ans += (len - 1);
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
