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
2017-04-11 23:19:42
ignoring the test case b=0 costed me 2 wA
2017-03-09 19:13:44
Take input as char or string>>Take out the last digit>>do modular exponentation of both the digits, that is lastdigit(char a) and num b.print the result ,thats it.
2017-02-16 12:57:40
AC!
WA didn't consider b can be 0 too!
2017-02-16 10:21:26
AC in one go!! 0.00s
2017-01-16 13:18:55
very easy ac in one go :)
2017-01-13 05:48:54
did LASTDIG very seriously, this time just nailed it :)
2016-12-13 12:06:41
Remarks : 1. Use mod. expo..
2. base storage as a char array and index long long int
3. IMP CONCEPT (rem(%10) with last digit of base = rem(%10) with the entire base )
2016-12-08 05:48:32
@kshubham02, I think that the file size restriction is that they want a modular exponentiation solution which takes a very little file size compared to the Theta(t) solution
2016-12-08 05:45:32
o.oo seconds in C !!

Last edit: 2016-12-08 06:13:20
2016-10-28 14:39:24
29th, used std::string, same as LASTDIG.
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.