P14125 [SCCPC 2021] Monster Hunter

Description

Ema is the best carry player in a game. In the game, she needs to eliminate $m$ monsters. The $i$-th monster has $h_i$ health points (HP) at the beginning. When a monster is attacked by Ema, its HP is reduced by her attack power. When the HP of a monster is less than or equal to $0$, the monster is eliminated. To make the game more interesting, the attack power is not a constant number. There is a basic attack sequence $a_1, a_2, \cdots, a_n$, and the damage caused is generated by repeating this sequence. Formally, let $r_i$ be the damage caused by the $i$-th attack, we have $$ r_{i}= \left \{ \begin{array}{ll} a_i & 1 \le i \le n \\ r_{i - n} & i > n \end{array} \right. $$ To eliminate the monsters as soon as possible, Ema wants to minimize the number of attacks. Can you tell her the minimum number of attacks required to eliminate all the monsters?

Input Format

There are multiple test cases. The first line of the input contains an integer $T$ indicating the number of test cases. For each test case: The first line contains an integer $n$ ($1 \le n \le 10^5$) indicating the length of the basic attack sequence. The second line contains $n$ integers $a_1, a_2, \cdots, a_n$ ($1 \le a_i \le 3$) indicating the basic attack sequence. The third line contains an integer $m$ ($1 \le m \le 10^5$) indicating the number of monsters. The fourth line contains $m$ integers $h_1, h_2, \cdots, h_m$ ($1 \le h_i \le 10^9$) where $h_i$ indicates the initial HP of the $i$-th monster. It's guaranteed that neither the sum of $n$ nor the sum of $m$ of all test cases will exceed $10^5$.

Output Format

For each test case output one line containing one integer indicating the minimum number of attacks to eliminate all the monsters.

Explanation/Hint

For the first example, the damage sequence is $3, 2, 3, 2, 3, 2, \cdots$. We can attack monsters 1, 2, 3 and 2 in order to eliminate all the $3$ monsters. For the second example, we can attack monsters 2, 2, 1 in order.