Cracking RSA
면접 대비시간 제한3초메모리 제한1024 MB
작은 RSA 모듈러스를 두 소인수로 분해해 오일러 파이 함수를 구한 뒤, e의 모듈러 역원을 계산한다.
문제

Image by Sean MacEntee (Flickr) [CC BY 2.0]
RSA is a widely used public-key cryptosystem that allows two parties (such as people or computers) to exchange secret messages without revealing information to anyone else listening on the conversation. Many websites use RSA when visited over a secure https connection. In RSA, each party has two different keys: a public key that is published and a private key that is kept secret. To encrypt a message intended for a specific recipient, the sender will use the recipient's public key to encrypt the message. The recipient will use their private key to decrypt the message.
An RSA public key consists of two numbers and . The number is a product of two distinct prime numbers, and . In real applications, would be hundreds of decimal digits long for security.
Let be Euler's totient function, which in this case is equal to . The private key consists of , where is the same as in the public key and is the solution to the congruence \[de \equiv 1 \bmod \varphi(n)\] Formally, a congruence \[a \equiv b \bmod c\] holds for three integers , , and , if there exists an integer such that .
The sender will encrypt a message (which, for simplicity, is assumed to be an integer smaller than both and ) by computing and sending it to the receiver. The recipient will calculate since by Euler's theorem . This will reconstruct the original message. Without the private key, no practical way has been found for a potential attacker to recover from the knowledge of and .
Your task is to crack RSA by finding the private key related to a specific public key.
입력
The first line of input has the number of test cases , (). Each test case has one line that contains the two numbers and . You may assume that is the product of two primes such that . Also, will be chosen so that exists and is unique, and . Note that the product may not fit into a 32-bit integer (e.g. Java's int type).
출력
For each test case, output the single number .