#include <bits/stdc++.h>
using namespace std;
void FAST() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
const long long md = 1000000007;
const long long inf = 1e18L + 5;
const char nl = '\n';
const int N = 200005;
bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {
  return (a.second < b.second);
}
int main() {
  FAST();
  int tt{}, T{};
  tt = 1;
  cin >> tt;
  T = tt;
  while (tt--) {
    string str, sa, s, t, sb;
    int n{}, m{}, ans{}, k{}, a{}, b{}, c{}, x{}, y{}, f{}, q{};
    cin >> n;
    cin >> s >> t;
    vector<std::pair<int, int> > v;
    vector<std::vector<int> > w(21);
    for (int i = 0; i < n; ++i) {
      if (s[i] > t[i]) {
        f = 1;
        break;
      }
      v.push_back({s[i] - 'a' + 1, t[i] - 'a' + 1});
      if (v[i].second > v[i].first) w[v[i].first].push_back(v[i].second);
    }
    if (f) {
      cout << "-1\n";
      continue;
    }
    for (int i = 1; i < 21; ++i) {
      if (w[i].size()) {
        sort(w[i].begin(), w[i].end());
        a = lower_bound(w[i].begin(), w[i].end(), i + 1) - w[i].begin();
        if (a > 0) {
          w[i].erase(w[i].begin(), w[i].begin() + a);
        }
        if (w[i].size()) {
          for (auto &p : w[i]) {
            w[w[i][0]].push_back(p);
          }
          sort(w[w[i][0]].begin(), w[w[i][0]].end());
          ++ans;
        }
      }
    }
    cout << ans << nl;
  }
  return 0;
}
