#include <iostream>
#define N 20
using namespace std;
unsigned int potega(int a, int b);
int main()
{
int D,a,b,c;
unsigned int wyniki[N];
cin >> D;
for(int i=0;i<D;i++){
cin >> a >> b;
int c = b%4;
if(b>0 && c>0)
b=c;
wyniki[i] = potega(a,b);
}
for(int i=0;i<D;i++){
cout << wyniki[i]%10 << endl;
}
}
unsigned int potega(int a, int b){
if(b==0)
return 1;
else
return a=a*potega(a,--b);
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojZGVmaW5lIE4gMjAKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKdW5zaWduZWQgaW50IHBvdGVnYShpbnQgYSwgaW50IGIpOwppbnQgbWFpbigpCnsKICAgIGludCBELGEsYixjOwogICAgdW5zaWduZWQgaW50IHd5bmlraVtOXTsKICAgIGNpbiA+PiBEOwogICAgZm9yKGludCBpPTA7aTxEO2krKyl7CiAgICAgICAgY2luID4+IGEgPj4gYjsKICAgICAgICBpbnQgYyA9IGIlNDsKICAgICAgICBpZihiPjAgJiYgYz4wKQogICAgICAgICAgICBiPWM7CiAgICAgICB3eW5pa2lbaV0gPSBwb3RlZ2EoYSxiKTsKICAgIH0KCiAgICBmb3IoaW50IGk9MDtpPEQ7aSsrKXsKICAgICAgICBjb3V0IDw8IHd5bmlraVtpXSUxMCA8PCBlbmRsOwogICAgfQp9CnVuc2lnbmVkIGludCBwb3RlZ2EoaW50IGEsIGludCBiKXsKICAgIGlmKGI9PTApCiAgICAgICAgcmV0dXJuIDE7CiAgICBlbHNlCiAgICAgICAgcmV0dXJuIGE9YSpwb3RlZ2EoYSwtLWIpOwp9Cg==