#include <bits/stdc++.h>
using namespace std;
const long long mod = (long long)1e9 + 7;
const long long maxn = (long long)2e5 + 1100;
const int nnn = 664579 + 500;
const int inf = numeric_limits<int>::max() - 1;
const long long INF = 1e9;
long long dx[] = {0, 1, 0, -1};
long long dy[] = {1, 0, -1, 0};
long long dxx[] = {0, 1, 0, -1, 1, 1, -1, -1};
long long dyy[] = {1, 0, -1, 0, 1, -1, 1, -1};
void solve() {
  long long n;
  cin >> n;
  string a, b;
  cin >> a >> b;
  n = a.length();
  long long t[n + 5];
  long long mark[n + 5];
  long long dp[55][55];
  memset(dp, 0, sizeof(dp));
  memset(mark, -1, sizeof(mark));
  memset(t, 0, sizeof(t));
  for (int i = 0; i < (n); i++) {
    if (b[i] < a[i]) {
      cout << "-1\n";
      return;
    }
    if (b[i] == a[i])
      mark[i] = 1;
    else
      dp[a[i] - 'a'][b[i] - 'a']++;
  }
  long long ans = 0;
  for (long long i = 0; i < 21; i++) {
    for (long long j = i + 1; j < 21; j++) {
      if (dp[i][j] != 0) {
        ans++;
        for (long long x = j + 1; x < 21; x++) dp[j][x] += dp[i][x];
        break;
      }
    }
  }
  cout << ans << '\n';
  return;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  long long T;
  T = 1;
  cin >> T;
  long long CT = 0;
  while (T--) {
    solve();
  }
  return 0;
}
