import sys def I(): return int(sys.stdin.readline().rstrip()) def MI(): return map(int,sys.stdin.readline().rstrip().split()) T = I() mod = 10**9+7 for _ in range(T): X,K = MI() if X == 0: print(0) continue a = pow(K,(mod-1)//2-2,mod-1) print(pow(X,a,mod))