#d2847. 素数之欢

素数之欢

Prime Delight

Problem Description

You are given two four-digit prime numbers aa and bb. You may perform the following operation multiple times:

Change one digit of aa so that it becomes another four-digit prime number.

For example, 1033→17331033 \rightarrow 1\mathbf{7}33, where both 10331033 and 17331733 are prime.

What is the minimum number of transformations needed to turn aa into bb? Or answer that it is impossible.

Input Format

The first line contains an integer TT, indicating that TT groups of data follow.

The next TT lines each contain two four-digit primes a,ba, b separated by a space.

Output Format

Output TT lines. If possible, output the minimum number of transformations; otherwise output −1-1.

Sample Input

2
1033 1033
1033 8779

Sample Output

0
5

Sample Explanation

$$1033 \rightarrow 1\mathbf{7}33 \rightarrow \mathbf{3}733 \rightarrow 373\mathbf{9} \rightarrow 37\mathbf{7}9 \rightarrow \mathbf{8}779$$

Hint

If possible at all, you will need at most 88 operations.

Constraints

  • Multiple test cases, 1≤T≤1001 \le T \le 100