Under construction

Still being built. Things may change

Prof. Pang Earning Aus

Time limit3sMemory limit1024 MB

Summary
Given two stores with balloon and candy stocks and six exchange rates, find the most Au Prof. Pang can earn starting from 1 Au.
Level

Medium6 of 10

Topics
Greedy, Simulation, Math
Solved
No attempts yet

Problem

Prof. Pang has only 11 Au in his pocket. (Yes, Prof. Pang is from Austan, and he uses the currency Au there.)

He makes money using a balloon store and a candy store. In the balloon store, Prof. Pang can pay 11 Au to buy k_abk\_{ab} balloons, or pay 11 candy to buy k_cbk\_{cb} balloons. In the candy store, he can pay 11 Au to buy k_ack\_{ac} candies, or pay 11 balloon to buy k_bck\_{bc} candies. He can also sell one balloon for k_bak\_{ba} Au and sell one candy for k_cak\_{ca} Au. The balloon store has n_bn\_b balloons, and the candy store has n_cn\_c candies. He can buy only as many items as the stores have. Selling items does not increase the number of balloons or candies in the stores.

Each of the six transactions can be performed in any order, any number of times (00 or more). The transactions cannot be split. For example, he cannot pay 1/21/2 Au to buy k_ab/2k\_{ab}/2 balloons.

Find the maximum number of Au he can make.

Input

The first line contains a single integer TT (1≤T≤10001\le T\le 1000), the number of test cases.

Each of the next TT lines contains eight integers n_bn\_b, n_cn\_c, k_abk\_{ab}, k_bak\_{ba}, k_ack\_{ac}, k_cak\_{ca}, k_bck\_{bc}, k_cbk\_{cb} (1≤n_b,n_c≤1091\le n\_b, n\_c\le 10^9, 1≤k_ab,k_ba,k_ac,k_ca,k_bc,k_cb≤1001\le k\_{ab}, k\_{ba}, k\_{ac}, k\_{ca}, k\_{bc}, k\_{cb}\le 100), separated by single spaces.

Output

For each test case, print one line containing the maximum number of Au.

Hint

In the first example, Prof. Pang buys 22 balloons with 11 Au and then sells 22 balloons for 44 Au. Then he buys 22 candies with 11 Au and sells 22 candies for 44 Au.

Examples1

  1. Example 1

    Input
    6
    2 2 2 2 2 2 2 2
    78 74 5 3 10 2 4 7
    31 75 3 6 6 1 8 4
    91 86 4 2 9 5 8 5
    48 89 3 9 2 3 5 7
    13 25 5 7 6 1 2 4
    
    Expected output
    7
    355
    239
    571
    637
    109