#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
int n;
string a, b;
map<pair<char, char>, int> cnt;
void solve() {
  cin >> n;
  cin >> a >> b;
  cnt.clear();
  for (int i = 0; i < n; ++i) {
    if (a[i] > b[i]) {
      cout << -1 << "\n";
      return;
    }
    if (a[i] < b[i]) ++cnt[{a[i], b[i]}];
  }
  vector<pair<char, char>> res;
  for (auto it : cnt) res.emplace_back(it.first.first, it.first.second);
  long long ans = 0;
  while (!res.empty()) {
    char go = res[0].first, to = res[0].second;
    ++ans;
    vector<pair<char, char>> new_res;
    for (int i = 0; i < res.size(); ++i)
      if (res[i].first != go || res[i].second != to) new_res.push_back(res[i]);
    for (int i = 0; i < new_res.size(); ++i)
      if (new_res[i].first == go) new_res[i].first = to;
    res = new_res;
    sort(res.begin(), res.end());
  }
  cout << ans << "\n";
}
int main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  cin >> t;
  for (int i = 1; i <= t; ++i) {
    solve();
  }
  return 0;
}
