#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
using namespace std;
const long long int N = 4e5 + 5;
long long int n;
long long int t[2 * N]{0};
long long int combine(long long int &a, long long int &b) { return a + b; }
void build() {
  for (long long int i = n - 1; i > 0; i--) {
    t[i] = combine(t[i << 1], t[i << 1 | 1]);
  }
}
void modify(long long int p) {
  for (t[p += n]++; p > 1; p >>= 1) {
    t[p >> 1] = combine(t[p], t[p ^ 1]);
  }
}
long long int query(long long int l, long long int r) {
  long long int ret = 0;
  for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
    if (l & 1) ret = combine(ret, t[l++]);
    if (r & 1) ret = combine(t[--r], ret);
  }
  return ret;
}
void pre() {}
void solve() {
  cin >> n;
  long long ans = 0, a, b, c;
  priority_queue<pair<long long int, long long int>> pq;
  for (int i = 0; i < n; i++) {
    t[n + i] = 0;
  }
  build();
  pair<long long int, long long int> arr[n];
  for (int i = 0; i < n; i++) {
    cin >> a >> b >> c;
    arr[i].second = a;
    arr[i].first = b - c;
    pq.push(arr[i]);
    ans += c;
  }
  int curr = 0;
  while (pq.size() > 0 && pq.top().first >= 0) {
    auto q = pq.top();
    pq.pop();
    if (query(0, q.second) < q.second) {
      modify(q.second - 1);
      ans += q.first;
    }
    curr++;
  }
  vector<vector<long long int>> v(n);
  while (pq.size() > 0) {
    auto o = pq.top();
    v[o.second - 1].push_back(o.first);
    pq.pop();
  }
  multiset<long long int> q;
  int pref = n;
  int cnt = 0;
  for (int i = n - 1; i >= 0; --i) {
    pref -= v[i].size();
    int need = i + 1 - pref;
    for (auto x : v[i]) {
      q.insert(-1 * x);
    }
    while (cnt < need) {
      ++cnt;
      ans -= *q.begin();
      q.erase(q.begin());
    }
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  pre();
  long long int num = 1;
  cin >> num;
  for (long long int i = 0; i < num; i++) {
    solve();
  }
}
