In 1801, Carl Friedrich Gauss (1777-1855) published "Disquisitiones Arithmeticae", which laid the foundations of modern number theory and is still in print today. One of the topics it treats most often is the quadratic residue.
You are given a prime p and an integer a with a≡0(modp). For a to be a quadratic residue modulo p, there must exist an integer x such that
x2≡a(modp)
Legendre (1752-1833) introduced the following Legendre symbol.
(pa)={1−1a is a quadratic residue mod pa is not a quadratic residue mod p
The Legendre symbol can be computed using the following properties, which hold only for distinct odd primes p, q and integers a, b not divisible by p.
For example, the Legendre symbol can be computed as follows.
(7929)=(−1)78⋅28/4(2979)=(2979)=(29−8)=(29−1)(292)3=(29−1)(292)=(−1)28/2(−1)(292−1)/8=(−1)14(−1)105=−1
The input consists of several test cases. The first line contains the number of test cases T. Each of the following test cases consists of two integers a and p. Here p (2<p<109) is an odd prime, and a satisfies a≡0(modp) and ∣a∣≤109.
For each test case, print "Scenario #i:" on the first line, where i is the test case number starting from 1. On the next line, print the value of the Legendre symbol (pa) (either 1 or -1). Print one blank line between the outputs of consecutive test cases.