#include <bits/stdc++.h>
using namespace std;
struct camel {
  long long k, l, r;
  camel(long long _k, long long _l, long long _r) {
    k = _k;
    l = _l;
    r = _r;
  }
};
struct dif {
  bool operator()(camel const& a, camel const& b) {
    return a.l - a.r > b.l - b.r;
  }
};
bool comp(camel a, camel b) { return a.k > b.k; }
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long test;
  cin >> test;
  while (test--) {
    long long n;
    cin >> n;
    vector<camel> left, right;
    long long a, b, c, ans = 0;
    for (__typeof(n) i = 0; i < n; i++) {
      cin >> a >> b >> c;
      if (b > c)
        left.push_back(camel(a, b, c));
      else {
        if (n - a == 0)
          ans += b;
        else
          right.push_back(camel(n - a, c, b));
      }
    }
    sort(left.begin(), left.end(), comp);
    sort(right.begin(), right.end(), comp);
    long long pos, cur;
    pos = 0, cur = 1;
    priority_queue<camel, vector<camel>, dif> pq;
    while (pos < (long long)left.size()) {
      if (left[pos].k > cur)
        cur++;
      else {
        pq.push(left[pos]);
        if ((long long)pq.size() > min(cur, (long long)left.size())) {
          ans += pq.top().r;
          pq.pop();
        }
        pos++;
      }
    }
    while (!pq.empty()) {
      ans += pq.top().l;
      pq.pop();
    }
    pos = 0, cur = 1;
    while (pos < (long long)right.size()) {
      if (right[pos].k > cur)
        cur++;
      else {
        pq.push(right[pos]);
        if ((long long)pq.size() > min(cur, (long long)right.size())) {
          ans += pq.top().r;
          pq.pop();
        }
        pos++;
      }
    }
    while (!pq.empty()) {
      ans += pq.top().l;
      pq.pop();
    }
    cout << ans << endl;
  }
  return 0;
}
