Under construction

Still being built. Things may change

Witcher Potion

Time limit1sMemory limit512 MB

Summary
Given up to 8 potions with energy and toxicity, determine how many identical monsters Geralt can beat given energy, poison, and time rules.
Level

Hard8 of 10

Topics
Brute force, Simulation, Bit manipulation, Greedy
Solved
No attempts yet

Problem

In a mythical world there is a fighting contest. Each contestant faces monsters one at a time and defeats as many as possible before being knocked out. The Witcher tribe has an edge over the others, because a Witcher drinks energy potions that are also poisonous. A potion raises energy at once, and a Witcher is never knocked out while energy remains. The poison limits how often a potion helps: a Witcher may drink at most one bottle right after defeating a monster.

Geralt, a legendary Witcher, enters the tournament with NN potion bottles. Bottle ii has strength EiE_i and toxicity PiP_i. Drinking it raises his energy by EiE_i units, but energy never rises above 100. It also raises the toxicity in his blood by PiP_i units. If blood toxicity reaches 100 Geralt is knocked out, so he never drinks a bottle that would take it that far.

His body clears poison quickly. In TT minutes blood toxicity drops by TT units, and it never falls below 0. That is what lets him safely drink another bottle later on.

All monsters are the same. Defeating one costs KK energy units and takes MM minutes. If Geralt has KK or fewer energy units he cannot defeat a monster, and the contest is over for him.

Geralt starts with 100 energy units and 0 blood toxicity. Analyse the potion data and the monster data, then report the largest number of monsters Geralt can defeat.

Input

The first line contains the number of test cases CC (1≤C≤201 \le C \le 20). Each test case is given on four lines.

  • Line 1: KK and MM, the energy and the time Geralt spends on each monster (10≤K≤8010 \le K \le 80, 1≤M≤1001 \le M \le 100)
  • Line 2: NN, the number of potion bottles Geralt carries (1≤N≤81 \le N \le 8)
  • Line 3: E1,E2,…,ENE_1, E_2, \dots, E_N, the strength of bottles 1 to NN (1≤Ei≤1001 \le E_i \le 100)
  • Line 4: P1,P2,…,PNP_1, P_2, \dots, P_N, the toxicity of bottles 1 to NN (1≤Pi≤1001 \le P_i \le 100)

Output

For each test case print the largest number of monsters Geralt can defeat, one line per case.

Notes

Geralt may skip the potion after defeating a monster even when bottles are left in his inventory. Each bottle is drunk at most once. When no bottle is left, or when he chooses not to drink, he fights on with the energy he still has.

One fight follows a fixed order. He spends KK energy and MM minutes to defeat the monster, his blood toxicity drops by MM units (never below 0), and only after that may he drink one bottle.

Examples2

  1. Example 1

    Input
    4
    30 20
    6
    30 30 50 100 40 50
    10 30 80 90 50 60
    20 30
    6
    10 30 80 90 50 60
    30 30 50 100 40 50
    50 30
    8
    10 60 70 20 70 50 60 40
    15 40 50 30 60 70 40 75
    30 50
    8
    15 40 50 30 60 70 40 75
    10 60 70 20 70 50 60 40
    
    Expected output
    10
    16
    7
    15
    
  2. Example 2

    Input
    1
    10 1
    1
    1
    1
    
    Expected output
    10