import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
class SegmentTree:
    def __init__(self, n, ele, segfun):
        self.ide_ele = ele
        self.segfun = segfun
        self.n = n
        self.N0 = 1 << n.bit_length()
        self.data = [self.ide_ele] * (self.N0 * 2)

    def update_add(self, l, r, val):
        l += self.N0
        r += self.N0
        while l < r:
            if l & 1:
                self.data[l] += val
                l += 1
            if r & 1:
                self.data[r - 1] += val
                r -= 1
            l //= 2
            r //= 2

    def query(self, i):
        i += len(self.data) // 2
        ret = self.data[i]
        while i > 0:
            i //= 2
            ret = self.segfun(ret, self.data[i])
        return ret

n = int(readline())
for _ in range(n):
  t = int(readline())
  chk = []
  for i in range(t):
    k,l,r = map(int,readline().split())
    chk.append((abs(l-r),l,r,k))
  chk.sort(reverse=True)
  S = SegmentTree(t,0,lambda a, b: a+b)
  E = SegmentTree(t,0,lambda a, b: a+b)
  ans = 0
  for c,l,r,k in chk:
    if k == t or l == r:
      ans += l
    elif l > r:
      if S.query(k-1) <= k:
        ans += l
        S.update_add(k-1,t,1)
      else:
        ans += r
    elif r > l:
      if E.query(k) <= t-k-1:
        ans += r
        E.update_add(0,k,1)
      else:
        ans += l
  print(ans)