#include <bits/stdc++.h>
#pragma optimize GCC("O3")
using namespace std;
void solve() {
  int n;
  cin >> n;
  int go[20][20] = {0};
  string a, b;
  cin >> a >> b;
  for (int i = 0; i < n; i++) {
    go[a[i] - 'a'][b[i] - 'a']++;
    if (a[i] > b[i]) {
      cout << "-1\n";
      return;
    }
  }
  for (int i = 19; i >= 0; i--) {
    for (int j = 19; j >= i; j--) {
      if (i == j) continue;
      for (int k = j - 1; k > i; k--) {
        if (go[k][j] > 0) {
          go[k][j] += go[i][j];
          go[i][k] += go[i][j];
          go[i][j] = 0;
        }
      }
    }
  }
  int cnt = 0;
  for (int i = 0; i < 20; i++) {
    for (int j = 0; j < 20; j++) {
      if (i == j) continue;
      if (go[i][j] > 0) {
        cnt++;
      }
    }
  }
  cout << cnt << endl;
}
int main() {
  cin.tie(NULL);
  cout.tie(NULL);
  ios::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--) solve();
}
