#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int INF = INT_MAX;
const long double pi = 4 * atan((long double)1);
template <class T>
inline T gcd(T a, T b) {
  if (b)
    return gcd(b, a % b);
  else
    return a;
}
template <class T>
inline T lcm(T a, T b) {
  return a / gcd(a, b) * b;
}
bool cmp(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) {
  if (a.first.first == b.first.first) return a.first.second > b.first.second;
  return a.first.first < b.first.first;
}
bool cmp1(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) {
  return a.first.second > b.first.second;
}
signed main() {
  ios::sync_with_stdio();
  cin.tie(0);
  cout.tie(0);
  ;
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<pair<pair<int, int>, int> > arr;
    vector<int> ri;
    for (int i = 1; i <= n; i++) {
      int k, l, r;
      cin >> k >> l >> r;
      arr.push_back({{k, l - r}, i});
      ri.push_back(r);
    }
    priority_queue<pair<pair<int, int>, int>,
                   vector<pair<pair<int, int>, int> >,
                   greater<pair<pair<int, int>, int> > >
        pq;
    sort(arr.begin(), arr.end(), cmp);
    vector<pair<pair<int, int>, int> > wa;
    for (int i = 0; i < n; i++) {
      if (arr[i].first.first > pq.size()) {
        pq.push({{arr[i].first.second, arr[i].first.first}, arr[i].second});
      } else {
        vector<pair<pair<int, int>, int> > luu;
        int rst = arr[i].first.first - pq.size() + 1;
        int sum = 0;
        while (rst--) {
          pair<pair<int, int>, int> temp = pq.top();
          pq.pop();
          sum += temp.first.first;
          luu.push_back({temp});
        }
        if (sum < arr[i].first.second) {
          pq.push({{arr[i].first.second, arr[i].first.first}, arr[i].second});
        } else {
          for (int i = 0; i < luu.size(); i++) pq.push(luu[i]);
        }
      }
    }
    int ans = 0;
    for (int i = 0; i < ri.size(); i++) ans += ri[i];
    int cnt = 0;
    while (!pq.empty()) {
      pair<pair<int, int>, int> temp = pq.top();
      pq.pop();
      if (temp.first.first < 0)
        wa.push_back({{temp.first.second, temp.first.first}, temp.second});
      else {
        cnt++;
        ans += temp.first.first;
      }
    }
    sort(wa.begin(), wa.end(), cmp1);
    for (int i = 0; i < wa.size(); i++) {
      if (wa[i].first.first > cnt) {
        cnt++;
        ans += wa[i].first.second;
      }
    }
    cout << ans << endl;
  }
}
