#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
int t, n, n2, x, y, z, datmin[800010], datmax[800010], ans;
vector<pair<int, pair<int, int> > > v, v2;
bool cmp(pair<int, pair<int, int> > a, pair<int, pair<int, int> > b) {
  return abs(a.second.first - a.second.second) >
         abs(b.second.first - b.second.second);
}
void build(int k, int lb, int ub) {
  if (lb == ub) {
    if (lb < n)
      datmin[k] = datmax[k] = lb;
    else {
      datmin[k] = 1e9;
      datmax[k] = -1e9;
    }
    return;
  }
  build(k * 2 + 1, lb, (lb + ub) / 2);
  build(k * 2 + 2, (lb + ub) / 2 + 1, ub);
  datmax[k] = max(datmax[k * 2 + 1], datmax[k * 2 + 2]);
  datmin[k] = min(datmin[k * 2 + 1], datmin[k * 2 + 2]);
}
void updmax(int k) {
  datmax[k] = -1e9;
  while (k > 0) {
    k = (k - 1) / 2;
    datmax[k] = max(datmax[k * 2 + 1], datmax[k * 2 + 2]);
  }
}
void updmin(int k) {
  datmax[k] = 1e9;
  while (k > 0) {
    k = (k - 1) / 2;
    datmin[k] = min(datmin[k * 2 + 1], datmin[k * 2 + 2]);
  }
}
int querymax(int k, int lb, int ub, int tlb, int tub) {
  if (ub < tlb || tub < lb) return -1e9;
  if (tlb <= lb && ub <= tub) return datmax[k];
  return max(querymax(k * 2 + 1, lb, (lb + ub) / 2, tlb, tub),
             querymax(k * 2 + 2, (lb + ub) / 2 + 1, ub, tlb, tub));
}
int querymin(int k, int lb, int ub, int tlb, int tub) {
  if (ub < tlb || tub < lb) return 1e9;
  if (tlb <= lb && ub <= tub) return datmin[k];
  return min(querymin(k * 2 + 1, lb, (lb + ub) / 2, tlb, tub),
             querymin(k * 2 + 2, (lb + ub) / 2 + 1, ub, tlb, tub));
}
int main() {
  cin >> t;
  for (int tn = 0; tn < t; tn++) {
    cin >> n;
    n2 = 1;
    while (n2 < n) n2 *= 2;
    v.clear();
    v2.clear();
    for (int i = 0; i < n; i++) {
      scanf("%d%d%d", &x, &y, &z);
      if (y > z)
        v.push_back(make_pair(x - 1, make_pair(y, z)));
      else
        v2.push_back(make_pair(x, make_pair(z, y)));
    }
    sort(v.begin(), v.end(), cmp);
    sort(v2.begin(), v2.end(), cmp);
    build(0, 0, n2 - 1);
    ans = 0;
    for (int i = 0; i < v.size(); i++) {
      int tmp = querymax(0, 0, n2 - 1, 0, v[i].first);
      if (tmp == -1e9) {
        ans += v[i].second.second;
        continue;
      }
      ans += v[i].second.first;
      updmax(tmp + n2 - 1);
    }
    for (int i = 0; i < v2.size(); i++) {
      int tmp = querymin(0, 0, n2 - 1, v2[i].first, n - 1);
      if (tmp == 1e9) {
        ans += v2[i].second.second;
        continue;
      }
      ans += v2[i].second.first;
      updmax(tmp + n2 - 1);
    }
    cout << ans << endl;
  }
  return 0;
}
