#include <bits/stdc++.h>
using namespace std;
using dou = long double;
string yes = "yes";
string Yes = "Yes";
string YES = "YES";
string no = "no";
string No = "No";
string NO = "NO";
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const long long mod = 1000000007ll;
struct mint {
  long long x;
  mint(long long x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += mod - a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(long long t) const {
    if (!t) return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1) a *= *this;
    return a;
  }
  mint inv() const { return pow(mod - 2); }
  mint& operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream& operator>>(istream& is, const mint& a) { return is >> a.x; }
ostream& operator<<(ostream& os, const mint& a) { return os << a.x; }
long long ceil(long long a, long long b) { return ((a + b - 1) / b); }
const int INF = 2000000000;
const long long INF64 = 9223372036854775ll;
const long long MOD = 1000000007ll;
const long long OD = 1000000000000007ll;
const dou pi = 3.141592653589793;
long long modpow(long long a, long long n, long long MOD) {
  long long resize = 1;
  while (n > 0) {
    if (n & 1) resize = resize * a % MOD;
    a = a * a % MOD;
    n >>= 1;
  }
  return resize;
}
int main() {
  int qq;
  std::cin >> qq;
  for (long long qqq = 0; qqq < (long long)(qq); qqq++) {
    int n;
    std::cin >> n;
    std::vector<tuple<long long, long long, long long>> t(n);
    std::set<pair<long long, long long>> s;
    long long ans = 0;
    for (long long i = 0; i < (long long)(n); i++) {
      long long d, dd, ddd;
      std::cin >> d >> dd >> ddd;
      d--;
      ans += ddd;
      t[i] = make_tuple(d, dd, ddd);
    }
    for (long long i = 0; i < (long long)(n); i++)
      s.insert(make_pair(get<1>(t[i]) - get<2>(t[i]), i));
    sort((t).begin(), (t).end());
    int p = 0;
    for (long long i = 0; i < (long long)(n); i++) {
      if (s.size() == 0) continue;
      pair<long long, long long> po = *s.rbegin();
      if (po.first > 0 || s.size() == n - i) {
        ans += po.first;
        s.erase(po);
      }
      while (get<0>(t[p]) == i) {
        s.erase(make_pair(get<1>(t[p]) - get<2>(t[p]), p));
        p++;
      }
    }
    std::cout << ans << std::endl;
  }
}
