#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using lint = long long;
const lint inf = 1e18 + 7;
const long long MOD = 1000000007;
class Camel {
 public:
  long long k, l, r;
  void input() { cin >> k >> l >> r; }
};
long long solve() {
  long long n;
  cin >> n;
  lint res = 0;
  vector<Camel> cam(n);
  for (long long i = 0; i < n; ++i) {
    cam[i].input();
    res += min(cam[i].l, cam[i].r);
  }
  vector<pair<long long, long long> > front;
  vector<pair<long long, long long> > rear;
  for (long long i = 0; i < n; ++i) {
    if (cam[i].l > cam[i].r)
      front.push_back({cam[i].k, cam[i].l - cam[i].r});
    else {
      rear.push_back({cam[i].k, cam[i].r - cam[i].l});
    }
  }
  sort(front.begin(), front.end());
  sort(rear.begin(), rear.end());
  reverse(front.begin(), front.end());
  front.push_back({0, inf});
  long long cur = 0;
  priority_queue<long long> que;
  for (long long i = front.size() - 1; i > 0; --i) {
    while (front[cur].first >= i) {
      que.push(front[cur].second);
      ++cur;
    }
    if (!que.empty()) {
      res += que.top();
      que.pop();
    }
  }
  priority_queue<long long> que2;
  rear.push_back({inf, inf});
  cur = 0;
  for (long long i = front.size(); i <= n; ++i) {
    while (rear[cur].first < i) {
      que.push(rear[cur].second);
      ++cur;
    }
    if (!que.empty()) {
      res += que.top();
      que.pop();
    }
  }
  return res;
}
signed main() {
  long long t;
  cin >> t;
  vector<long long> res(t);
  for (long long i = 0; i < t; ++i) {
    res[i] = solve();
  }
  for (long long i = 0; i < t; ++i) {
    cout << res[i] << "\n";
  }
  return 0;
}
