#P1787H. Codeforces Scoreboard

    ID: 8203 Type: RemoteJudge 3000ms 512MiB Tried: 0 Accepted: 0 Difficulty: 10 Uploaded By: Tags>binary searchdata structuresdpgeometry*3300

Codeforces Scoreboard

No submission language available for this problem.

Description

You are participating in a Codeforces Round with nn problems.

You spend exactly one minute to solve each problem, the time it takes to submit a problem can be ignored. You can only solve at most one problem at any time. The contest starts at time 00, so you can make your first submission at any time t1t \ge 1 minutes. Whenever you submit a problem, it is always accepted.

The scoring of the ii-th problem can be represented by three integers kik_i, bib_i, and aia_i. If you solve it at time tt minutes, you get max(bikit,ai)\max(b_i - k_i \cdot t,a_i) points.

Your task is to choose an order to solve all these nn problems to get the maximum possible score. You can assume the contest is long enough to solve all problems.

Each test contains multiple test cases. The first line contains an integer tt (1t1041 \le t \le 10^4) — the number of test cases.

The first line of each test case contains one integer nn (1n21051 \le n \le 2 \cdot 10^5) — the number of problems.

The nn lines follow, the ii-th of them contains three integers kik_i, bib_i, aia_i (1ki,bi,ai1091\le k_i,b_i,a_i\le 10^9; ai<bia_i < b_i), denoting that you get the score of max(bikit,ai)\max(b_i - k_i \cdot t,a_i) if you solve the ii-th task at time tt minutes.

It's guaranteed that the sum of nn does not exceed 21052 \cdot 10^5.

For each test case, print a line containing a single integer — the maximum score you can get.

Input

Each test contains multiple test cases. The first line contains an integer tt (1t1041 \le t \le 10^4) — the number of test cases.

The first line of each test case contains one integer nn (1n21051 \le n \le 2 \cdot 10^5) — the number of problems.

The nn lines follow, the ii-th of them contains three integers kik_i, bib_i, aia_i (1ki,bi,ai1091\le k_i,b_i,a_i\le 10^9; ai<bia_i < b_i), denoting that you get the score of max(bikit,ai)\max(b_i - k_i \cdot t,a_i) if you solve the ii-th task at time tt minutes.

It's guaranteed that the sum of nn does not exceed 21052 \cdot 10^5.

Output

For each test case, print a line containing a single integer — the maximum score you can get.

Sample Input 1

4
4
10000 1000000000 2006
10000 1000000000 9999
2 999991010 1010
1000000000 1000000000 999999999
6
1 8 1
9 29 4
2 14 3
4 13 1
2 19 5
10 12 5
8
4 10 1
4 19 8
1 14 3
4 15 6
2 9 6
1 11 10
2 19 12
4 19 14
10
5 12 7
5 39 12
2 39 11
3 23 15
5 30 11
3 17 13
5 29 14
3 17 11
3 36 18
3 9 8

Sample Output 1

3999961003
53
78
180

Note

In the second test case, the points for all problems at each minute are listed below.

Time112233445566
Problem 117766554\color{red}{4}3322
Problem 2220\color{red}{20}111144444444
Problem 33121210108\color{red}{8}664433
Problem 44995511111\color{red}{1}11
Problem 55171715\color{red}{15}131311119977
Problem 6655555555555\color{red}{5}

The points displayed in red denote one of the optimal orders with the score 5353.