#include <bits/stdc++.h>
using namespace std;
class Dinic {
  int MAX_V;
  int INF;
  struct edge {
    int to, cap, rev, icap, flow;
  };
  vector<vector<edge>> G;
  vector<int> level;
  vector<int> iter;
  void max_flow_bfs(int s) {
    fill(level.begin(), level.end(), -1);
    queue<int> que;
    level[s] = 0;
    que.push(s);
    while (!que.empty()) {
      int v = que.front();
      que.pop();
      for (int i = 0; i < G[v].size(); i++) {
        edge& e = G[v][i];
        if (e.cap > 0 && level[e.to] < 0) {
          level[e.to] = level[v] + 1;
          que.push(e.to);
        }
      }
    }
  }
  int max_flow_dfs(int v, int t, int f) {
    if (v == t) return f;
    for (int& i = iter[v]; i < G[v].size(); i++) {
      edge& e = G[v][i];
      if (e.cap > 0 && level[v] < level[e.to]) {
        int d = max_flow_dfs(e.to, t, min(f, e.cap));
        if (d > 0) {
          e.cap -= d;
          G[e.to][e.rev].cap += d;
          e.flow += d;
          return d;
        }
      }
    }
    return 0;
  }

 public:
  Dinic(int N) : MAX_V(N), G(N), level(N), iter(N) { INF = 99999999; }
  void add_edge(int from, int to, int cap) {
    G[from].push_back((edge){to, cap, (int)G[to].size(), cap, 0});
    G[to].push_back((edge){from, 0, (int)G[from].size() - 1, 0, 0});
  }
  int get_flow(int from, int to) {
    for (int i = (0); i < (int)(G[from].size()); i++) {
      if (G[from][i].to == to) {
        return G[from][i].flow;
      }
    }
    return -1;
  }
  int max_flow(int s, int t) {
    int flow = 0;
    while (true) {
      max_flow_bfs(s);
      if (level[t] < 0) return flow;
      fill(iter.begin(), iter.end(), 0);
      int f;
      while ((f = max_flow_dfs(s, t, INF)) > 0) {
        flow += f;
      }
    }
  }
  int min_cut(int s, int t, vector<int>& S, vector<int>& T) {
    S.clear();
    T.clear();
    int maxf = max_flow(s, t);
    for (int i = 0; i < level.size(); i++) {
      if (level[i] >= 0)
        S.push_back(i);
      else
        T.push_back(i);
    }
    return maxf;
  }
};
void solve(int N, const vector<vector<int>>& v) {
  Dinic dinic(N * N + 2);
  int st = N * N;
  int ed = N * N + 1;
  for (int i = (0); i < (int)(N); i++) {
    for (int j = (0); j < (int)(N); j++) {
      if (i <= v[i][0]) {
        dinic.add_edge(i, N + j, v[i][1]);
      } else {
        dinic.add_edge(i, N + j, v[i][2]);
      }
    }
  }
  for (int i = (0); i < (int)(N); i++) {
    dinic.add_edge(st, i, 0);
    dinic.add_edge(N + i, ed, 0);
  }
  cout << dinic.max_flow(st, ed) << endl;
}
int main() {
  int T;
  cin >> T;
  for (int t = (0); t < (int)(T); t++) {
    int N;
    cin >> N;
    vector<vector<int>> v;
    for (int i = (0); i < (int)(N); i++) {
      int k, l, r;
      cin >> k >> l >> r;
      k--;
      v.push_back({k, l, r});
    }
    solve(N, v);
  }
  return 0;
}
