#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    string a, b;
    cin >> a >> b;
    int ans = 0;
    for (char c = 'a'; c <= 'z'; c++) {
      if (ans == -1) break;
      int cnt[25] = {};
      for (int i = 0; i < n; i++) {
        if (b[i] == c && a[i] > c) {
          ans = -1;
          break;
        }
        if (b[i] == c && a[i] < c) {
          if (cnt[a[i] - 'a'] == 0) ans++;
          cnt[a[i] - 'a'] = 1;
        }
      }
      for (int i = 0; i < n; i++) {
        if (cnt[a[i] - 'a']) a[i] = c;
      }
    }
    cout << ans << endl;
  }
}
