#include <bits/stdc++.h>
using namespace std;
struct Camel {
  int K, L, R;
};
namespace Greedy1 {
class BinaryIndexedTree {
  int N;
  vector<int> tree;

 public:
  BinaryIndexedTree(int _N) : N(_N), tree(_N + 1) {}
  int query(int idx) {
    assert(idx <= N);
    int res = 0;
    for (int i = idx; i > 0; i -= (i & -i)) res += tree[i];
    return res;
  }
  int query(int L, int R) { return L <= R ? query(R) - query(L - 1) : 0; }
  void update(int idx, int val = 1) {
    for (int i = idx; i <= N; i += (i & -i)) tree[i] += val;
  }
};
long long solve(int N, vector<Camel> camels) {
  sort(camels.begin(), camels.end(),
       [&](Camel a, Camel b) { return abs(a.L - a.R) > abs(b.L - b.R); });
  BinaryIndexedTree bitL(N + 1);
  BinaryIndexedTree bitR(N + 1);
  long long res = 0;
  for (Camel c : camels) {
    if (c.L > c.R) {
      if (bitL.query(c.K) < c.K) {
        bitL.update(c.K);
        res += c.L;
      } else {
        assert(bitR.query(c.K + 1, N) < N - c.K);
        bitR.update(c.K + 1);
        res += c.R;
      }
    } else {
      if (bitR.query(c.K + 1, N) < N - c.K) {
        bitR.update(c.K + 1);
        res += c.R;
      } else {
        assert(bitL.query(c.K) < c.K);
        bitL.update(c.K);
        res += c.L;
      }
    }
  }
  return res;
}
}  // namespace Greedy1
namespace Greedy3 {
long long solve(int N, vector<Camel> camels) {
  sort(camels.begin(), camels.end(), [&](Camel a, Camel b) {
    if (a.K != b.K) return a.K < b.K;
    return a.L - a.R > b.L - b.R;
  });
  long long base = 0;
  for (Camel c : camels) base += c.R;
  priority_queue<int, vector<int>, greater<int> > pq;
  for (Camel c : camels) {
    if (c.K > int((pq).size())) {
      pq.push(c.L - c.R);
    } else {
      pq.push(c.L - c.R);
      pq.pop();
    }
  }
  long long res = base;
  while (!pq.empty()) {
    res += pq.top();
    pq.pop();
  }
  return res;
}
}  // namespace Greedy3
int main(int argc, char* argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int TC;
  cin >> TC;
  for (int tc = (1), _b = (TC); tc <= _b; ++tc) {
    int N;
    cin >> N;
    vector<Camel> camels;
    for (int i = 0, _n = (N); i < _n; ++i) {
      int K, L, R;
      cin >> K >> L >> R;
      camels.push_back({K, L, R});
    }
    long long res = Greedy1::solve(N, camels);
    cout << res << '\n';
  }
  return 0;
}
