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 = int(input()) for _ in range(T): *abc,n = map(int,input().split()) a,b,c = sorted(abc) ans = 0 g,p,q = extgcd(a,b) print(sum(t*q//a + t*p//b + 1 for t in range(n,-1,-c) if t%g==0)%1000000007)