def extgcd(x,y): if y==0: return 1,0 r0,r1,s0,s1 = x,y,1,0 while r1 != 0: r0,r1,s0,s1 = r1,r0%r1,s1,s0-r0//r1*s1 return r0,s0,(r0-s0*x)//y T,*A = map(int,open(0).read().split()) for i in range(0,T,4): a,b,c = sorted(A[i:i+3]) g,p,q = extgcd(a,b) print(sum(t*q//a + t*p//b + 1 for t in range(A[i+4],-1,-c) if t%g==0)%1000000007)