760. Monu bhaiya and Baseball

0

Easy

You are keeping score for a baseball game with strange rules. The game consists of several rounds, where the scores of past rounds may affect future rounds' scores.
At the beginning of the game, you start with an empty record. You are given a list of strings ops, where ops[i] is the ith operation you must apply to the record and is one of the following:
1. An integer x - Record a new score of x.
2. "+" - Record a new score that is the sum of the previous two scores. It is guaranteed there will always be two previous scores.
3. "D" - Record a new score that is double the previous score. It is guaranteed there will always be a previous score.
4. "C" - Invalidate the previous score, removing it from the record. It is guaranteed there will always be a previous score.
Return the sum of all the scores on the record.

Input Format

First line contains T - no of testcases.

Each testcase contains two lines each - first line containing integer N and second line containing N elements of the array in space separated format.

Each testcase contains two lines each - first line containing integer N and second line containing N elements of the array in space separated format.

Output Format

For each testcase, print the sum of all the scores on the record

Example

Input

1
5
5 2 C D +

Output

30

Constraints

1 <= T <= 1000

1 <= N <= 1000

ops[i] is "C", "D", "+", or a string representing an integer in the range [-3 * 10

For operation "+", there will always be at least two previous scores on the record.

For operations "C" and "D", there will always be at least one previous score on the record.

1 <= N <= 1000

ops[i] is "C", "D", "+", or a string representing an integer in the range [-3 * 10

^{4}, 3 * 10^{4}]For operation "+", there will always be at least two previous scores on the record.

For operations "C" and "D", there will always be at least one previous score on the record.

Loading...

View Submissions

Console