#include <bits/stdc++.h>
using namespace std;
struct fast_ios {
  fast_ios() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  };
} fast_ios_;
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << '[';
  for (auto v : vec) os << v << ',';
  os << ']';
  return os;
}
int solve() {
  int N;
  cin >> N;
  string A, B;
  cin >> A >> B;
  vector<vector<int>> cnt(20, vector<int>(20));
  for (int i = (0), i_end_ = (N); i < i_end_; i++)
    cnt[A[i] - 'a'][B[i] - 'a']++;
  int ret = 0;
  for (int i = (0), i_end_ = (20); i < i_end_; i++) {
    int t = -1;
    for (int j = (0), j_end_ = (20); j < j_end_; j++)
      if (cnt[i][j] and j != i) {
        t = j;
        break;
      }
    if (t < 0) continue;
    if (t < i) return -1;
    ret++;
    for (int j = (i + 1), j_end_ = (20); j < j_end_; j++)
      cnt[t][j] += cnt[i][j];
  }
  return ret;
}
int main() {
  int T;
  cin >> T;
  while (T--) cout << solve() << '\n';
}
