#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 100;
long long n;
pair<long long, long long> srt[N];
long long solve(long long border) {
  set<long long> free;
  for (long long i = 1; i <= n; i++) free.insert(i);
  long long ans = 0;
  for (long long i = 1; i <= n; i++) {
    long long 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);
  long long t;
  cin >> t;
  while (t--) {
    cin >> n;
    long long start = 0;
    for (long long i = 1; i <= n; i++) {
      long long k, l, r;
      cin >> k >> l >> r;
      long long 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<long long, long long> >());
    long long lo = 0, hi = n;
    long long ans = 0;
    while (lo <= hi) {
      long long mi1 = (lo * 2 + hi) / 3;
      long long mi2 = (lo + 2 * hi) / 3;
      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";
  }
}
