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
No_words: 2012-10-30 17:41:01

700B ??
is it correct ? :O

Rajagopal: 2012-10-07 20:56:36

what data structure can be used if we need to program in c/c++.

Rodrigo Salazar: 2012-09-15 02:14:48

If you're having trouble understanding or solving this problem, try checking out:

http://www.swageroo.com/wordpress/spoj-problem-3442-the-last-digit-lastdig/

Abhimanyu: 2012-09-14 20:16:03

wasted couple of hours taking array of 10 ... while it should be 11 ... wondering why it still showed the right answer on my machine even for largest data ... stil got AC .. silly mistakes !! :)

Last edit: 2012-09-14 20:19:41
Jose Daniel Rodríguez Morales: 2012-09-14 17:36:48

Source Limit should be enough

PS: the data tests were fixed

Prakash Murthy: 2012-09-14 17:36:48

My original code broke down when a=20 & b=214,748,3000

Found a better algorithm, and got accepted. ;-)

In the second attempt, had forgotten the case of any number raised to 0 is 1.

Last edit: 2012-08-13 17:06:10
gourav: 2012-09-14 17:36:48

what is tricky case ?

InVoKe[R]: 2012-09-14 17:36:48

NZEC (non-zero exit code) - this message means that the program exited returning a value different from 0 to the shell. For languages such as C, this probably means you forgot to add "return 0" at the end of the program. For interpreted languages (including JAVA) NZEC will usually mean that your program either crashed or raised an uncaught exception.

Last edit: 2012-08-03 07:52:02
007: Name stolen: 2012-09-14 17:36:48

can anyone tell me why my code is giving NZEC id 7393791

Preetam Shivaram: 2012-09-14 17:36:48

@Jose Daniel Rodríguez Morales-in which case my code gives error..code id is 7278511


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