Given a lock which is made up of N different circular rings and each ring has 0-9 digit printed serially on it. Initially all N rings together show a N digit number but there is particular code which can open the lock. You can rotate each ring any number of times in either direction. The task to find the minimum number of rotations done on rings of the lock to open the lock.

**Input:**

The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains two numbers - *P* which is random number and *R* which is required number to open the lock. Both the numbers contain equal number of digits.

**Output:**

For each test case, print the minimum rotations required to open the lock, in new line.

**Constraints:**

1 <= T <= 100

1 <= p,q <= 10^{5}

**Example:**

**Input:**

2

222 333

2345 5432

**Output:**

3

8

**Explanation:**

```
Input : 2345 5432
Output : Rotations required = 8
Explanation :
1st ring is rotated thrice as 2->3->4->5
2nd ring is rotated once as 3->4
3rd ring is rotated once as 4->3
4th ring is rotated thrice as 5->4->3->2
```

Author: arun03

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

Sulagna | 117 |

RUTVIKHARIPARA | 116 |

janvidavda105190 | 93 |

harshsiddhapura105461 | 93 |

devsinghindra | 80 |

PiyushPandey4 | 614 |

ASWATHAMA | 561 |

akhyasharma01 | 547 |

john_wick | 521 |

jagrit_07 | 443 |

blackshadows | 5362 |

Ibrahim Nash | 5242 |

akhayrutdinov | 5111 |

mb1973 | 4929 |

Quandray | 4598 |

Login to report an issue on this page.