#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
vector<int> a[N];
int n, k[N], l[N], r[N], vis[N];
bool cmp(int u, int v) { return (l[u] - r[u]) >= (l[v] - r[v]); }
void rst() {
  for (int i = 0; i < N; ++i) {
    a[i].clear();
    vis[i] = 0;
  }
}
void solve() {
  int pos = 0;
  long long ans = 0;
  rst();
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> k[i] >> l[i] >> r[i];
    k[i]--;
    a[k[i]].push_back(i);
  }
  for (int i = 0; i < n; ++i) {
    sort(a[i].begin(), a[i].end(), cmp);
  }
  for (int i = 0; i < n; ++i) {
    for (int v : a[i]) {
      if (l[v] > r[v]) {
        vis[v] = 1;
        ans += (1LL * l[v]);
        pos++;
        break;
      }
    }
  }
  for (int i = n - 1; i >= 0; --i) {
    for (int v : a[i]) {
      if (vis[v]) continue;
      if (i >= pos) {
        ans += (1LL * l[v]);
        pos++;
      } else {
        ans += (1LL * r[v]);
        pos++;
      }
    }
  }
  cout << ans << '\n';
}
int main(int argc, char const *argv[]) {
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
