def matrix_mul(A,B,mod): C=[[0]*len(B)for _ in[0]*len(A)] for i in range(len(A)): for k in range(len(B)): for j in range(len(B[0])): C[i][j]=(C[i][j]+A[i][k]*B[k][j])%mod return C def matrix_exp(X,n,mod): Y=[[0]*len(X)for _ in[0]*len(X)] for i in range(len(X)): Y[i][i]=1 while n>0: if n&1: Y=matrix_mul(Y,X,mod) X=matrix_mul(X,X,mod) n>>=1 return Y def calculate_nth(mod,n,A): A=matrix_exp(A,n,mod) res=matrix_mul(A,[[1],[0],[0],[3],[0],[0]],mod) return res[3][0] def solve(): mod=10**9+7 N,M=map(int,pin().split()) assert(1<=N<=10**9) assert(1<=M<=10**9) A=[[1,0,0,0,1,1],[0,1,0,1,0,1],[0,0,1,1,1,0],[3,0,0,0,0,0],[0,3,0,0,0,0],[0,0,3,0,0,0]] ans=calculate_nth(mod,N,A) c=pow(pow(3,mod-2,mod),N+1,mod) # A にいる確率 a=ans*c%mod # A にいない確率 b=(1-a)%mod # A に1台もいない確率 d=pow(b,M,mod) # A に1台以上いる確率 e=(1-d)%mod print(e) return from sys import stdin pin=stdin.readline def main(): T=int(pin()) assert(1<=T<=3*10**3) for i in range(T): solve() return main()