#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
const int maxm = 100;
const int inf = 2147483647;
const double eps = 1e-9;
const long long INF = 9223372036854775807ll;
long long qpow(long long a, long long b, long long c) {
  long long ans = 1;
  while (b) {
    if (b & 1) ans = ans * a % c;
    a = a * a % c;
    b >>= 1;
  }
  return ans;
}
struct Node {
  int l, r, d;
  bool operator<(const Node a) const { return abs(r - l) < abs(a.r - a.l); }
} s[maxn];
vector<int> a, b;
bool cmp1(int a, int b) { return s[a].d < s[b].d; }
bool cmp2(int a, int b) { return s[a].d > s[b].d; }
priority_queue<Node> aa, bb;
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n;
    scanf("%d", &n);
    a.clear(), b.clear();
    long long ans = 0;
    for (register int i = (0); i < (n); ++i) {
      scanf("%d%d%d", &s[i].d, &s[i].l, &s[i].r);
      if (s[i].l > s[i].r)
        a.push_back(i);
      else if (s[i].l < s[i].r)
        b.push_back(i);
      else
        ans += s[i].l;
    }
    sort(a.begin(), a.end(), cmp1);
    sort(b.begin(), b.end(), cmp2);
    for (register int i = (0); i < (a.size()); ++i) {
      if ((int)aa.size() == s[a[i]].d) {
        if (abs(aa.top().r - aa.top().l) < abs(s[a[i]].r - s[a[i]].l)) {
          ans += aa.top().r - aa.top().l;
          aa.pop();
          aa.push(s[a[i]]);
          ans += s[a[i]].l;
        } else {
          ans += s[a[i]].r;
        }
      } else if (aa.size() < s[a[i]].d) {
        aa.push(s[a[i]]);
        ans += s[a[i]].l;
      }
    }
    for (register int i = (0); i < (b.size()); ++i) {
      if ((int)bb.size() == n - s[b[i]].d && n - s[b[i]].d) {
        if (abs(bb.top().l - bb.top().r) < abs(s[b[i]].r - s[b[i]].l)) {
          ans += bb.top().l - bb.top().r;
          bb.pop();
          ans += s[b[i]].r;
          bb.push(s[b[i]]);
        } else {
          ans += s[b[i]].l;
        }
      } else if ((int)bb.size() < n - s[b[i]].d) {
        bb.push(s[b[i]]);
        ans += s[b[i]].r;
      } else {
        ans += s[b[i]].l;
      }
    }
    while (!aa.empty()) aa.pop();
    while (!bb.empty()) bb.pop();
    printf("%lld\n", ans);
  }
}
