#5417. Problem 1. Chip Exchange

0

Problem 1. Chip Exchange

Problem 1. Chip Exchange

USACO 2026 First Contest, Bronze

Bessie the cow has in her possession AA chips of type A and BB chips of type B (0A,B1090\le A,B\le 10^9). She can perform the following operation as many times as she likes:

  • If you have at least cBc_B chips of type B, exchange cBc_B chips of type B for cAc_A chips of type A (1cA,cB1091\le c_A,c_B\le 10^9).

Determine the minimum non-negative integer xx such that the following holds: after receiving xx additional random chips, it is guaranteed that Bessie can end up with at least fAf_A chips of type A (0fA1090\le f_A\le 10^9).

INPUT FORMAT (input arrives from the terminal / stdin):

The first line contains TT, the number of independent test cases (1T1041\le T\le 10^4).

Then follow TT tests, each consisting of five integers A,B,cA,cB,fAA,B,c_A,c_B,f_A.

OUTPUT FORMAT (print output to the terminal / stdout):

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++).

SAMPLE INPUT:


2
2 3 1 1 6
2 3 1 1 4

SAMPLE OUTPUT:


1
0

SAMPLE INPUT:


5
0 0 2 3 5
0 1 2 3 5
1 0 2 3 5
10 10 2 3 5
0 0 1 1000000000 1000000000

SAMPLE OUTPUT:


9
8
7
0
1000000000000000000

For the first test, Bessie initially starts with no chips. If she receives any 99 additional chips, she can perform the operation to end up with at least 55 chips of type A. For example, if she receives 22 chips of type A and 77 chips of type B, she can perform the operation twice to end up with 656\ge 5 chips of type A. However, if she only receive 88 chips of type B, she can only end up with 4<54<5 chips of type A.

For the fourth test, she already has enough chips of type A from the start.

SCORING: Input 3: cA=cB=1c_A=c_B=1Inputs 4-5: x10x\le 10 for all casesInputs 6-7: cA=2c_A=2, cB=3c_B=3Inputs 8-12: No additional constraints.

Problem credits: Benjamin Qi