INF = 10 ** 18
t = int(input())
for _ in range(t):
    n = int(input())
    li = [list(map(int, input().split())) for _ in range(n)]
    li.sort()
    ans = 0
    k, a = [0] * n, [0] * n
    for i in range(n):
        k[i], l, r = li[i]
        ans += r
        a[i] = l - r
    dp = [[-INF] * (n + 1) for _ in range(n + 1)]
    dp[0][0] = 0
    for i in range(n):
        for j in range(n):
            if k[i] > j:
                dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + a[i])
            if k[i] != n:
                dp[i + 1][j] = max(dp[i + 1][j], dp[i][j])
    print(max(dp[n]) + ans)
