#include <bits/stdc++.h>
struct Node {
  long long l, r;
  int k;
  Node(long long l = 0, long long r = 0, int k = 0) : l(l), r(r), k(k) {}
  bool operator<(const Node& other) const {
    long long v0 = l - r, v1 = other.l - other.r;
    if (v0 != v1)
      return v0 < v1;
    else
      return k < other.k;
  }
};
struct rNode {
  long long l, r;
  int k;
  rNode(long long l = 0, long long r = 0, int k = 0) : l(l), r(r), k(k) {}
  bool operator<(const rNode& other) const {
    long long v0 = r - l, v1 = other.r - other.l;
    if (v0 != v1)
      return v0 < v1;
    else
      return k > other.k;
  }
};
Node a[200005];
rNode b[200005];
int cmp0(Node a, Node b) { return a.k < b.k; }
int cmp1(rNode a, rNode b) { return a.k > b.k; }
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    int n;
    scanf("%d", &n);
    int sizeA = 0, sizeB = 0;
    for (int i = 1; i <= n; i++) {
      int k;
      long long l, r;
      scanf("%d%lld%lld", &k, &l, &r);
      if (l >= r)
        a[++sizeA] = Node(l, r, k);
      else
        b[++sizeB] = rNode(l, r, k);
    }
    std::sort(a + 1, a + 1 + sizeA, cmp0);
    std::sort(b + 1, b + 1 + sizeB, cmp1);
    std::priority_queue<rNode> QQ;
    long long ans = 0;
    int p = sizeB;
    for (int i = sizeB + 1; i <= n + 1; i++) {
      while (p >= 1 && b[p].k < i) {
        QQ.push(b[p]);
        p--;
      }
      if (QQ.size() != 0) {
        rNode u = QQ.top();
        QQ.pop();
        if (i != n + 1)
          ans += u.r;
        else
          ans += u.l;
      }
    }
    while (QQ.size() != 0) {
      rNode u = QQ.top();
      QQ.pop();
      ans += u.l;
    }
    p = sizeA;
    std::priority_queue<Node> Q;
    for (int i = sizeA; i >= 1; i--) {
      while (p >= 1 && a[p].k >= i) {
        Q.push(a[p]);
        p--;
      }
      if (Q.size() != 0) {
        Node u = Q.top();
        Q.pop();
        ans += u.l;
      }
    }
    while (Q.size() != 0) {
      Node u = Q.top();
      Q.pop();
      ans += u.r;
    }
    printf("%lld\n", ans);
  }
  return 0;
}
