t=int(input()) for i in range(t): l,r,a,b=map(int,input().split()) se=set() for j in range(l,r+1): se.add(a*j+b) print(max(se))