#include <bits/stdc++.h>
using namespace std;
int t[30];
inline int find(int x) { return t[x] == x ? x : t[x] = find(t[x]); }
inline void merge(int a, int b) {
  int x = find(a);
  int y = find(b);
  t[x] = t[y];
}
void solve() {
  int n;
  string a;
  string b;
  cin >> n;
  cin >> a;
  cin >> b;
  int cnt = 0;
  for (int i = 0; i < 27; i++) t[i] = i;
  for (int i = 0; i < n; i++) {
    if (a[i] > b[i]) {
      cout << "-1\n";
      return;
    }
    int x = a[i] - 'a';
    int y = b[i] - 'a';
    if (find(x) != find(y)) {
      cnt++;
      merge(x, y);
    }
  }
  cout << cnt << "\n";
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
}
