#include <bits/stdc++.h>
using namespace std;
using in = int64_t;
const double PI = 3.14159265358979323846;
const in MOD = 1000000007;
const in INF = 1e18 + 7;
const in inf = 1e9 + 7;
using P = pair<in, in>;
vector<in> dx = {0, 1, -1, 0};
vector<in> dy = {1, 0, 0, -1};
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(10);
  in t;
  cin >> t;
  for (in vbuhsbvh = 0; vbuhsbvh < (in)(t); vbuhsbvh++) {
    in n;
    cin >> n;
    in sum = 0;
    vector<in> K(n), L(n), R(n);
    for (in i = 0; i < (in)(n); i++) cin >> K[i] >> L[i] >> R[i];
    for (in i = 0; i < (in)(n); i++) K[i]--;
    for (in i = 0; i < (in)(n); i++) sum += R[i];
    vector<P> sa(n);
    for (in i = 0; i < (in)(n); i++) sa[i] = P(L[i] - R[i], K[i]);
    sort(sa.begin(), sa.end());
    reverse(sa.begin(), sa.end());
    vector<in> full(n, -1);
    in bord = 0;
    for (in i = 0; i < (in)(n); i++) {
      for (in j = sa[i].second; j >= -1; j--) {
        if (j == -1) {
          sum -= sa[i].second;
          break;
        }
        sum += sa[i].second;
        break;
      }
    }
    cout << sum << endl;
  }
}
