#include <bits/stdc++.h>
using namespace std;
template <typename Type>
class RangeAddMinSegmentTree {
 private:
  unsigned size_;
  std::vector<Type> dat1, dat2;
  inline void update_(unsigned a, unsigned b, Type x, unsigned k, unsigned l,
                      unsigned r) {
    if (r <= a || b <= l) return;
    if (a <= l && r <= b) {
      dat2[k] += x;
      return;
    }
    update_(a, b, x, (k << 1), l, (l + r) >> 1);
    update_(a, b, x, (k << 1) + 1, (l + r) >> 1, r);
    dat1[k] = std::min(dat1[k << 1] + dat2[k << 1],
                       dat1[(k << 1) + 1] + dat2[(k << 1) + 1]);
  }
  inline Type query_(unsigned a, unsigned b, unsigned k, unsigned l,
                     unsigned r) {
    if (r <= a || b <= l) return std::numeric_limits<Type>::max();
    if (a <= l && r <= b) return dat1[k] + dat2[k];
    Type lc = query_(a, b, (k << 1), l, (l + r) >> 1);
    Type rc = query_(a, b, (k << 1) + 1, (l + r) >> 1, r);
    return std::min(lc, rc) + dat2[k];
  }

 public:
  RangeAddMinSegmentTree()
      : size_(0), dat1(std::vector<Type>()), dat2(std::vector<Type>()){};
  RangeAddMinSegmentTree(int size__) {
    for (size_ = 1; size_ < size__;) size_ <<= 1;
    dat1.resize(size_ << 1, 0);
    dat2.resize(size_ << 1, 0);
  }
  inline unsigned size() { return size_; }
  inline void add(unsigned i, Type x) { update_(i, i + 1, x, 1, 0, size_); }
  inline void add(unsigned l, unsigned r, Type x) {
    update_(l, r, x, 1, 0, size_);
  }
  inline Type query(unsigned l, unsigned r) {
    return query_(l, r, 1, 0, size_);
  }
};
long long T;
long long N, K[1 << 18], L[1 << 18], R[1 << 18];
long long A1[1 << 18], A2[1 << 18];
void init() {
  for (int i = 0; i <= 250000; i++) A1[i] = 0;
  for (int i = 0; i <= 250000; i++) A2[i] = 0;
}
vector<long long> calc(vector<pair<long long, long long>> D) {
  sort(D.begin(), D.end());
  reverse(D.begin(), D.end());
  long long score = 0;
  vector<long long> L;
  L.push_back(0);
  RangeAddMinSegmentTree<long long> Z(N + 2);
  for (int i = 1; i <= N; i++) Z.add(i, N + 1, 1);
  for (int i = 0; i < D.size(); i++) {
    int pos = D[i].second;
    Z.add(pos, N + 1, -1);
    if (Z.query(0, N + 2) == -1) {
      Z.add(pos, N + 1, 1);
    } else {
      score += D[i].first;
    }
    L.push_back(score);
  }
  return L;
}
long long solve() {
  init();
  long long BASE = 0;
  vector<pair<long long, long long>> V1, V2;
  for (int i = 1; i <= N; i++) {
    BASE += min(L[i], R[i]);
    if (L[i] >= R[i] && K[i] > 0) V1.push_back(make_pair(L[i] - R[i], K[i]));
    if (L[i] < R[i] && K[i] < N) V2.push_back(make_pair(R[i] - L[i], N - K[i]));
  }
  vector<long long> B1 = calc(V1);
  vector<long long> B2 = calc(V2);
  for (int i = 1; i <= N; i++) A1[i] = B1[min(i, (int)B1.size() - 1)];
  for (int i = 1; i <= N; i++) A2[N + 1 - i] = B2[min(i, (int)B2.size() - 1)];
  long long Answer = 0;
  for (int i = 1; i <= N; i++) Answer = max(Answer, A1[i] + A2[i + 1]);
  return BASE + Answer;
}
int main() {
  cin >> T;
  for (int i = 1; i <= T; i++) {
    cin >> N;
    for (int j = 1; j <= N; j++) cin >> K[j] >> L[j] >> R[j];
    cout << solve() << endl;
  }
  return 0;
}
