#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> v;
vector<bool> vis;
void dfs(int i, int &c) {
  vis[i] = true, c++;
  for (int j : v[i]) {
    if (!vis[j]) {
      dfs(j, c);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int T;
  cin >> T;
  while (T--) {
    int n;
    cin >> n;
    v.clear(), vis.clear();
    v.resize(20), vis.resize(20);
    string s, t;
    cin >> s >> t;
    bool poss = true;
    for (int i = 0; i < n; ++i) {
      if (s[i] > t[i]) {
        poss = false;
        break;
      }
      v[s[i] - 'a'].push_back(t[i] - 'a'), v[t[i] - 'a'].push_back(s[i] - 'a');
    }
    if (!poss) {
      cout << "-1\n";
      continue;
    }
    int cnt = 0;
    for (int i = 0; i < 20; ++i) {
      if (!vis[i]) {
        int c = 0;
        dfs(i, c);
        cnt += c - 1;
      }
    }
    cout << cnt << "\n";
  }
  return 0;
}
