#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int MN = 1e5 + 5;
int g[26][26];
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    memset(g, 0, sizeof(g));
    int n;
    cin >> n;
    string a, b;
    cin >> a >> b;
    bool ok = true;
    for (int i = 0; i < n; i++) {
      if (a[i] > b[i]) {
        ok = false;
        break;
      }
      g[a[i] - 'a'][b[i] - 'a']++;
    }
    if (!ok) {
      cout << -1 << '\n';
    } else {
      int ans = 0;
      for (int i = 0; i < 20; i++) {
        int mn = 0;
        for (int j = i + 1; j < 20; j++) {
          if (g[i][j]) {
            mn = j;
            break;
          }
        }
        if (mn) {
          for (int j = i + 1; j < 20; j++) {
            g[mn][j] += g[i][j];
          }
          ans++;
        }
      }
      cout << ans << '\n';
    }
  }
  return 0;
}
