#include <bits/stdc++.h>
using namespace std;
const int alph = 30;
int n;
string s, t;
vector<int> vec[alph];
int solve();
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int TEST = 1;
  cin >> TEST;
  while (TEST--) solve();
  return 0;
}
int solve() {
  cin >> n >> s >> t;
  for (int i = 0; i < alph; i++) vec[i].clear();
  for (int i = 0; i < n; i++) {
    if (s[i] > t[i]) return cout << "-1\n", 0;
    if (s[i] == t[i]) continue;
    vec[s[i] - 'a'].push_back(t[i] - 'a');
  }
  int ans = 0;
  for (int i = 0; i < alph; i++) {
    sort(vec[i].begin(), vec[i].end());
    vec[i].resize(unique(vec[i].begin(), vec[i].end()) - vec[i].begin());
    if (vec[i].size()) {
      int val = vec[i][0];
      int st = 1;
      if (val == i) {
        if (vec[i].size() > 1) val = vec[i][1];
        st = 2;
      }
      for (int j = st; j < vec[i].size(); j++)
        vec[vec[i][0]].push_back(vec[i][j]);
      ans++;
    }
  }
  cout << ans << '\n';
  return 0;
}
