#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-8;
const int INF = 0x3F3F3F3F;
const int64_t LINF = 4611686018427387903;
const int MOD = 1e9 + 7;
void solve() {
  string a, b;
  int n;
  cin >> n >> a >> b;
  set<pair<char, char>> has;
  for (int i = 0; i < n; i++) {
    if (a[i] > b[i]) {
      cout << "-1\n";
      return;
    }
    if (a[i] != b[i]) has.insert({a[i], b[i]});
  }
  int ans = 0;
  while (has.size()) {
    auto x = *has.begin();
    while (has.size() && has.begin()->first == x.first) {
      char c = has.begin()->second;
      if (c != x.second) has.insert({x.second, c});
      has.erase(has.begin());
    }
    ans++;
  }
  cout << ans << '\n';
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
