#P17534. [JAG 2026 Summer Camp #1] Capybara Petting Club

[JAG 2026 Summer Camp #1] Capybara Petting Club

Problem Description

The International Capybara Petting Club is preparing T-shirts for an event. There are four sizes, S, M, L, and XL, in increasing order. The numbers of T-shirts needed are nS,nM,nLn_{\mathrm{S}},n_{\mathrm{M}},n_{\mathrm{L}}, and nXLn_{\mathrm{XL}}, respectively. At the standard color intensity, T-shirts of sizes S, M, L, and XL require B,Br,Br2B,Br,Br^2, and Br3Br^3 units of dye, respectively.

The dyeing machine uses an integer intensity xx. The minimum allowed color intensity is PP, and x=Kx=K is the standard intensity. Thus, xx must satisfy P≤x≤KP\le x\le K. If a T-shirt requires qq units at the standard intensity, dyeing it at intensity xx uses ⌈qx/K⌉\lceil qx/K\rceil units. All T-shirts must use the same intensity xx.

Given AA units of dye, find the maximum possible intensity xx such that all T-shirts can be dyed without using more than AA units. If no such xx exists, output -1.

Input Format

The input contains TT test cases. The first line contains an integer TT (1≤T≤1001\le T\le 100), the number of test cases. Each test case consists of nine integers given in the following format:

n_S n_M n_L n_XL r A B P K

nS,nM,nLn_{\mathrm{S}},n_{\mathrm{M}},n_{\mathrm{L}}, and nXLn_{\mathrm{XL}} denote the numbers of T-shirts of sizes S, M, L, and XL, respectively ($0\le n_{\mathrm{S}},n_{\mathrm{M}},n_{\mathrm{L}},n_{\mathrm{XL}}\le 10^5$ and $n_{\mathrm{S}}+n_{\mathrm{M}}+n_{\mathrm{L}}+n_{\mathrm{XL}}\ge 1$).

The value rr denotes the factor by which the amount of dye required at the standard intensity increases with each increase in size (1≤r≤101\le r\le 10).

AA denotes the total amount of dye available, in units (0≤A≤10150\le A\le 10^{15}).

BB denotes the amount of dye required to dye one size-S T-shirt at the standard intensity, in units (1≤B≤1061\le B\le 10^6).

PP denotes the minimum allowed color intensity, and KK denotes the standard color intensity (1≤P≤K≤1091\le P\le K\le 10^9).

Output Format

For each test case, output the maximum integer xx such that P≤x≤KP\le x\le K and all T-shirts can be dyed using at most AA units of dye. If no such integer exists, output -1.

3
1 1 1 1 2 15 2 1 12
2 0 1 0 3 8 2 4 10
0 0 0 1 10 1000 1 900000 999999
6
-1
999999