#include <bits/stdc++.h>
using namespace std;
vector<int> in[200200];
int k[200200], l[200200], r[200200];
int main() {
  int Test;
  scanf("%d", &Test);
  while (Test--) {
    int n, x = 0;
    scanf("%d", &n);
    long long base = 0;
    for (int i = 0; i < n; i++) {
      scanf("%d %d %d", &k[i], &l[i], &r[i]);
      if (l[i] >= r[i]) x++;
      base += r[i];
    }
    for (int i = 0; i < n; i++) {
      if (l[i] >= r[i]) {
        if (k[i] > x) k[i] = x;
      } else {
        if (k[i] <= x) continue;
      }
      in[k[i]].push_back(l[i] - r[i]);
    }
    priority_queue<int> Q;
    for (int i = x; i >= 1; i--) {
      for (auto &v : in[i]) Q.push(v);
      if (!Q.empty()) base += Q.top(), Q.pop();
    }
    while (!Q.empty()) Q.pop();
    for (int i = n; i > x; i--) {
      for (auto &v : in[i]) Q.push(v);
      if (!Q.empty()) base += Q.top(), Q.pop();
    }
    printf("%lld\n", base);
  }
  return 0;
}
