String Imbalance

Time limit: 1s Memory limit: 64MB Input: Output:

Bogdan is giving Carlo orthography lessons! The course will be held in QQ days, during which Carlo will have to copy the string written on the blackboard as an exercise. The string is initially empty, and each day ii (0i<Q0 \le i < Q) will be extended by Bodgan with FiF_i copies of the character CiC_i that is the lesson's topic. More formally, the exercise string SiS_i will be as follows:

  • S0S_0 consists of F0F_0 characters equal to C0C_0;
  • SiS_i is the concatenation of Si1S_{i-1} and FiF_i characters equal to CiC_i.

Carol practising.\text{Carol practising.}

Carlo is very prone to mistakes and on day ii he will get up to KiK_i letters wrong. Thus, the string he will write on that day will differ from SiS_i in at most KiK_i positions. Bogdan would like to know the minimum possible imbalance of the strings Carlo will write. We define the imbalance of a string of length NN as the number of pair of indices (i,j)(i, j) such that 0i<j<N0 \le i < j < N and SiSjS_i \neq S_j.

Input data

The first line of the input file contains a single integer TT, the number of test cases. TT test cases follow, each preceded by an empty line. Each test case consists of:

  • a line containing integer QQ.
  • QQ lines, the ii-th of which consisting of integer FiF_{i}; character CiC_{i} and integer KiK_{i}.

Output data

For each query from each testcase, output a single line containing one integer denoting the answer for that query.

Constraints and clarifications

  • 1T101 \le T \le 10.
  • 1Q200 0001 \le Q \le 200 \ 000.
  • 1Fi5 0001 \le F_i \le 5 \ 000.
  • 1Ki1091 \le K_i \le 10^9.
  • CiC_i is a lowercase or uppercase letter of the English alphabet.
  • The sum of QQ over all test cases does not exceed 200 000200 \ 000.
# Points Constraints
1 0 Examples.
2 20 Q200Q \le 200 and Fi=1,Ki10F_i = 1, K_i \le 10 for every 0i<Q0 \le i < Q.
3 20 Ci=C_i = a or Ci=C_i = b for every 0i<Q0 \le i < Q.
4 30 CiC_i is a lowercase letter for every 0i<Q0 \le i < Q.
5 30 No additional limitations.

Examples

stdin

2
3
2 a 0
3 b 3
2 c 2
2
2 A 10
3 a 1

stdout

0
0
10
0
4

Explanation

In the first testcase of the sample case:

  • S0S_0 = aa. Its imbalance is 00 and it is possible for Carlo to copy it correctly.
  • S1S_1 = aabbb. It is possible for Carlo to write aaaaa, which has an imbalance of 00.
  • S2S_2 = aabbbcc. Its imbalance is 1616. It is possible for Carlo to write aabbbbb, which has an imbalance of 1010. He cannot write a string with a lower imbalance with up to 22 mistakes.

Log in or sign up to be able to send submissions!