#include <bits/stdc++.h>
using namespace std;
mt19937_64 mt(time(0));
const int MAXN = 2e5 + 10;
int k[MAXN], a[MAXN], b[MAXN], idx[MAXN];
bool cmp(int i, int j) { return k[i] < k[j]; }
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout.precision(10);
  cout << fixed;
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    long long ret = 0;
    vector<int> idx[2];
    for (int i = 0; i < n; i++) {
      cin >> k[i] >> a[i] >> b[i];
      long long tmp = min(a[i], b[i]);
      ret += tmp;
      a[i] -= tmp;
      b[i] -= tmp;
      if (a[i] > b[i])
        idx[0].push_back(i);
      else if (b[i] > a[i])
        idx[1].push_back(i);
    }
    {
      sort(idx[0].begin(), idx[0].end(), cmp);
      priority_queue<int, vector<int>, greater<int>> pq;
      for (int i : idx[0]) {
        if (pq.size() < k[i]) {
          pq.push(a[i]);
          ret += a[i];
        } else {
          if (pq.top() < a[i]) {
            ret -= pq.top();
            pq.pop();
            pq.push(a[i]);
            ret += a[i];
          }
        }
      }
    }
    {
      sort(idx[1].begin(), idx[1].end(), cmp);
      reverse(idx[1].begin(), idx[1].end());
      priority_queue<int, vector<int>, greater<int>> pq;
      for (int i : idx[1]) {
        int tmp = n - k[i];
        if (tmp == 0) continue;
        if (pq.size() < tmp) {
          ret += b[i];
          pq.push(b[i]);
        } else if (pq.top() < b[i]) {
          ret -= pq.top();
          ret += b[i];
          pq.push(b[i]);
        }
      }
    }
    cout << ret << '\n';
  }
  return 0;
}
