#include <bits/stdc++.h>
typedef struct {
  int key, id;
} data;
typedef struct {
  data obj[400001];
  int size;
} max_heap;
void push(data x, max_heap* h) {
  int i = ++(h->size), j = i >> 1;
  data tmp;
  h->obj[i] = x;
  while (j > 0) {
    if (h->obj[i].key > h->obj[j].key) {
      tmp = h->obj[j];
      h->obj[j] = h->obj[i];
      h->obj[i] = tmp;
      i = j;
      j >>= 1;
    } else
      break;
  }
}
data pop(max_heap* h) {
  int i = 1, j = 2;
  data output = h->obj[1], tmp;
  h->obj[1] = h->obj[(h->size)--];
  while (j <= h->size) {
    if (j < h->size && h->obj[j ^ 1].key > h->obj[j].key) j ^= 1;
    if (h->obj[j].key > h->obj[i].key) {
      tmp = h->obj[j];
      h->obj[j] = h->obj[i];
      h->obj[i] = tmp;
      i = j;
      j <<= 1;
    } else
      break;
  }
  return output;
}
void add_BIT(int N, int BIT[], int i, int k) {
  while (i <= N) {
    BIT[i] += k;
    i += (i & -i);
  }
}
int sum_BIT(int BIT[], int r) {
  int sum = 0;
  while (r > 0) {
    sum += BIT[r];
    r -= (r & -r);
  }
  return sum;
}
int main() {
  int i, l, r, m, t, T, N, K[200001], L[200001], R[200001], BIT[2][200001], tmp,
      flag[200001];
  long long ans;
  data d;
  max_heap h;
  scanf("%d", &T);
  for (t = 1; t <= T; t++) {
    scanf("%d", &N);
    h.size = 0;
    ans = 0;
    for (i = 1; i <= N; i++) {
      flag[i] = 0;
      BIT[0][i] = 0;
      BIT[1][i] = 0;
    }
    for (i = 1; i <= N; i++) {
      scanf("%d %d %d", &(K[i]), &(L[i]), &(R[i]));
      d.key = L[i] - R[i];
      d.id = i;
      push(d, &h);
      d.key *= -1;
      d.id *= -1;
      push(d, &h);
    }
    while (h.size > 0) {
      d = pop(&h);
      if (flag[abs(d.id)] == 1) continue;
      if (d.id > 0) {
        tmp = sum_BIT(BIT[0], K[d.id]);
        if (tmp < K[d.id]) {
          ans += L[d.id];
          flag[d.id] = 1;
          l = 0;
          r = K[d.id] - 1;
          while (l < r) {
            m = (l + r + 1) / 2;
            if (tmp - sum_BIT(BIT[0], m) == K[d.id] - m) r = m - 1;
            l = m;
          }
          add_BIT(N, BIT[0], l + 1, 1);
        }
      } else {
        d.id *= -1;
        tmp = sum_BIT(BIT[1], N - K[d.id]);
        if (tmp < N - K[d.id]) {
          ans += R[d.id];
          flag[d.id] = 1;
          l = 0;
          r = N - K[d.id] - 1;
          while (l < r) {
            m = (l + r + 1) / 2;
            if (tmp - sum_BIT(BIT[1], m) == N - K[d.id] - m) r = m - 1;
            l = m;
          }
          add_BIT(N, BIT[1], l + 1, 1);
        }
      }
    }
    printf("%lld\n", ans);
  }
  fflush(stdout);
  return 0;
}
