#include <bits/stdc++.h>
#pragma GCC optimize "-O3"
using namespace std;
const int ALPHABET_SIZE = 20;
void set_io() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
}
int n;
string a, b;
void read() {
  cin >> n;
  cin >> a >> b;
}
vector<int> adj_list[ALPHABET_SIZE];
bool seen[ALPHABET_SIZE];
void dfs(int node) {
  seen[node] = true;
  for (int adj_node : adj_list[node]) {
    if (!seen[adj_node]) {
      dfs(adj_node);
    }
  }
}
void solve() {
  for (int i = 0; i < n; i++) {
    if (a[i] > b[i]) {
      cout << "-1\n";
      return;
    }
  }
  for (int i = 0; i < ALPHABET_SIZE; i++) {
    adj_list[i].clear();
  }
  for (int i = 0; i < n; i++) {
    if (a[i] != b[i]) {
      adj_list[a[i] - 'a'].emplace_back(b[i] - 'a');
      adj_list[b[i] - 'a'].emplace_back(a[i] - 'a');
    }
  }
  memset(seen, false, sizeof(seen));
  int answer = ALPHABET_SIZE;
  for (int i = 0; i < ALPHABET_SIZE; i++) {
    if (!seen[i]) {
      dfs(i);
      answer--;
    }
  }
  cout << answer << '\n';
}
int main() {
  set_io();
  int test_cases;
  cin >> test_cases;
  while (test_cases--) {
    read();
    solve();
  }
  return EXIT_SUCCESS;
}
