import math n=int(input()) f=[[1, 1], [1, 0]] MOD=10**9+7 def mul(a, b): ret=[[0, 0], [0, 0]] for i in range(2): for j in range(2): for k in range(2): ret[i][j]+=a[i][k]*b[k][j] return ret def powmod(a, k, mod): ret=[[1, 0], [0, 1]] ap=a while k>0: if k&1: ret=mul(ret, ap) for i in range(2): for j in range(2): ret[i][j]%=mod ap=mul(ap, ap) for i in range(2): for j in range(2): ap[i][j]%=mod k>>=1 return ret ans=1 def lcm(a, b): return a//math.gcd(a, b)*b for i in range(n): p, k=map(int, input().split()) mod=p**k e=(p**(2*k-2))*(p*p-1) x=p-1 fac=[] for j in range(2, x+1): if j*j>x: break if x%j==0: t=0 while x%j==0: x//=j t+=1 fac.append([j, t]) if x>1: fac.append([x, 1]) x=p+1 for j in range(2, x+1): if j*j>x: break if x%j==0: t=0 while x%j==0: x//=j t+=1 if j==2: fac[0][1]+=t else: fac.append([j, t]) if x>1: fac.append([x, 1]) for j in range(2*k-2): e//=p fp=powmod(f, e, mod) if fp[0][0]==1 and fp[1][0]==0: continue e*=p break for q in fac: for j in range(q[1]): e//=q[0] fp=powmod(f, e, mod) if fp[0][0]==1 and fp[1][0]==0: continue e*=q[0] break ans=lcm(ans, e) print(ans%MOD)