This page is still under construction.

Parts of this page are still being built. What you see may change.

A Lazy Worker

Time limit1sMemory limit128 MB

Summary
Jobs have processing times with arrival times and deadlines, and the worker picks among available jobs without idling to minimize total executed work.
Level

Hard8 of 10

Topics
Dynamic programming, Sorting
Solved
No attempts yet

Problem

There is a lazy worker who wants to do as little work as possible. He is under one constraint: whenever there is a job he is able to work on, he must be busy working.

There are jobs 1,2,…,n1, 2, \dots, n, and job ii has processing time tit_i. Job ii arrives at time aia_i and has deadline did_i, where tit_i, aia_i, and did_i are nonnegative integers. Every job has a hard deadline: job ii may only be executed inside its allowed interval Ii=[ai,di]I_i = [a_i, d_i], so it must start no earlier than aia_i and finish no later than did_i.

The worker handles only one job at a time, and once a job is started it runs to completion without interruption. When a job finishes, the worker must immediately begin another job if one is available. If no job is available, the worker stays idle and starts a job as soon as one becomes available.

For every job ii the interval length di−aid_i - a_i is at least tit_i and strictly less than 2ti2 t_i.

The worker may choose which of the available jobs to run next, and different choices may cause some jobs to miss their deadlines and never be executed. Write a program that finds the minimum possible total time the worker spends working, that is, the minimum possible sum of processing times over the jobs he actually executes.

Input

The input consists of TT test cases. The first line contains the number of test cases TT.

The first line of each test case contains the number of jobs nn (0≤n≤1000 \le n \le 100). Each of the next nn lines contains three integers: the processing time tit_i, the arrival time aia_i, and the deadline did_i of one job. The values satisfy 1≤ti1 \le t_i, 0≤ai≤2500 \le a_i \le 250, 1≤di≤2501 \le d_i \le 250, and each job satisfies ti≤di−ai<2tit_i \le d_i - a_i < 2 t_i.

Output

For each test case, print exactly one line containing the minimum total amount of time the worker spends working.

Examples2

  1. Example 1

    Input
    3
    3
    15 0 25
    50 0 90
    45 15 70
    3
    15 5 20
    15 25 40
    15 45 60
    5
    3 3 6
    3 6 10
    3 14 19
    6 7 16
    4 4 11
    
    Expected output
    50
    45
    15
    
  2. Example 2

    Input
    1
    1
    10 0 15
    
    Expected output
    10