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

hide comments
mishkat007: 2022-09-15 14:58:49

should i return result % 10 to get the exact last digit??

Last edit: 2022-09-15 15:00:13
rishisingh7012: 2021-11-20 14:47:58

check for special cases 0 1 and 1 0 also.

mohit_010: 2021-11-10 08:03:17

think in a way that last digit of A will be the main cause to find the last digit of power ans ;;

rapiram31: 2021-09-09 14:22:54

There will be only two cases one is cycle from 1 (1 3 9 27 81) , second is cycle from x (1 4 16 64)

sabkx: 2021-08-31 15:54:41

Euler theorem can solve this in O(1) for those that are stuck

messifcbarcel: 2021-08-13 03:02:22

Solved in first attempt instead of a compilation error because of that 700B thing

shubh3082: 2021-06-15 11:08:08

i am getting correct solution for the problem on codechef but getting WA over here.
plz help

unwanted_coder: 2021-06-01 07:40:35

AC in one go After reading about modular exponentiation

shankar_0x: 2021-05-31 09:42:20

AC IN ONE GO, YALL NOOBS

loser_404: 2021-05-31 06:09:35

good problem. Though (a=0 and b=0)=0 also get accepted. use modular exponentiation and use mod value as 10 for last digit


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