#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, cost;
} 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;
LL Dinic(int s, int t);
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 + 5;
    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 += Dinic(start, dest);
    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[pos].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 dep[MAXV], cur[MAXV];
LL dis[MAXV];
bool vis[MAXV];
bool Dinic_bfs(int s, int t);
int Dinic_dfs(int u, int cf_min, int t);
LL Dinic(int s, int t) {
  int i, maxflow;
  LL sum;
  maxflow = sum = 0;
  while (Dinic_bfs(s, t)) {
    for (i = 1; i <= V; i++) cur[i] = head[i];
    while (int val = Dinic_dfs(s, INF, t)) {
      maxflow += val;
      sum += val * dis[t];
    }
  }
  return sum;
}

bool Dinic_bfs(int s, int t) {
  int i;
  // memset(dep, 0, sizeof(dep));
  // memset(vis, 0, sizeof(vis));
  // memset(dis, -1, sizeof(dis));
  fill(dep, dep + V + 5, 0);
  fill(vis, vis + V + 5, 0);
  fill(dis, dis + V + 5, -INFLL);
  queue<int> Q;
  dis[s] = 0, dis[t] = -INFLL;
  Q.push(s), vis[s] = true;
  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    vis[u] = false;
    for (i = head[u]; i; i = e[i].next) {
      int v = e[i].to, cf = e[i].cf, w = e[i].cost;
      if (cf && dis[v] < dis[u] + w) {
        dis[v] = dis[u] + w;
        dep[v] = dep[u] + 1;
        if (!vis[v]) Q.push(v), vis[v] = true;
      }
    }
  }
  if (dis[t] == -INFLL) return false;
  return true;
}

int Dinic_dfs(int u, int cf_min, int t) {
  if (u == t || !cf_min) return cf_min;
  for (int &i = cur[u]; i; i = e[i].next) {
    int v = e[i].to, cf = e[i].cf, w = e[i].cost;
    if (cf && dep[v] == dep[u] + 1 && dis[v] == dis[u] + w) {
      int val = Dinic_dfs(v, min(cf_min, cf), t);
      if (val) {
        e[i].cf -= val;
        e[i ^ 1].cf += val;
        return val;
      }
    }
  }
  return 0;
}

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].cost = w;
  e[cnt_adj].next = head[x];
  head[x] = cnt_adj;
}
