def floor_sum(n,m,a,b): res = 0 while True: res += ((n-1)*n*(a//m)>>1) + (b//m)*n a %= m b %= m if a*n+b <= m: return res Y = (a*n+b)//m n,m,a,b = Y,a,m, a*n + b - m*Y def points_in_triange(a,b,t): return floor_sum(1+t//a,b,a,t-t//a*a) + t//a + 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_in_triange(a,b,t) - points_in_triange(a,b,t-1) print(ans)