CF BUDDY
← Problems·

1538G · Gift Set

2100 · binary search, greedy, math

Problem: Polycarp has xx of red and yy of blue candies. Using them, he wants to make gift sets. Each gift set contains either aa red candies and bb blue candies, or aa blue candies and bb red candies. Any candy can belong to at most one gift set.

Help Polycarp to find the largest number of gift sets he can create.

For example, if x=10x = 10, y=12y = 12, a=5a = 5, and b=2b = 2, then Polycarp can make three gift sets:

  • In the first set there will be 55 red candies and 22 blue candies;
  • In the second set there will be 55 blue candies and 22 red candies;
  • In the third set will be 55 blue candies and 22 red candies.

Note that in this example there is one red candy that Polycarp does not use in any gift set.

Input Format: The first line contains an integer tt (1t1041 \le t \le 10^4). Then tt test cases follow.

Each test case consists of a single string containing four integers xx, yy, aa, and bb (1x,y,a,b1091 \le x, y, a, b \le 10^9).

Output Format: For each test case, output one number — the maximum number of gift sets that Polycarp can make.

Sample Cases

Case 1

Input

9
10 12 2 5
1 1 2 2
52 311 13 27
1000000000 1000000000 1 1
1000000000 1 1 1000000000
1 1000000000 1000000000 1
1 2 1 1
7 8 1 2
4 1 2 3

Output

3
0
4
1000000000
1
1
1
5
0

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback