#include <bits/stdc++.h>
using namespace std;
const int maxn = 25;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    string a, b;
    cin >> n >> a >> b;
    int flag = 0;
    int t[maxn][maxn];
    memset(t, 0, sizeof(t));
    for (int i = 0; i < n; i++) {
      if (a[i] > b[i]) {
        flag = 1;
        break;
      }
    }
    if (flag) {
      cout << -1 << endl;
      continue;
    }
    for (int i = 0; i < n; i++) {
      if (a[i] != b[i]) {
        t[a[i] - 'a'][b[i] - 'a']++;
      }
    }
    int ans = 0;
    for (int i = 0; i < 24; i++) {
      int index = -1;
      for (int j = i + 1; j < 24; j++) {
        if (t[i][j]) {
          ans++;
          index = j;
          break;
        }
      }
      if (index == -1) continue;
      for (int j = 0; j < 24; j++) {
        t[index][j] += t[i][j];
      }
    }
    cout << ans << endl;
  }
  return 0;
}
