#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

using LL = long long;
const int MAXN = 2e5 + 5;
const int MAXV = 1e6 + 5;
const int MAXE = 2e6 + 5;
const int INF = 1e9 + 5;
const LL INFLL = 1e15 + 5;
struct edge {
  int next, to, cf, w;
} e[MAXE << 2];
int head[MAXV], cnt_adj;
void add_edge(int x, int y, int z, int w);
void add_edge_flow(int x, int y, int z, int w);
int V, start, dest;
pair<int, LL> Cost_Low(int st, int se, int V);
int T, N, M;
int K[MAXN];
pair<int, int> LR[MAXN];
struct ST {
  int left, right;
  int size;
} st[MAXN << 2];
void build(int pos, int l, int r);
void update(int pos, int l, int r, int u);

int main() {
  //
  scanf("%d", &T);
  while (T--) {
    scanf("%d", &N);
    M = (N << 2) + N + 2;
    fill(head, head + M + 1, 0);
    cnt_adj = 1;
    V = N << 2;
    start = 5 * N + 1, dest = start + 1;
    build(1, 1, N);
    LL ans = 0;
    for (int i = 1; i <= N; i++) {
      scanf("%d%d%d", K + i, &LR[i].first, &LR[i].second);
      ans += min(LR[i].first, LR[i].second);
      if (LR[i].first != LR[i].second) {
        add_edge_flow(start, V + i, 1, -abs(LR[i].first - LR[i].second));
        if (LR[i].first > LR[i].second)
          update(1, 1, K[i], V + i);
        else if (K[i] < N)
          update(1, K[i] + 1, N, V + i);
      }
    }
    V = dest;
    ans += Cost_Low(start, dest, V).second * -1;
    printf("%lld\n", ans);
  }
  return 0;
}

void build(int pos, int l, int r) {
  st[pos].left = l, st[pos].right = r;
  if (l == r) {
    st[pos].size = 1;
    add_edge_flow(pos, dest, 1, 0);
    return;
  }
  int lc = pos << 1, rc = lc + 1;
  int mid = (l + r) >> 1;
  build(lc, l, mid);
  build(rc, mid + 1, r);
  add_edge_flow(pos, lc, st[lc].size, 0);
  add_edge_flow(pos, rc, st[rc].size, 0);
  st[pos].size = st[lc].size + st[rc].size;
}

void update(int pos, int l, int r, int u) {
  if (l <= st[pos].left && st[pos].right <= r) {
    add_edge_flow(u, pos, 1, 0);
    return;
  }
  int lc = pos << 1, rc = lc + 1;
  int mid = (st[pos].left + st[pos].right) >> 1;
  if (l <= mid) update(lc, l, r, u);
  if (mid < r) update(rc, l, r, u);
}

int cur[MAXV];
LL h[MAXV], dis[MAXV];
bool vis[MAXV];
bool Dijkstra(int st, int se, int V);
int dfs(int u, int se, int cf_min);
pair<int, LL> Cost_Low(int st, int se, int V) {
  int maxflow;
  LL mincost;
  maxflow = mincost = 0;
  while (Dijkstra(st, se, V)) {
    // cout << dis[se] << endl;
    for (int i = 1; i <= V; i++) cur[i] = head[i];
    int val;
    val = dfs(st, se, INF);
    for (int i = 1; i <= V; i++)
      if (dis[i] < INF) h[i] += dis[i];
    maxflow += val;
    mincost += val * h[se];
  }
  return make_pair(maxflow, mincost);
}

bool Dijkstra(int st, int se, int V) {
  fill(dis + 1, dis + 1 + V, INFLL);
  priority_queue<pair<LL, int>> Q;
  dis[st] = 0;
  Q.push(make_pair(-dis[st], st));
  while (!Q.empty()) {
    auto item = Q.top();
    Q.pop();
    int u = item.second;
    if (dis[u] < -item.first) continue;
    for (int i = head[u]; i; i = e[i].next) {
      int v = e[i].to, cf = e[i].cf;
      LL w = e[i].w + h[u] - h[v];
      if (cf && dis[v] > dis[u] + w) {
        dis[v] = dis[u] + w;
        Q.push(make_pair(-dis[v], v));
      }
    }
  }
  return dis[se] < INF;
}

int dfs(int u, int se, int cf_min) {
  if (u == se) return cf_min;
  int r = cf_min;
  vis[u] = true;
  for (int &i = cur[u]; i && r; i = e[i].next) {
    int v = e[i].to, cf = e[i].cf, w = e[i].w + h[u] - h[v];
    if (!vis[v] && cf && dis[v] == dis[u] + w) {
      int x = dfs(v, se, min(cf, r));
      r -= x;
      e[i].cf -= x;
      e[i ^ 1].cf += x;
    }
  }
  vis[u] = false;
  return cf_min - r;
}

void add_edge_flow(int x, int y, int z, int w) {
  add_edge(x, y, z, w);
  add_edge(y, x, 0, -w);
}

void add_edge(int x, int y, int z, int w) {
  e[++cnt_adj].to = y;
  e[cnt_adj].cf = z;
  e[cnt_adj].w = w;
  e[cnt_adj].next = head[x];
  head[x] = cnt_adj;
}
