#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> g;
vector<bool> vis;
void dfs(int x) {
  vis[x] = 1;
  for (int y : g[x])
    if (!vis[y]) dfs(y);
  return;
}
void solve() {
  int n;
  string a, b;
  cin >> n >> a >> b;
  g = vector<vector<int>>(20);
  vis = vector<bool>(20, 0);
  for (int i = 0; i < n; i++) {
    if (a[i] != b[i]) {
      if (a[i] > b[i]) {
        cout << "-1\n";
        return;
      }
      g[a[i] - 'a'].push_back(b[i] - 'a');
      g[b[i] - 'a'].push_back(a[i] - 'a');
    }
  }
  int ans = 20;
  for (int i = 0; i < 20; i++)
    if (!vis[i]) {
      dfs(i);
      ans--;
    }
  cout << ans << '\n';
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
