#P1860B. Fancy Coins

    ID: 9968 Type: RemoteJudge 2000ms 256MiB Tried: 0 Accepted: 0 Difficulty: (None) Uploaded By: Tags>binary searchbrute forcegreedymath

Fancy Coins

Description

Monocarp is going to make a purchase with cost of exactly mm burles.

He has two types of coins, in the following quantities:

  • coins worth 11 burle: a1a_1 regular coins and infinitely many fancy coins;
  • coins worth kk burles: aka_k regular coins and infinitely many fancy coins.

Monocarp wants to make his purchase in such a way that there's no change — the total worth of provided coins is exactly mm. He can use both regular and fancy coins. However, he wants to spend as little fancy coins as possible.

What's the smallest total number of fancy coins he can use to make a purchase?

The first line contains a single integer tt (1t31041 \le t \le 3 \cdot 10^4) — the number of testcases.

The only line of each testcase contains four integers m,k,a1m, k, a_1 and aka_k (1m1081 \le m \le 10^8; 2k1082 \le k \le 10^8; 0a1,ak1080 \le a_1, a_k \le 10^8) — the cost of the purchase, the worth of the second type of coin and the amounts of regular coins of both types, respectively.

For each testcase, print a single integer — the smallest total number of fancy coins Monocarp can use to make a purchase.

Input

The first line contains a single integer tt (1t31041 \le t \le 3 \cdot 10^4) — the number of testcases.

The only line of each testcase contains four integers m,k,a1m, k, a_1 and aka_k (1m1081 \le m \le 10^8; 2k1082 \le k \le 10^8; 0a1,ak1080 \le a_1, a_k \le 10^8) — the cost of the purchase, the worth of the second type of coin and the amounts of regular coins of both types, respectively.

Output

For each testcase, print a single integer — the smallest total number of fancy coins Monocarp can use to make a purchase.

Sample Input 1

4
11 3 0 0
11 3 20 20
11 3 6 1
100000000 2 0 0

Sample Output 1

5
0
1
50000000

Note

In the first testcase, there are no regular coins of either type. Monocarp can use 22 fancy coins worth 11 burle and 33 fancy coins worth 33 (since k=3k=3) burles to get 1111 total burles with 55 total fancy coins.

In the second testcase, Monocarp has a lot of regular coins of both types. He can use 1111 regular coins worth 11 burle, for example. Notice that Monocarp doesn't have to minimize the total number of used coins. That way he uses 00 fancy coins.

In the third testcase, Monocarp can use 55 regular coins worth 11 burle and 11 regular coin worth 33 burles. That will get him to 88 total burles when he needs 1111. So, 11 fancy coin worth 33 burles is enough.