LASTDIG2 - The last digit re-visited

Pappu was doing the work of his math class about three days but he is tired of make operations a lot and he should deliver his task tomorrow. His math’s teacher gives two numbers a and b. The problem consist in find the last digit of the potency of base a and index b. Help Pappu with his problem. You are given two integer numbers: the base a (number of digits d, such that 1 <= d <= 1000) and the index b (0 <= b <= 922*10^15). You have to find the last digit of a^b.

Input

The first line of input contains an integer t, the number of test cases (t <= 30). t test cases follow. For each test case will appear a and b separated by space.

Output

For each test case output an integer per line representing the result.

Example

Input:
3
3 10
6 2
150 53

Output:
9
6
0

Source limit is 700 Bytes.


Added by:Hari
Date:2009-12-27
Time limit:1s
Source limit:700B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: NODEJS OBJC PERL6 SQLITE VB.NET
Resource:harrydcoder

hide comments
2015-06-08 10:31:57 Ankush
Applied same algorithm which I applied in http://www.spoj.com/problems/LASTDIG/ . Modular Exponentiation and Python \m/ :D
2015-06-05 19:15:55 Manish Sombansh
AC in one go!
2015-05-26 19:32:26 Tejas Meshram
what is the answer for a=0 and b=0 ... i mean 0^0.
2015-05-06 19:51:45 [Mayank Pratap]
Taking a as string and b as long long int made it easier...rest simple maths...
2015-05-02 16:50:47 tech96
Only two lines for the actual function in Python
2015-04-20 10:34:29 casualcoder
Good practice for boundary cases.
2015-03-06 15:14:55 Sue
use math only :))
2015-03-03 08:06:29 Amogh
silly mistakes due to modulo caused me 3 WAs :( exponent will fit in to long long
2015-02-28 13:24:41 Ankit
an accepted solution of the problem gives 100^0 as 0. Shouldnt the answer be 1 ?
2015-02-08 15:22:21 Zed
variable (string) which stores the base has to have length longer than 101, this has cost me 2WA. Also, exponent easily fits into long long unsigned.
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.