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.


hide comments
Ankush : 2015-06-08 10:31:57

Applied same algorithm which I applied in http://www.spoj.com/problems/LASTDIG/ . Modular Exponentiation and Python \m/ :D

Manish Sombansh: 2015-06-05 19:15:55

AC in one go!

Tejas Meshram: 2015-05-26 19:32:26

what is the answer for a=0 and b=0 ... i mean 0^0.

[Mayank Pratap]: 2015-05-06 19:51:45

Taking a as string and b as long long int made it easier...rest simple maths...

tech96: 2015-05-02 16:50:47

Only two lines for the actual function in Python

casualcoder: 2015-04-20 10:34:29

Good practice for boundary cases.

Sue: 2015-03-06 15:14:55

use math only :))

Amogh: 2015-03-03 08:06:29

silly mistakes due to modulo caused me 3 WAs :( exponent will fit in to long long

Ankit: 2015-02-28 13:24:41

an accepted solution of the problem gives 100^0 as 0. Shouldnt the answer be 1 ?

Zed: 2015-02-08 15:22:21

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.


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