#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5;
int t, n, k[maxn], l[maxn], r[maxn];
long long ans;
vector<int> pre[maxn], suff[maxn];
int main() {
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d%d%d", &k[i], &l[i], &r[i]);
    ans = 0;
    for (int i = 1; i <= n; i++) pre[i].clear(), suff[i].clear();
    for (int i = 1; i <= n; i++) {
      if (l[i] > r[i]) {
        pre[k[i]].push_back(l[i] - r[i]);
        ans += r[i];
      } else if (l[i] < r[i] && k[i] < n) {
        suff[k[i] + 1].push_back(r[i] - l[i]);
        ans += l[i];
      } else
        ans += l[i];
    }
    priority_queue<int, vector<int>, greater<int> > que;
    for (int i = 1; i <= n; i++) {
      for (int elem : pre[i]) que.push(elem);
      while (que.size() > i) {
        ans -= que.top();
        que.pop();
      }
    }
    while (que.size()) {
      ans += que.top();
      que.pop();
    }
    for (int i = n; i >= 1; i--) {
      for (int elem : suff[i]) que.push(elem);
      while (que.size() > n - i + 1) {
        ans -= que.top();
        que.pop();
      }
    }
    while (que.size()) {
      ans += que.top();
      que.pop();
    }
    cout << ans << "\n";
  }
}
