#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
using namespace std;
template <typename T>
void etch(T V) {
  for (auto x : V) cout << x << ' ';
  cout << '\n';
}
vector<string> vec_splitter(string s) {
  s += ',';
  vector<string> res;
  while (!s.empty()) {
    res.push_back(s.substr(0, s.find(',')));
    s = s.substr(s.find(',') + 1);
  }
  return res;
}
void debug_out(vector<string> __attribute__((unused)) args,
               __attribute__((unused)) long long int idx,
               __attribute__((unused)) long long int LINE_NUM) {
  cerr << endl;
}
template <typename Head, typename... Tail>
void debug_out(vector<string> args, long long int idx, long long int LINE_NUM,
               Head H, Tail... T) {
  if (idx > 0)
    cerr << ", ";
  else
    cerr << "Line(" << LINE_NUM << ") ";
  stringstream ss;
  ss << H;
  cerr << args[idx] << " = " << ss.str();
  debug_out(args, idx + 1, LINE_NUM, T...);
}
const long long int MOD = 1e9 + 7;
const long long int MOD1 = 998244353;
const long long int N = 2e5 + 5;
const long long int INF = 1000111000111000111LL;
const long double EPS = 1e-12;
const long double PI = 3.141592653589793116;
struct pt {
  long long int ki;
  long long int profit;
};
bool comp(pt &a, pt &b) { return a.profit > b.profit; }
struct FenwickTree {
  vector<long long int> bit;
  long long int n;
  FenwickTree(long long int n) {
    this->n = n;
    bit.assign(n, 0);
  }
  FenwickTree(vector<long long int> a) : FenwickTree(a.size()) {
    for (size_t i = 0; i < a.size(); i++) add(i, a[i]);
  }
  long long int sum(long long int r) {
    long long int ret = 0;
    for (; r >= 0; r = (r & (r + 1)) - 1) ret += bit[r];
    return ret;
  }
  long long int sum(long long int l, long long int r) {
    return sum(r) - sum(l - 1);
  }
  void add(long long int idx, long long int delta) {
    for (; idx < n; idx = idx | (idx + 1)) bit[idx] += delta;
  }
};
long long int solve() {
  long long int n;
  cin >> n;
  long long int ans = 0;
  std::vector<pt> v(n);
  set<long long int> vis;
  for (long long int i = (0); i < (n); i++) {
    long long int l, r, k;
    cin >> k >> l >> r;
    v[i].profit = l - r;
    ans += r;
    v[i].ki = k;
  }
  sort((v).begin(), (v).end(), comp);
  vector<long long int> tmp(n + 5, 0);
  FenwickTree ft(tmp);
  long long int cnt_b = 0;
  for (long long int i = 0; i < n; i++) {
    if (v[i].profit > 0) {
      if (ft.sum(1, v[i].ki) < v[i].ki) {
        ft.add(v[i].ki, 1);
        ans += v[i].profit;
      }
    } else if (v[i].profit < 0) {
      if (n - cnt_b > v[i].ki) {
        cnt_b++;
      } else {
        ans += v[i].profit;
      }
    }
  }
  return cout << ans << '\n', 0;
}
int32_t main() {
  std::ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int T;
  cin >> T;
  while (T--) {
    solve();
  }
  return 0;
}
