LASTDIG - The last digit

Nestor 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 him two numbers a and b. The problem consist of finding the last digit of the potency of base a and index b. Help Nestor with his problem. You are given two integer numbers: the base a (0 <= a <= 20) and the index b (0 <= b <= 2,147,483,000), a and b both are not 0. You have to find the last digit of ab.

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:
2
3 10
6 2

Output: 9 6

Added by:Jose Daniel Rodriguez Morales
Date:2008-12-01
Time limit:1s
Source limit:700B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: GOSU
Resource:Own

hide comments
2015-09-03 14:40:51 thirupathireddy
this is not good mentioning that both a and b not zero.............
2015-08-03 05:27:31 Mahesh Chaurasia
Please consider case a==0 and b==0; Considering them not together.
2015-07-25 16:52:31
@avid coder
add
a=a%10 ; //0<= a<=20
2015-07-19 20:05:05 Avinash Bansal
check the test case when index=0,
it cost me 1 WA
2015-07-18 08:47:13
I got TLE with Modular Exponentiation in C :\
2015-07-18 08:16:33 Avinash Kumar
if you get 700 bytes limit error, remove all spaces. while solving just remember 1,4,4,2,1 :]
2015-07-14 23:16:14
removed extra space and thus got accepted.
2015-07-12 13:28:00 Shivam Singh
you only need the last digit of a to find the last digit of any of its powers.
Hint: if last digit of a is 3, the last digits of b repeat as 3,9,7,1 in a period of four
2015-07-04 09:40:06
YOU CAN FIND THE C++ SOLUTION HERE IF YOU CAN'T SOLVE IT
<snip>

Last edit: 2023-06-12 22:43:01
2015-06-20 20:47:26
b=0 case is included despite the contradicting constraints... -_-

Last edit: 2015-06-20 20:48:54
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.