P14974 [USACO26JAN1] Chip Exchange B

Description

Bessie the cow has in her possession $A$ chips of type A and $B$ chips of type B ($0\le A,B\le 10^9$). She can perform the following operation as many times as she likes: - If you have at least $c_B$ chips of type B, exchange $c_B$ chips of type B for $c_A$ chips of type A ($1\le c_A,c_B\le 10^9$). Determine the minimum non-negative integer $x$ such that the following holds: after receiving $x$ additional random chips, it is guaranteed that Bessie can end up with at least $f_A$ chips of type A ($0\le f_A\le 10^9$).

Input Format

The first line contains $T$, the number of independent test cases ($1\le T\le 10^4$). Then follow $T$ tests, each consisting of five integers $A,B,c_A,c_B,f_A$.

Output Format

Output the answer for each test on a separate line. **Note: The large size of integers involved in this problem may require the use of 64-bit integer data types (e.g., a "long long" in C/C++).**

Explanation/Hint

For the first test, Bessie initially starts with no chips. If she receives any $9$ additional chips, she can perform the operation to end up with at least $5$ chips of type A. For example, if she receives $2$ chips of type A and $7$ chips of type B, she can perform the operation twice to end up with $6\ge 5$ chips of type A. However, if she only receive $8$ chips of type B, she can only end up with $4