시간 제한 메모리 제한 제출 정답 맞은 사람 정답 비율
5 초 512 MB 0 0 0 0.000%

문제

Misaka likes to shoot coins as a powerful railgun.

She prepares a line of coins to fight crime. To produce a stronger coin, she mixes coins together. However, coins with different materials are not compatible to each other, so she only mixes coins with same material together.

Here’s the steps Misaka makes coins:

  1. Find first three consecutive coins with same material from the beginning of the line
  2. Take them out from the line
  3. Mix together and produce a new coin with same material
  4. Put the new coin at the end of line

She repeatedly do these steps until she can’t produce new coins anymore.

Misaka wants to know how many coins she will have. Please help her count coins rapidly!

입력

On the first line there is a single integer T indicating the number of test cases.

The first line of each test case contains an integer N indicating the number of groups of consecutive coins Misaka has. All coins are in a single line.

Then N lines follow, each line containing a character ci and an integer ni, denoting that there are ni consecutive coins with material ci for i-th group of consecutive coins, behind (i − 1)-th.

You may assume:

  • T ≤ 10
  • 1 ≤ N ≤ 105
  • 1 ≤ ni ≤ 109
  • ci is an uppercase alphabet, ci ≠ ci+1 for 1 ≤ i < N

출력

For each test case, output an integer in one line, indicating the number of coins after Misaka doing the steps of making coins as many as possible.

예제 입력

2
3
A 3
B 1
A 2
3
A 2
B 3
A 2

예제 출력

2
3

힌트