#include <bits/stdc++.h>
int main() {
  int t;
  for (scanf("%d", &t); t--;) {
    int n;
    scanf("%d", &n);
    struct camel {
      int k, l, r;
      camel(const int& k, const int& l, const int& r) : k(k), l(l), r(r) {}
    };
    std::vector<camel> a, b;
    for (int i = 0; i < n; i++) {
      int k, l, r;
      scanf("%d %d %d", &k, &l, &r);
      auto& v = l >= r ? a : b;
      v.emplace_back(k, l, r);
    }
    sort(a.begin(), a.end(),
         [](const camel& l, const camel& r) { return l.k > r.k; });
    sort(b.begin(), b.end(),
         [](const camel& l, const camel& r) { return l.k < r.k; });
    long long h = 0;
    for (auto& i : a) h += i.r;
    for (auto& i : b) h += i.l;
    {
      std::priority_queue<int> q;
      for (int i = (int)a.size(), j = 0; i >= 1; i--) {
        while (j < (int)a.size() && a[j].k >= i) {
          q.emplace(a[j].l - a[j].r);
          j++;
        }
        if (!q.empty()) {
          h += q.top();
          q.pop();
        }
      }
    }
    {
      std::priority_queue<int> q;
      for (int i = (int)a.size() + 1, j = 0; i <= n; i++) {
        while (j < (int)b.size() && b[j].k < i) {
          q.emplace(b[j].r - b[j].l);
          j++;
        }
        if (!q.empty()) {
          h += q.top();
          q.pop();
        }
      }
    }
    printf("%lld\n", h);
  }
  return 0;
}
