#include <bits/stdc++.h>
using namespace std;
struct pair_hash {
  template <class T1, class T2>
  size_t operator()(const pair<T1, T2> &p) const {
    auto h1 = hash<T1>{}(p.first);
    auto h2 = hash<T2>{}(p.second);
    return h1 ^ h2;
  }
};
using Vote = pair<long long int, long long int>;
using Unordered_map = unordered_map<Vote, int, pair_hash>;
const int mod = 998244353;
const int mod2 = 1000000007;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    string s1, s2;
    cin >> s1 >> s2;
    int count = 0;
    int ok = 1;
    for (long long int i = 0; i < n; i++) {
      if (s1[i] > s2[i]) {
        ok = 0;
        break;
      }
    }
    if (!ok) {
      cout << "-1\n";
      continue;
    }
    multiset<pair<char, char>> st;
    for (long long int i = 0; i < n; i++)
      if (s1[i] != s2[i]) st.insert({s1[i], s2[i]});
    while (!st.empty()) {
      pair<char, char> p = *st.begin();
      while (!st.empty() && *st.begin() == p) st.erase(st.begin());
      while (!st.empty() && st.begin()->first == p.first) {
        st.insert({p.second, st.begin()->second});
        st.erase(st.begin());
      }
      count++;
    }
    cout << count << '\n';
  }
}
