#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
void *wmem;
char memarr[96000000];
template<class T> inline void walloc1d(T **arr, int x, void **mem = &wmem){
  static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
  (*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] );
  (*arr)=(T*)(*mem);
  (*mem)=((*arr)+x);
}
template<class T1> void sortA_L(int N, T1 a[], void *mem = wmem){
  sort(a, a+N);
}
template<class T1, class T2> void sortA_L(int N, T1 a[], T2 b[], void *mem = wmem){
  int i;
  pair<T1, T2> *arr;
  walloc1d(&arr, N, &mem);
  for(i=(0);i<(N);i++){
    arr[i].first = a[i];
    arr[i].second = b[i];
  }
  sort(arr, arr+N);
  for(i=(0);i<(N);i++){
    a[i] = arr[i].first;
    b[i] = arr[i].second;
  }
}
inline int my_getchar_unlocked(){
  static char buf[1048576];
  static int s = 1048576;
  static int e = 1048576;
  if(s == e && e == 1048576){
    e = fread_unlocked(buf, 1, 1048576, stdin);
    s = 0;
  }
  if(s == e){
    return EOF;
  }
  return buf[s++];
}
inline void rd(int &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = my_getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = my_getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
inline void rd(long long &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = my_getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = my_getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
inline int rd_int(void){
  int x;
  rd(x);
  return x;
}
struct MY_WRITER{
  char buf[1048576];
  int s;
  int e;
  MY_WRITER(){
    s = 0;
    e = 1048576;
  }
  ~MY_WRITER(){
    if(s){
      fwrite_unlocked(buf, 1, s, stdout);
    }
  }
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar_unlocked(int a){
  if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
    fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
    MY_WRITER_VAR.s = 0;
  }
  MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
  my_putchar_unlocked(a);
}
inline void wt_L(long long x){
  int s=0;
  int m=0;
  char f[20];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    my_putchar_unlocked('-');
  }
  while(s--){
    my_putchar_unlocked(f[s]+'0');
  }
}
int N;
int K[200000];
long long L[200000];
long long R[200000];
int sz;
int pk[200000];
long long pv[200000];
long long solve(void){
  int i;
  int j = sz-1;
  long long res = 0;
  priority_queue<long long> q;
  sortA_L(sz, pk, pv);
  for(i=(sz)-1;i>=(0);i--){
    while(j >= 0 && pk[j] > i){
      q.push(pv[j]);
      j--;
    }
    if(q.size()){
      res += q.top();
      q.pop();
    }
  }
  return res;
}
int main(){
  int cTE1_r3A;
  wmem = memarr;
  int RZTsC2BF = rd_int();
  for(cTE1_r3A=(0);cTE1_r3A<(RZTsC2BF);cTE1_r3A++){
    int i;
    long long res = 0;
    rd(N);
    {
      int WYIGIcGE;
      for(WYIGIcGE=(0);WYIGIcGE<(N);WYIGIcGE++){
        rd(K[WYIGIcGE]);
        rd(L[WYIGIcGE]);
        rd(R[WYIGIcGE]);
      }
    }
    sz = 0;
    for(i=(0);i<(N);i++){
      if(L[i] > R[i]){
        pk[sz] = K[i];
        pv[sz] = L[i] - R[i];
        res += R[i];
        sz++;
      }
    }
    res += solve();
    sz = 0;
    for(i=(0);i<(N);i++){
      if(L[i] < R[i]){
        pk[sz] = N - K[i];
        pv[sz] = R[i] - L[i];
        res += L[i];
        sz++;
      }
    }
    res += solve();
    for(i=(0);i<(N);i++){
      if(L[i]==R[i]){
        res += L[i];
      }
    }
    wt_L(res);
    wt_L('\n');
  }
  return 0;
}
// cLay varsion 20200509-1

// --- original code ---
// int N, K[2d5]; ll L[2d5], R[2d5];
// int sz, pk[2d5]; ll pv[2d5];
// 
// ll solve(void){
//   int i, j = sz-1;
//   ll res = 0;
//   priority_queue<ll> q;
// 
//   sortA(sz, pk, pv);
//   rrep(i,sz){
//     while(j >= 0 && pk[j] > i){
//       q.push(pv[j]);
//       j--;
//     }
//     if(q.size()){
//       res += q.top();
//       q.pop();
//     }
//   }
// 
//   return res;
// }
// 
// {
//   REP(rd_int()){
//     ll res = 0;
//     rd(N,(K,L,R)(N));
// 
//     sz = 0;
//     rep(i,N) if(L[i] > R[i]){
//       pk[sz] = K[i];
//       pv[sz] = L[i] - R[i];
//       res += R[i];
//       sz++;
//     }
//     res += solve();
// 
//     sz = 0;
//     rep(i,N) if(L[i] < R[i]){
//       pk[sz] = N - K[i];
//       pv[sz] = R[i] - L[i];
//       res += L[i];
//       sz++;
//     }
//     res += solve();
// 
//     rep(i,N) if(L[i]==R[i]) res += L[i];
// 
//     wt(res);
//   }
// }


Measured memory_bytes: 11916