import networkx as nx
import sys
input = sys.stdin.readline

T = int(input())
for _ in range(T):
    N = int(input())
    G = nx.MultiDiGraph()
    # s: source t: sink
    # 0 ... N-1: camel
    # N ... 2*N-1: loc1
    # 2*N ... 3*N-1: loc2
    G.add_node('s', demand=-N)
    G.add_node('t', demand=N)
    for i in range(N):
        G.add_edge('s', i, weight=0, capacity=1)
    for i in range(N-1):
        G.add_edge(N+i, N+i+1, weight=0, capacity=i+1)
    for i in range(N-1):
        G.add_edge(2*N+i+1, 2*N+i, weight=0, capacity=N-i-1)
    G.add_edge(2*N-1, 't', weight=0, capacity=N)
    G.add_edge(2*N, 't', weight=0, capacity=N)
    for i in range(N):
        K, L, R = map(int, input().split())
        K -= 1
        G.add_edge(i, N+K, weight=-L, capacity=1)
        if K < N - 1:
            G.add_edge(i, 2*N+K+1, weight=-R, capacity=1)
    print(-nx.min_cost_flow_cost(G))