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