#include <bits/stdc++.h>
using namespace std;
using llong = long long;
using T = tuple<llong, llong, llong>;
llong solve() {
  llong n;
  priority_queue<T> maxq;
  priority_queue<T> que2;
  llong ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    llong k, l, r;
    cin >> k >> l >> r;
    ans += r;
    T a = T(-k, l - r, l);
    maxq.push(a);
  }
  llong left = 0;
  while (!maxq.empty()) {
    auto a = maxq.top();
    maxq.pop();
    auto [ki, step, x] = a;
    ki *= -1;
    if (left < ki && step > 0) {
      ans += step;
      left++;
    } else {
      T d = T(step, -ki, x);
      que2.push(d);
    }
  }
  while (!que2.empty()) {
    auto a = que2.top();
    que2.pop();
    auto [step, ki, x] = a;
    ki *= -1;
    if (left < ki) {
      ans += step;
      left++;
    }
  }
  return ans;
}
int main() {
  llong t;
  cin >> t;
  while (t--) {
    cout << solve() << '\n';
  }
}
