#include <bits/stdc++.h>
using namespace std;
bool isrange(int second, int first, int n, int m) {
  if (0 <= second && second < n && 0 <= first && first < m) return true;
  return false;
}
int dy[4] = {1, 0, -1, 0}, dx[4] = {0, 1, 0, -1},
    ddy[8] = {1, 0, -1, 0, 1, 1, -1, -1}, ddx[8] = {0, 1, 0, -1, 1, -1, 1, -1};
struct inf {
  int k;
  long long int l, r;
};
bool tmr(inf a, inf b) { return a.k < b.k; }
vector<inf> a, b;
int main(void) {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n;
    scanf("%d", &n);
    a.clear();
    b.clear();
    long long int ans = 0;
    for (int e = 0; e < n; e++) {
      int aa;
      long long int bb, c;
      scanf("%d%lld%lld", &aa, &bb, &c);
      if (bb == c)
        ans += bb;
      else if (bb > c) {
        inf now = {aa, bb, c};
        a.push_back(now);
      } else {
        if (n - aa == 0)
          ans += bb;
        else {
          inf now2 = {n - aa, c, bb};
          b.push_back(now2);
        }
      }
    }
    sort(a.begin(), a.end(), tmr);
    sort(b.begin(), b.end(), tmr);
    priority_queue<long long int> p, q;
    for (int e = 0; e < (int)a.size(); e++) {
      if ((int)p.size() < a[e].k) {
        ans += a[e].l;
        p.push(-a[e].l + a[e].r);
      } else {
        long long int tt = p.top();
        if (a[e].l > -tt) {
          p.pop();
          ans += (tt + a[e].l);
          p.push(-a[e].l + a[e].r);
        }
      }
    }
    for (int e = 0; e < (int)b.size(); e++) {
      if ((int)q.size() < b[e].k) {
        ans += b[e].l;
        q.push(-b[e].l + b[e].r);
      } else {
        long long int tt = q.top();
        if (b[e].l > -tt) {
          q.pop();
          ans += (tt + b[e].l);
          q.push(-b[e].l + b[e].r);
        }
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}
