class UnionFind:
    def __init__(self, n=1):
        self.parent = [i for i in range(n)]
    
    def find(self, x):
        if self.parent[x] == x:
            return x
        else:
            self.parent[x] = self.find(self.parent[x])
            return self.parent[x]
    
    def union(self, x, y): # x -> y
        x = self.find(x)
        y = self.find(y)
        self.parent[x] = y
    
    def is_same(self, x, y):
        return self.find(x) == self.find(y)


def solve(KLR):
  res = 0
  N = len(KLR)
  left = UnionFind(N+1)
  right = N
  DKLR = [(abs(l-r), k, l, r) for k, l, r in KLR]
  DKLR.sort(reverse=True)
  for _, k, l, r in DKLR:
    if l < r:
      if k < right:
        res += r
      else:
        res += l
      right -= 1
    elif l > r:
      idx = left.find(k)
      if idx:
        res += l
        _idx = left.find(idx-1)
        left.union(idx, _idx)
      else:
        res += r
        right -= 1
    else:
      res += l
  return res


ans = []
T = int(input())
for i in range(T):
  N = int(input())
  KLR = [tuple(map(int, input().split())) for i in range(N)]
  ans.append(solve(KLR))

print(*ans, sep='\n')