#include <iostream>
#define N 20
using namespace std;
int potega(int a, int b);
int main()
{
int D;
int tab[N], wyniki[N];
cin >> D;
for(int i=0;i<2*D;i+=2){
cin >> tab[i] >> tab[i+1];
tab[i+1] = tab[i+1]%4;
}
for(int i=0,n=0;i<2*D;i+=2,n++){
wyniki[n] = potega(tab[i],tab[i+1]);
}
for(int n=0;n<D;n++){
cout << wyniki[n]%10 << endl;
}
}
int potega(int a, int b){
if(b==0)
return 1;
else
return a=a*potega(a,--b);
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojZGVmaW5lIE4gMjAKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKaW50IHBvdGVnYShpbnQgYSwgaW50IGIpOwppbnQgbWFpbigpCnsKICAgIGludCBEOwogICAgaW50IHRhYltOXSwgd3luaWtpW05dOwogICAgY2luID4+IEQ7CiAgICBmb3IoaW50IGk9MDtpPDIqRDtpKz0yKXsKICAgICAgICBjaW4gPj4gdGFiW2ldID4+IHRhYltpKzFdOwogICAgICAgIHRhYltpKzFdID0gdGFiW2krMV0lNDsKICAgIH0KICAgIGZvcihpbnQgaT0wLG49MDtpPDIqRDtpKz0yLG4rKyl7CiAgICAgICB3eW5pa2lbbl0gPSBwb3RlZ2EodGFiW2ldLHRhYltpKzFdKTsKICAgIH0KICAgIGZvcihpbnQgbj0wO248RDtuKyspewogICAgICAgIGNvdXQgPDwgd3luaWtpW25dJTEwIDw8IGVuZGw7CiAgICB9CgoKfQppbnQgcG90ZWdhKGludCBhLCBpbnQgYil7CiAgICBpZihiPT0wKQogICAgICAgIHJldHVybiAxOwogICAgZWxzZQogICAgICAgIHJldHVybiBhPWEqcG90ZWdhKGEsLS1iKTsKfQo=