#include <bits/stdc++.h>
using namespace std;
int n, k[500010], l[500010], r[500010], dif[500010], dis[500010], id[500010];
int p1[500010], p2[500010];
inline bool cmp(int x, int y) {
  if (dif[x] != dif[y])
    return dif[x] > dif[y];
  else
    return dis[x] < dis[y];
}
inline int find_set(int p[], int x) {
  return p[x] == x ? x : p[x] = find_set(p, p[x]);
}
signed main() {
  int t;
  cin >> t;
  while (t--) {
    cin >> n;
    for (int i = 1; i <= n; i++) {
      scanf("%d%d%d", &k[i], &l[i], &r[i]);
      dif[i] = abs(r[i] - l[i]);
      if (l[i] > r[i])
        dis[i] = k[i];
      else
        dis[i] = n - k[i];
      id[i] = i;
    }
    if (n <= 10) {
      long long ans = 0ll;
      do {
        long long num = 0ll;
        for (int i = 1; i <= n; i++) {
          if (id[i] <= k[i])
            num += (long long)l[i];
          else
            num += (long long)r[i];
        }
        ans = max(ans, num);
      } while (next_permutation(id + 1, id + n + 1));
      cout << ans << endl;
      continue;
    }
    sort(id + 1, id + n + 1, cmp);
    for (int i = 0; i <= n + 1; i++) {
      p1[i] = p2[i] = i;
    }
    long long ans = 0ll;
    for (int i = 1; i <= n; i++) {
      if (l[id[i]] > r[id[i]]) {
        int idx = find_set(p1, k[id[i]]);
        if (idx == 0) {
          int nw = find_set(p2, k[id[i]] + 1);
          ans += (long long)r[id[i]];
          p1[nw] = nw - 1;
          p2[nw] = nw + 1;
        } else {
          ans += (long long)l[id[i]];
          p1[idx] = idx - 1;
          p2[idx] = idx + 1;
        }
      } else {
        int idx = find_set(p2, k[id[i]] + 1);
        if (idx == n + 1) {
          int nw = find_set(p1, k[id[i]]);
          ans += (long long)l[id[i]];
          p1[nw] = nw - 1;
          p2[nw] = nw + 1;
        } else {
          ans += (long long)r[id[i]];
          p1[idx] = idx - 1;
          p2[idx] = idx + 1;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
