#include <bits/stdc++.h>
using namespace std;
vector<long long> vi[200200];
int main() {
  int n, i, j, k, x, y, ans, curr, temp, m;
  int tc;
  cin >> tc;
  while (tc--) {
    cin >> n;
    for (i = 1; i <= n; i++) vi[i].clear();
    long long chaeng = 0;
    multiset<long long, greater<long long> > ms;
    for (i = 1; i <= n; i++) {
      long long a, b;
      scanf("%d %lld %lld", &k, &a, &b);
      vi[k].push_back(b - a);
      chaeng += a;
    }
    int slot = 1;
    for (i = n - 1; i > 0; i--) {
      sort(vi[i].begin(), vi[i].end());
      while (!vi[i].empty() && ms.size() < slot) {
        if (vi[i].back() > 0) {
          ms.insert({vi[i].back()});
          vi[i].pop_back();
        } else {
          break;
        }
      }
      slot++;
    }
    for (auto x : ms) {
      chaeng += x;
    }
    cout << chaeng << endl;
  }
  return 0;
}
