from sys import stderr perr=stderr.write class INPUT: def __init__(self): self._l=open(0).read().split() self._length=len(self._l) self._index=0 return def stream(self,k=1,f=int,f2=False): assert(-1 return left all #listを変数で受け取るとき、必ずlistをTrueにすること。 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,M): A=matrix_exp(A,n,mod) res=matrix_mul(A,[[M],[0],[0],[3*M],[0],[0]],mod) return res[3][0] def solve(): mod=10**9+7 N,M=map(int,pin().split()) 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]] #A=[[1/3,0,0,0,1/3,1/3],[0,1/3,0,1/3,0,1/3],[0,0,1/3,1/3,1/3,0],[1,0,0,0,0,0],[0,1,0,0,0,0],[0,0,1,0,0,0]] ans=calculate_nth(mod,N,A,M) c=pow(pow(3,mod-2,mod),N+1,mod) print(ans*c%mod) #print(*ans,sep="\n") return from sys import stdin pin=stdin.readline def main(): T=int(pin()) for i in range(T): solve() return main()