Hide

Problem B
Bricks

You are given a sequence of white (W) and black (B) bricks. The goal is to partition it into some number of non-empty, contiguous blocks, each one having the same ratio of white and black bricks.

Of course one can always “partition” the sequence into one single block (which is not very interesting). We want, however, to have as many blocks as possible. Consider for example the following sequences and its partitions:

  • $\text {BWWWBB} = \text {BW} + \text {WWBB}$ (ratio $1:1$),

  • $\text {WWWBBBWWWWWWWWWB} = \text {WWWB} + \text {BBWWWWWW} + \text {WWWB}$ (ratio $3:1$).

Note that both of these partitions are optimal with respect to the number of blocks.

Input

The first line of input contains the number of test cases $T$ ($1 \leq T \leq 600$). The descriptions of the test cases follow:

Each test case starts with one line containing an integer $n$ ($1 \leq n \leq 10^5 $) which is the length of the description of a sequence. Each of the following $n$ lines consists of an integer $k$ ($1 \leq k \leq 10^9 $) and one of the characters $W$ or $B$, meaning that $k$ bricks of the given color follow next in the sequence. It is guaranteed that the total length of the brick sequence does not exceed $10^9$.

The total number of lines in all test cases is at most $5\, 000\, 000$.

Output

For each test case, output a single line containing the largest possible number of blocks.

Sample Input 1 Sample Output 1
3
3
1 B
3 W
2 B
4
3 W
3 B
9 W
1 B
2
2 W
3 W
2
3
5

Please log in to submit a solution to this problem

Log in