#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    long long int k, l, r;
    deque<pair<long long int, pair<long long int, long long int>>> left;
    deque<pair<long long int, pair<long long int, long long int>>> right;
    for (int i = 0; i < n; i++) {
      cin >> k >> l >> r;
      if (l >= r)
        left.push_back({k, {l, r}});
      else
        right.push_back({k, {l, r}});
    }
    sort(left.begin(), left.end());
    long long int ans = 0;
    long long int cnt = 1;
    long long int p = 0;
    while (!left.empty()) {
      if (left.front().first >= cnt) {
        ans += left.front().second.first;
        cnt++;
      } else {
        ans += left.front().second.second;
        p++;
      }
      left.pop_front();
    }
    cnt += p;
    sort(right.begin(), right.end());
    while (!right.empty()) {
      if (right.front().first >= cnt)
        ans += right.front().second.first;
      else
        ans += right.front().second.second;
      cnt++;
      right.pop_front();
    }
    cout << ans << '\n';
  }
}
