This page is still under construction.

Parts of this page are still being built. What you see may change.

Timer

Time limit2sMemory limit256 MB

Summary
Each use of the exploit rounds the timer up to the next multiple of y; with at most k uses over t seconds, find the largest value reachable.
Level

Medium7 of 10

Topics
Greedy, Math, Implementation, Brute force
Solved
No attempts yet

Problem

As punishment for acting up at school, Petya is serving a sentence under house arrest. In his room there is a timer that shows how long Petya has already been serving his punishment. Every second the number on the timer increases by one. Wanting to shorten his punishment, Petya found a vulnerability in the timer. If the timer currently shows aa seconds, then, using the vulnerability, Petya can make it show bb seconds, where bb is the smallest number greater than or equal to aa that is divisible by a given number yy.

Petya wants the timer to show as many seconds as possible. However, Petya also does not want to get caught, so he will not use the vulnerability more than kk times. What is the maximum number on the timer that Petya can get after tt real seconds? At the moment Petya discovered the vulnerability, the timer showed the number xx.

For example, suppose that at the moment the vulnerability was discovered the timer showed x=9x = 9, y=10y = 10, Petya plans to use the vulnerability at most k=10k = 10 times, and t=2t = 2. Then the maximum number Petya can get on the timer is 30, and to do this he must act as follows. Immediately using the vulnerability, he gets the value 10 on the timer. After 1 second the timer will show 11, and by using the vulnerability Petya gets 20 on it. After another second it will be 21, and by using the vulnerability Petya will get the value 30.

Input

The first line contains the integer TT (1≤T≤1041 \le T \le 10^4), the number of test cases. Each of the following TT lines contains four integers: xx, yy, kk, and tt. (All numbers are between 1 and 10910^9 inclusive.)

Output

For each of the TT test cases, output a single number: the maximum possible number the timer can show after tt seconds.

Examples1

  1. Example 1

    Input
    2
    2 6 1 3
    9 10 10 2
    
    Expected output
    9
    30