#include <bits/stdc++.h>
using namespace std;
mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x; }
template <typename T>
void read(T &x) {
  x = 0;
  char c = getchar();
  T sig = 1;
  for (; !isdigit(c); c = getchar())
    if (c == '-') sig = -1;
  for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
  x *= sig;
}
class Solution {
 public:
  void solve() {
    int n;
    read(n);
    vector<vector<pair<int, int>>> v(n + 1);
    long long ans = 0;
    for (int i = 0; i < n; ++i) {
      int k, l, r;
      read(k), read(l), read(r);
      v[k].emplace_back(l, r);
      ans += r;
    }
    priority_queue<pair<int, int>> pq;
    vector<bool> vis(n + 1);
    for (int i = n; i >= 1; --i) {
      for (auto p : v[i]) pq.push({p.first - p.second, i});
      if (!pq.empty() && pq.top().first > 0) {
        ans += pq.top().first;
        pq.pop();
        vis[i] = true;
      }
    }
    int cnt = 0;
    for (int i : vis) cnt += i;
    vis.assign(n + 1, false);
    for (int i = 1; i <= cnt; ++i) vis[i] = true;
    int curr = cnt + 1;
    while (!pq.empty()) {
      if (curr <= pq.top().second) ans += pq.top().first;
      pq.pop();
      curr++;
    }
    cout << ans << endl;
  }
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t;
  read(t);
  while (t--) {
    Solution solution = Solution();
    solution.solve();
  }
}
