#include #include using namespace std; int T, L, R, A, B; int main() { cin >> T; while (T--) { cin >> L >> R >> A >> B; int x = floor((R - B) / (double) A); int y = ceil((L - B) / (double) A); if (x < y) { cout << B << endl; } else { cout << max(A * x + B, A * y + B) << endl; } } return 0; }