#include <bits/stdc++.h>
using namespace std;
int inf = 1000001000;
long long INF = 1e18;
long long mod = 1000000007;
long long MOD = INF;
int main() {
  int t;
  cin >> t;
  for (int(ii) = 0; (ii) < (t); (ii)++) {
    int n;
    cin >> n;
    set<int> s;
    for (int(i) = 0; (i) < (n); (i)++) s.insert(i + 1);
    long long o = 0, c = 0;
    vector<pair<pair<long long, long long>, pair<long long, long long>>> a(n);
    for (int(i) = 0; (i) < (n); (i)++)
      cin >> a[i].first.second >> a[i].second.first >> a[i].second.second;
    for (int(i) = 0; (i) < (n); (i)++)
      a[i].first.first = a[i].second.first - a[i].second.second;
    sort((a).begin(), (a).end());
    reverse((a).begin(), (a).end());
    for (int(i) = 0; (i) < (n); (i)++) {
      auto it = s.upper_bound(a[i].first.first);
      if (a[i].first.first < 0) break;
      if (it == s.begin())
        c += a[i].second.second;
      else {
        c += a[i].second.first;
        o++;
        it--;
        s.erase(*it);
      }
    }
    reverse((a).begin(), (a).end());
    set<int> ss;
    for (int(i) = 0; (i) < (n); (i)++) ss.insert(i + 1);
    for (int(i) = 0; (i) < (n); (i)++) {
      if (a[i].first.first >= 0) break;
      auto it = ss.lower_bound(a[i].first.second + 1);
      if (it == ss.end())
        c += a[i].second.first;
      else {
        c += a[i].second.second;
        ss.erase(*it);
      }
    }
    cout << c << endl;
  }
}
