CF BUDDY
← Problems·

1383A · String Transformation 1

1700 · dsu, graphs, greedy

Problem: Note that the only difference between String Transformation 1 and String Transformation 2 is in the move Koa does. In this version the letter yy Koa selects must be strictly greater alphabetically than xx (read statement for better understanding). You can make hacks in these problems independently.

Koa the Koala has two strings AA and BB of the same length nn (A=B=n|A|=|B|=n) consisting of the first 2020 lowercase English alphabet letters (ie. from a to t).

In one move Koa:

  1. selects some subset of positions p1,p2,,pkp_1, p_2, \ldots, p_k (k1;1pin;pipjk \ge 1; 1 \le p_i \le n; p_i \neq p_j if iji \neq j) of AA such that Ap1=Ap2==Apk=xA_{p_1} = A_{p_2} = \ldots = A_{p_k} = x (ie. all letters on this positions are equal to some letter xx).
  2. selects a letter yy (from the first 2020 lowercase letters in English alphabet) such that y>xy>x (ie. letter yy is strictly greater alphabetically than xx).
  3. sets each letter in positions p1,p2,,pkp_1, p_2, \ldots, p_k to letter yy. More formally: for each ii (1ik1 \le i \le k) Koa sets Api=yA_{p_i} = y. Note that you can only modify letters in string AA.

Koa wants to know the smallest number of moves she has to do to make strings equal to each other (A=BA = B) or to determine that there is no way to make them equal. Help her!

Input Format: Each test contains multiple test cases. The first line contains tt (1t101 \le t \le 10) — the number of test cases. Description of the test cases follows.

The first line of each test case contains one integer nn (1n1051 \le n \le 10^5) — the length of strings AA and BB.

The second line of each test case contains string AA (A=n|A|=n).

The third line of each test case contains string BB (B=n|B|=n).

Both strings consists of the first 2020 lowercase English alphabet letters (ie. from a to t).

It is guaranteed that the sum of nn over all test cases does not exceed 10510^5.

Output Format: For each test case:

Print on a single line the smallest number of moves she has to do to make strings equal to each other (A=BA = B) or 1-1 if there is no way to make them equal.

Note:

  • In the 11-st test case Koa: selects positions 11 and 22 and sets A1=A2=A_1 = A_2 = b (aabbbb\color{red}{aa}b \rightarrow \color{blue}{bb}b). selects positions 22 and 33 and sets A2=A3=A_2 = A_3 = c (bbbbccb\color{red}{bb} \rightarrow b\color{blue}{cc}).
  • In the 22-nd test case Koa has no way to make string AA equal BB.
  • In the 33-rd test case Koa: selects position 11 and sets A1=A_1 = t (abctbc\color{red}{a}bc \rightarrow \color{blue}{t}bc). selects position 22 and sets A2=A_2 = s (tbctsct\color{red}{b}c \rightarrow t\color{blue}{s}c). selects position 33 and sets A3=A_3 = r (tsctsrts\color{red}{c} \rightarrow ts\color{blue}{r}).

Sample Cases

Case 1

Input

5
3
aab
bcc
4
cabc
abcb
3
abc
tsr
4
aabd
cccd
5
abcbd
bcdda

Output

2
-1
3
2
-1

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback