Under construction

Still being built. Things may change

Co-workers from Hell

Time limit1sMemory limit128 MB

Summary
Place tricks in chambers so the watchman's walk, where each trick is used once to change dwell time and jump target, maximizes total time before he normally finishes chamber n.
Level

Medium7 of 10

Topics
Dynamic programming, Graph, DFS, Implementation
Solved
No attempts yet

Statement

Every night a watchman inspects the chambers of a factory in a fixed order. He starts at chamber 11 and, after inspecting the final chamber nn, leaves the factory and goes home. In a normal inspection he stays in chamber ii for did_i units of time and then moves on to chamber i+1i+1.

A mischievous co-worker who has seen the schedule wants to keep the watchman at work as long as possible. Before the watchman arrives, the co-worker may place a trick in any subset of the chambers. When the watchman inspects a chamber that holds a trick, he is fooled: he stays there tditd_i units of time instead of did_i, and afterwards moves to chamber tcitc_i (which may be any chamber) instead of chamber i+1i+1.

A trick works only the first time the watchman enters that chamber; on every later visit to it he behaves normally (stays did_i and moves to chamber i+1i+1). A trick may send him backward, so he can pass through some chambers several times, or forward, so he may skip some chambers entirely. For instance, with five chambers inspected in order, if the only trick is placed in chamber 22 and points to chamber 44, the watchman walks 1 → 2 → 4 → 5 and then goes home, never checking chamber 33.

The watchman goes home only when he performs a normal inspection of the final chamber nn. Determine the maximum total time the co-worker can force the watchman to stay in the factory.

Input

The first line contains the number of test cases TT.

Each test case starts with a line containing an integer nn (0 ≤ nn ≤ 100), the number of chambers. The next nn lines describe the chambers in order; the ii-th line contains three integers did_i, tditd_i, and tcitc_i (1 ≤ tcitc_i ≤ nn): the normal inspection time in chamber ii, the inspection time when chamber ii is tricked, and the chamber the watchman moves to when chamber ii is tricked. Chamber 11 is always the first chamber and chamber nn is always the final one.

Output

For each test case, print a single line with one integer: the maximum total time the watchman can be forced to remain in the factory before he leaves the final chamber by a normal inspection.

Examples3

  1. Example 1

    Input
    1
    5
    1 2 2
    1 2 4
    1 1 4
    1 2 5
    1 2 4
    
    Expected output
    10
    
  2. Example 2

    Input
    1
    1
    3 7 1
    
    Expected output
    10
    
  3. Example 3

    Input
    1
    0
    
    Expected output
    0