#include <bits/stdc++.h>
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;
struct camel {
  int k;
  long long l, r, m;
};
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int t;
  cin >> t;
  int n;
  long long INF = (long long)1e18;
  vector<camel> ca;
  for (int _ = (0), __len = (t); _ < __len; ++_) {
    cin >> n;
    ca.resize(n);
    vector<camel> cap, cam;
    for (int i = (0), i_len = (n); i < i_len; ++i) {
      cin >> ca[i].k >> ca[i].l >> ca[i].r;
      ca[i].k--;
      ca[i].m = ca[i].l - ca[i].r;
      if (ca[i].m > 0)
        cap.push_back(ca[i]);
      else if (ca[i].m < 0)
        cam.push_back(ca[i]);
    }
    sort((cap).begin(), (cap).end(),
         [](camel a, camel b) { return (a.k != b.k ? a.k < b.k : a.m > b.m); });
    sort((cam).begin(), (cam).end(),
         [](camel a, camel b) { return (a.k != b.k ? a.k > b.k : a.m < b.m); });
    for (int i = (0), i_len = (cap.size()); i < i_len; ++i) 42;
    42;
    for (int i = (0), i_len = (cam.size()); i < i_len; ++i) 42;
    42;
    long long ans = 0;
    for (int i = (0), i_len = (n); i < i_len; ++i) ans += ca[i].r;
    42;
    int now = 0;
    for (int i = (0), i_len = (cap.size()); i < i_len; ++i) {
      if (cap[i].k >= now) {
        42;
        ans += cap[i].m;
        now++;
      }
    }
    now = n - 1;
    for (int i = (0), i_len = (cam.size()); i < i_len; ++i) {
      if (cam[i].k <= now) {
        42;
        if (now == cam[i].k) ans += cam[i].m;
        now--;
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
