Problem: This is the hard version of the problem. The only difference is that in this version, instead of listing the number of petals for each flower, the number of petals and the quantity of flowers in the store is set for all types of flowers.
A girl is preparing for her birthday and wants to buy the most beautiful bouquet. There are a total of different types of flowers in the store, each of which is characterized by the number of petals and the quantity of this type of flower. A flower with petals costs coins. The girl has decided that the difference in the number of petals between any two flowers she will use to decorate her cake should not exceed one. At the same time, the girl wants to assemble a bouquet with the maximum possible number of petals. Unfortunately, she only has coins, and she cannot spend more. What is the maximum total number of petals she can assemble in the bouquet?
Input Format: Each test consists of several test cases. The first line contains a single integer () — the number of test cases. This is followed by descriptions of the test cases.
The first line of each test case contains two integers , () — the number of types of flowers in the store and the number of coins the girl possesses, respectively. The second line of each test case contains different integers (), where is the number of petals of the -th flower type in the store (for different indexes , it must be ). The third line of each test case contains integers (), where is the quantity of the -th flower type in the store.
The sum of over all test cases does not exceed .
Output Format: For each test case, print one integer — the maximum possible number of petals in a bouquet that a girl can collect, observing all the conditions listed above.
Note: In the first test case, some valid bouquets are . The maximum over all valid bouquets not greater than is for . In the second test case, you can assemble a valid bouquet with with a sum of , which is the maximum number of petals the girl can buy. In the third test case, you can assemble a valid bouquet with with a sum of . It can be seen that no valid bouquet can have petals.