def gcd(a,b): while b:a,b=b,a%b return a def xgcd(a, b): x0, y0, x1,y1=1,0,0,1 while b != 0: q, a, b = a // b, b, a % b x0, x1 = x1, x0 - q * x1 y0, y1 = y1, y0 - q * y1 return x0 memo={} def modinv(a, m): if (a,m) in memo:return memo[(a,m)] x=xgcd(a, m) memo[(a,m)]=x%m return x % m import sys input=sys.stdin.readline t=int(input()) cases=[list(map(int,input().split())) for _ in range(t)] mod=10**9+7 for n,k,h,y in cases: ary=[n,k,h] ary.sort() a,b,c=ary g=gcd(a,b) na,nb=a//g,b//g now=0 ans=0 while now<=y: yy=y-now now+=c if yy%g!=0:continue yy=yy//g if yy==0: ans+=1 continue if yy