#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  vector<int> k(n), l(n), r(n);
  for (int i = 0; i < n; i++) {
    cin >> k.at(i) >> l.at(i) >> r.at(i);
  }
  int64_t s = 0;
  for (int i = 0; i < n; i++) {
    s += r.at(i);
    l.at(i) -= r.at(i);
  }
  vector<pair<int, int>> a(n);
  for (int i = 0; i < n; i++) {
    a.at(i) = make_pair(l.at(i), k.at(i));
  }
  sort(a.rbegin(), a.rend());
  int64_t ans = s;
  vector<int> t(n + 1);
  for (int i = 0; i < n; i++) {
    if (a.at(i).first < 0) break;
    int k_i = a.at(i).second, m = k_i, x = 0;
    while (m) {
      x += t.at(m);
      m -= m & -m;
    }
    if (x >= k_i) {
      continue;
    }
    int j = k_i;
    while (j <= n) {
      t.at(j)++;
      j += j & -j;
    }
    ans += a.at(i).first;
  }
  int minus = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (a.at(i).first > 0) break;
    if (n - a.at(i).second > minus) {
      minus++;
      continue;
    }
    ans += a.at(i).first;
  }
  cout << ans << "\n";
}
int main() {
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    solve();
  }
}
