#include <bits/stdc++.h>
using namespace std;
const int N_MAX = 200002;
int t;
int n;
struct Camel {
  int pos;
  long long x, y;
};
bool operator<(const Camel &a, const Camel &b) { return a.x - a.y > b.x - b.y; }
Camel v[N_MAX];
set<int> s;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> t;
  while (t--) {
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> v[i].pos >> v[i].x >> v[i].y;
    sort(v + 1, v + n + 1);
    s.clear();
    for (int i = 1; i <= n; i++) s.insert(i);
    long long ans = 0;
    for (int i = 1; i <= n; i++) ans += v[i].y;
    for (int i = 1; i <= n && v[i].x - v[i].y > 0; i++) {
      set<int>::iterator it = s.upper_bound(v[i].pos);
      if (it == s.begin()) {
        it = s.end();
        it--;
        s.erase(it);
        continue;
      }
      it--;
      ans += v[i].x - v[i].y;
    }
    for (int i = n; i >= 1 && v[i].x - v[i].y < 0; i--) {
      set<int>::iterator it = s.end();
      it--;
      if (*it <= v[i].pos) {
        it = s.begin();
        ans += v[i].x - v[i].y;
      }
      s.erase(it);
    }
    cout << ans << "\n";
  }
  return 0;
}
