#include <bits/stdc++.h>
using namespace std;
const int maxn = 200009;
const int MOD = 1e9 + 7;
int main() {
  ios_base::sync_with_stdio(false), cout.tie(0), cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<vector<int>> V(1 + n);
    int64_t ans = 0;
    for (int i = 0; i < n; ++i) {
      int k, l, r;
      cin >> k >> l >> r;
      ans += r;
      V[k].push_back(l - r);
    }
    priority_queue<int> que, que2;
    int sz = 0;
    for (int i = n; i >= 1; --i) {
      for (int j : V[i]) que.push(j);
      if (((int)(que).size()) == 0)
        sz++;
      else {
        if (que.top() >= 0 || sz == 0) {
          ans += que.top();
          que.pop();
        } else {
          while (((int)(que).size()) && que.top() < 0 &&
                 ((int)(que2).size()) != sz)
            que2.push(que.top()), que.pop();
          while (((int)(que).size()) && que.top() < que2.top()) {
            int a = que.top(), b = que2.top();
            que.pop();
            que2.pop();
            que2.push(a);
            que.push(b);
          }
        }
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
