#K49417. Total Power Calculation with Magic Subsequence

    ID: 28638 Type: Default 1000ms 256MiB

Total Power Calculation with Magic Subsequence

Total Power Calculation with Magic Subsequence

You are given a deck of cards, where each card has a certain power value. A player’s total power is defined as the sum of all card powers. However, if a specific contiguous subsequence, called the magic subsequence, occurs in the deck, the total power is multiplied by 2 for each occurrence.

Formally, let \(S\) be the sum of all card powers. Let \(k\) be the number of times that the magic subsequence appears in the given order consecutively in the deck. Then, the final total power is computed as:

[ \text{Total Power} = S \times 2^{k} ]

Your task is to compute the final total power for each test case.

inputFormat

The input is read from standard input (stdin) and has the following format:

T
N
card_1 card_2 ... card_N
M
magic_1 magic_2 ... magic_M
... (repeated for T test cases)

Here:

  • \(T\) is the number of test cases.
  • For each test case, \(N\) is the number of cards.
  • The next line contains \(N\) integers, representing the powers of the cards.
  • \(M\) is the length of the magic subsequence.
  • The next line contains \(M\) integers, representing the magic subsequence.

outputFormat

For each test case, output the final total power on a separate line to standard output (stdout).

## sample
1
5
3 6 3 2 1
2
3 6
30

</p>