#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 100;
int n;
pair<int, int> srt[N];
long long solve(int border) {
  set<int> free;
  for (int i = 1; i <= n; i++) free.insert(i);
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    int add = srt[i].first, pos = srt[i].second;
    if (pos > 0) {
      pos = min(pos, border);
      auto it = free.upper_bound(pos);
      if (it != free.begin()) free.erase(prev(it)), ans += add;
    } else if (pos < 0) {
      pos = -pos + 1;
      pos = max(pos, border + 1);
      auto it = free.lower_bound(pos);
      if (it != free.end()) free.erase(it), ans += add;
    }
  }
  return ans;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    cin >> n;
    long long start = 0;
    for (int i = 1; i <= n; i++) {
      int k, l, r;
      cin >> k >> l >> r;
      int mn = min(l, r);
      start += mn;
      l -= mn, r -= mn;
      if (l > 0)
        srt[i] = {l, k};
      else
        srt[i] = {r, -k};
    }
    sort(srt + 1, srt + n + 1, greater<pair<int, int> >());
    int lo = 0, hi = n;
    long long ans = 0;
    while (lo <= hi) {
      int mi1 = (lo + hi) / 2;
      int mi2 = min(mi1 + 1, hi);
      long long ans1 = solve(mi1);
      long long ans2 = solve(mi2);
      ans = max({ans, ans1, ans2});
      if (ans1 > ans2)
        hi = mi2 - 1;
      else
        lo = mi1 + 1;
    }
    cout << start + ans << "\n";
  }
}
