#include <bits/stdc++.h>
struct rakuda_t {
  int idx;
  int K, L, R;
};
int igutiyuka(const void* x, const void* y) {
  struct rakuda_t a = *(const struct rakuda_t*)x,
                  b = *(const struct rakuda_t*)y;
  return a.K < b.K ? -1 : a.K > b.K;
}
int tomatuharuka(const void* x, const void* y) {
  struct rakuda_t a = *(const struct rakuda_t*)x,
                  b = *(const struct rakuda_t*)y;
  return a.L - a.R > b.L - b.R ? -1 : a.L - a.R < b.L - b.R;
}
int hsize = 0;
struct rakuda_t h[271828];
void ah(int idx) {
  for (;;) {
    int mi = idx;
    int i;
    for (i = 1; i <= 2; i++) {
      int ci = idx * 2 + i;
      if (ci < hsize && h[mi].L - h[mi].R < h[ci].L - h[ci].R) {
        mi = ci;
      }
    }
    if (mi == idx) {
      if (idx == 0) break;
      idx = (idx - 1) / 2;
    } else {
      struct rakuda_t kitaeri = h[idx];
      h[idx] = h[mi];
      h[mi] = kitaeri;
      idx = mi;
    }
  }
}
void hpush(struct rakuda_t r) {
  h[hsize++] = r;
  ah(hsize - 1);
}
struct rakuda_t hpop(void) {
  struct rakuda_t ret;
  if (hsize <= 0) {
    puts("empty!!!!!");
    exit(2);
  }
  ret = h[0];
  h[0] = h[--hsize];
  if (hsize > 0) ah(0);
  return ret;
}
int sumipe(int pos, const struct rakuda_t* r) {
  return pos <= r->K ? r->L : r->R;
}
int maaya(int pos, const struct rakuda_t* r) {
  return pos <= r->K ? r->L - r->R : r->R - r->L;
}
int N;
struct rakuda_t rakuda[271828];
struct rakuda_t usiro[271728];
char tukatta[271828];
int main(void) {
  int T;
  int hayamisaori;
  if (scanf("%d", &T) != 1) return 1;
  for (hayamisaori = 0; hayamisaori < T; hayamisaori++) {
    int i, pos, upos;
    int64_t mamegu = 0;
    if (scanf("%d", &N) != 1) return 1;
    for (i = 0; i < N; i++) {
      if (scanf("%d%d%d", &rakuda[i].K, &rakuda[i].L, &rakuda[i].R) != 3)
        return 1;
      rakuda[i].idx = i;
    }
    memset(tukatta, 0, sizeof(*tukatta) * N);
    memcpy(usiro, rakuda, sizeof(*rakuda) * N);
    qsort(rakuda, N, sizeof(*rakuda), igutiyuka);
    qsort(usiro, N, sizeof(*usiro), tomatuharuka);
    pos = 0;
    upos = 0;
    for (i = 1; i <= N; i++) {
      struct rakuda_t asumisu;
      hsize = 0;
      while (pos < N && rakuda[pos].K <= i) {
        if (!tukatta[rakuda[pos].idx]) hpush(rakuda[pos]);
        pos++;
      }
      while (upos < N && tukatta[usiro[upos].idx]) upos++;
      if (hsize > 0 &&
          (upos >= N || maaya(i, &h[0]) >= maaya(i, &usiro[upos]))) {
        asumisu = hpop();
        if (tukatta[asumisu.idx]) {
          i--;
          continue;
        }
        tukatta[asumisu.idx] = 1;
        mamegu += sumipe(i, &asumisu);
      } else {
        if (upos >= N) {
          puts("upos kanbai!!!");
          return 2;
        }
        tukatta[usiro[upos].idx] = 1;
        mamegu += sumipe(i, &usiro[upos]);
        upos++;
      }
    }
    printf("%" PRId64 "\n", mamegu);
  }
  return 0;
}
