#include <iostream>
using namespace std;
int potega(int a, int b);
int main()
{
int D,c;
unsigned long int a,b;
cin >> D;
for(int i=0;i<D;i++){
cin >> a >> b;
int c = b%4;
if(b>0 && c>0)
b=c;
cout << potega(a,b)%10 << endl;
}
}
int potega(int a, int b){
if(b==0)
return 1;
else
return a=a*potega(a,--b);
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwppbnQgcG90ZWdhKGludCBhLCBpbnQgYik7CmludCBtYWluKCkKewogICAgaW50IEQsYzsKICAgIHVuc2lnbmVkIGxvbmcgaW50IGEsYjsKICAgIGNpbiA+PiBEOwogICAgZm9yKGludCBpPTA7aTxEO2krKyl7CiAgICAgICAgY2luID4+IGEgPj4gYjsKICAgICAgICBpbnQgYyA9IGIlNDsKCiAgICAgICAgaWYoYj4wICYmIGM+MCkKICAgICAgICAgICAgYj1jOwoKICAgICAgIGNvdXQgPDwgcG90ZWdhKGEsYiklMTAgPDwgZW5kbDsKCiAgICB9Cgp9CmludCBwb3RlZ2EoaW50IGEsIGludCBiKXsKICAgIGlmKGI9PTApCiAgICAgICAgcmV0dXJuIDE7CiAgICBlbHNlCiAgICAgICAgcmV0dXJuIGE9YSpwb3RlZ2EoYSwtLWIpOwp9Cgo=