Istnieje cykl, trzeba bylo policzyc sobie kilka pierwszych wyników a potem sprawdzić kiedy cykl sie rozpoczyna od nowa. W problemach algorytmicznych trzeba czasami pomyśleć, a nie robić na pałę.
import sys
matrix = [
["1\n","1\n","1\n","1\n"],
["2\n","4\n","8\n","6\n"],
["3\n","9\n","7\n","1\n"],
["4\n","6\n","4\n","6\n"],
["5\n","5\n","5\n","5\n"],
["6\n","6\n","6\n","6\n"],
["7\n","9\n","3\n","1\n"],
["8\n","4\n","2\n","6\n"],
["9\n","1\n","9\n","1\n"],
["0\n","0\n","0\n","0\n"],
]
test_num = int( sys.stdin.readline() )
for test_num in xrange(0, test_num):
input = sys.stdin.readline().split(' ')
sys.stdout.write( matrix[(int(input[0])-1)%10][(int(input[1])-1)%4] )
Albo po prostu
test_num = int(raw_input())
for test_num in xrange(0, test_num):
input = raw_input().split(' ')
a = int(input[0])
b = (int(input[1])-1)%4+1
x = 1
for i in xrange(0, b):
x *= a
x %= 10
print x