#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int t, n, k[N], l[N], r[N];
priority_queue<pair<int, int>, vector<pair<int, int> >,
               greater<pair<int, int> > >
    q;
int a[N], cnta;
long long ans = 0;
bool cmp1(int x, int y) { return k[x] < k[y]; }
bool cmp2(int x, int y) { return k[x] > k[y]; }
int main() {
  cin >> t;
  while (t--) {
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> k[i] >> l[i] >> r[i];
    cnta = 0;
    for (int i = 1; i <= n; i++)
      if (l[i] > r[i]) a[++cnta] = i;
    sort(a + 1, a + cnta + 1, cmp1);
    for (int i = 1; i <= cnta; i++) {
      int now = a[i];
      if (q.size() < k[now])
        q.push(pair<int, int>(l[now], now)), ans += l[now];
      else {
        if (q.empty()) {
          ans += r[now];
          continue;
        }
        int x = q.top().second;
        q.pop();
        ans -= l[x];
        if (l[x] > l[now])
          q.push(pair<int, int>(l[x], x)), ans += l[x] + r[now];
        else
          q.push(pair<int, int>(l[now], now)), ans += r[x] + l[now];
      }
    }
    while (!q.empty()) q.pop();
    for (int i = 1; i <= n; i++)
      if (l[i] == r[i]) ans += l[i];
    cnta = 0;
    for (int i = 1; i <= n; i++)
      if (l[i] < r[i]) a[++cnta] = i;
    sort(a + 1, a + cnta + 1, cmp2);
    for (int i = 1; i <= cnta; i++) {
      int now = a[i];
      if (q.size() < n - k[now])
        q.push(pair<int, int>(r[now], now)), ans += r[now];
      else {
        if (q.empty()) {
          ans += l[now];
          continue;
        }
        int x = q.top().second;
        q.pop();
        ans -= r[x];
        if (r[x] > r[now])
          q.push(pair<int, int>(r[x], x)), ans += r[x] + l[now];
        else
          q.push(pair<int, int>(r[now], now)), ans += l[x] + r[now];
      }
    }
    while (!q.empty()) q.pop();
    cout << ans << endl;
    ans = 0;
  }
  return 0;
}
