t=int(input()) xandk=[list(map(int,input().split())) for i in range(t)] p=10**9+7 def indetermined(a,b,c): p=[1,0] q=[0,1] while(a>1 and b>1): if(a>b): q[0]+=(a//b)*p[0] q[1]+=(a//b)*p[1] a=a%b elif(b>a): p[0]+=(b//a)*q[0] p[1]+=(b//a)*q[1] b=b%a if(a==1): x=((1-b)*q[1]-p[1])/(p[0]*q[1]-p[1]*q[0]) y=(1-q[0]*x)/q[1] else: x=(q[1]-(1-a)*p[1])/(p[0]*q[1]-p[1]*q[0]) y=(1-p[0]*x)/p[1] x=int(x) y=int(y) return x,y for i in range(t): x=xandk[i][0] k=xandk[i][1] ans=indetermined(k,p-1,1)[0]%(p-1) print(pow(x,ans,p))