#include <bits/stdc++.h>
#pragma GCC optimize("O2")
#pragma GCC target("avx2")
using namespace std;
const int cm = 1 << 17;
char cn[cm], *ci = cn + cm, ct;
inline char getcha() {
  if (ci - cn == cm) {
    fread_unlocked(cn, 1, cm, stdin);
    ci = cn;
  }
  return *ci++;
}
inline int getint() {
  int A = 0;
  if (ci - cn + 16 > cm)
    while ((ct = getcha()) >= '0') A = A * 10 + ct - '0';
  else
    while ((ct = *ci++) >= '0') A = A * 10 + ct - '0';
  return A;
}
const int dm = 1 << 21;
char dn[dm], *di = dn;
inline void putint2(int X) {
  int keta = 0;
  char C[10];
  while (X) {
    *(C + keta) = '0' + X % 10;
    X /= 10;
    keta++;
  }
  for (int i = keta - 1; i >= 0; i--) *di++ = (*(C + i));
  *di++ = '\n';
}
inline void putint(long long X) {
  int keta = 0;
  char C[20];
  while (X > 1e9) {
    *(C + keta) = '0' + X % 10;
    X /= 10;
    keta++;
  }
  for (int i = keta - 1; i >= 0; i--) *di++ = (*(C + i));
  *di++ = '\n';
}
int main() {
  pair<int, int> LR[200000];
  int T = getint();
  for (int t = 0; t < (T); t++) {
    int N = getint();
    if (N <= 2) {
      int k = getint(), l = getint(), r = getint();
      if (N == 1)
        putint2(l);
      else if (k == 2) {
        int k2 = getint(), l2 = getint(), r2 = getint();
        if (k2 == 2)
          putint2(l + l2);
        else
          putint2(l + max(l2, r2));
      } else {
        int k2 = getint(), l2 = getint(), r2 = getint();
        if (k2 == 2)
          putint2(l2 + max(l, r));
        else
          putint2(max(l + r2, l2 + r));
      }
      continue;
    }
    long long saishou = 0;
    int k1 = 0, k2 = N;
    for (int i = 0; i < (N); i++) {
      int k = getint(), l = getint(), r = getint();
      if (l < r) {
        LR[--k2] = make_pair(k + 1, r - l);
        saishou += l;
      } else {
        LR[k1++] = make_pair(k, l - r);
        saishou += r;
      }
    }
    sort(LR, LR + k1);
    sort(LR + k1, LR + N);
    priority_queue<int> PQ;
    int i = k1;
    while (i < N && LR[i].first < k1) {
      PQ.push(LR[i++].second);
    }
    for (int j = k1; j <= N; j++) {
      while (i < N && LR[i].first <= j) {
        PQ.push(LR[i++].second);
      }
      if (PQ.size()) {
        auto q = PQ.top();
        PQ.pop();
        saishou += q;
      }
    }
    PQ = priority_queue<int>();
    i = k1 - 1;
    while (i >= 0 && LR[i].first > k1) {
      PQ.push(LR[i--].second);
    }
    for (int j = k1 - 1; j >= 0; j--) {
      while (i >= 0 && LR[i].first > j) {
        PQ.push(LR[i--].second);
      }
      if (PQ.size()) {
        auto q = PQ.top();
        PQ.pop();
        saishou += q;
      }
    }
    putint(saishou);
  }
  fwrite(dn, 1, di - dn, stdout);
  return 0;
}
