ile = int(input())
for i in range(ile):
c = input()
a = int((c.split())[0])
b = int((c.split())[1])
print(str(a ** b)[-1])
aWxlID0gaW50KGlucHV0KCkpCgpmb3IgaSBpbiByYW5nZShpbGUpOgogICAgYyA9IGlucHV0KCkKCiAgICBhID0gaW50KChjLnNwbGl0KCkpWzBdKQogICAgYiA9IGludCgoYy5zcGxpdCgpKVsxXSkKCiAgICBwcmludChzdHIoYSAqKiBiKVstMV0p