import sys input = sys.stdin.readline mod=10**9+7 from math import gcd # 拡張ユークリッドの互除法.ax+by=gcd(a,b)となる(x,y)を一つ求め、(x,y)とgcd(x,y)を返す. def Ext_Euc(a,b,axy=(1,0),bxy=(0,1)): # axy=a*1+b*0,bxy=a*0+b*1なので,a,bに対応する係数の初期値は(1,0),(0,1) # print(a,b,axy,bxy) q,r=divmod(a,b) if r==0: return bxy,b # a*bxy[0]+b*bxy[1]=b rxy=(axy[0]-bxy[0]*q,axy[1]-bxy[1]*q) # rに対応する係数を求める. return Ext_Euc(b,r,bxy,rxy) T=int(input()) for tests in range(T): N,K,H,Y=map(int,input().split()) A=[N,K,H] A.sort(reverse=True) a,b,c=A G_bc=gcd(b,c) (x0,y0),G=Ext_Euc(b,c) L=b*c//G_bc ANS=0 for i in range(Y//a+1): rest=Y-i*a if rest%G_bc!=0: continue x1=x0*rest//G_bc y1=y0*rest//G_bc #print(-b*x1,c*y1) if (-b*x0)*(c*y0)<=0: ANS+=b*x1//L+c*y1//L+1 elif (-b*x0)<=0 and (c*y0)<=0: ANS+=abs(abs(-b*x1)//L-(abs(c*y1)-1)//L) else: ANS+=abs((abs(c*y1))//L-(abs(-b*x1)-1)//L) ANS%=mod print(ANS)