#include <bits/stdc++.h>
using namespace std;
struct ele {
  int k, l, r;
  bool operator<(const ele& other) const {
    return abs(l - r) < abs(other.l - other.r);
  }
};
bool cmp(const ele& x, const ele& y) { return x.k < y.k; }
int main() {
  cin.tie(nullptr);
  cout.tie(nullptr);
  ios_base::sync_with_stdio(false);
  int t, n;
  cin >> t;
  while (t--) {
    cin >> n;
    vector<ele> big, small;
    long long ans = 0;
    for (int i = 0; i < n; i++) {
      ele tmp;
      cin >> tmp.k >> tmp.l >> tmp.r;
      if (tmp.k < n && tmp.l != tmp.r) {
        if (tmp.l > tmp.r)
          big.push_back(tmp);
        else {
          tmp.k = n - tmp.k;
          small.push_back(tmp);
        }
      } else
        ans += tmp.l;
    }
    sort(big.begin(), big.end(), cmp);
    priority_queue<ele> q;
    for (ele& x : big) {
      q.push(x);
      if (x.k < q.size()) {
        small.push_back(q.top());
        q.pop();
      }
    }
    while (!q.empty()) {
      ans += q.top().l;
      q.pop();
    }
    sort(small.begin(), small.end(), cmp);
    for (ele& x : small) {
      q.push(x);
      if (x.k < q.size()) {
        ans += q.top().l;
        q.pop();
      }
    }
    while (!q.empty()) {
      ans += q.top().r;
      q.pop();
    }
    cout << ans << '\n';
  }
}
