#include <bits/stdc++.h>
using namespace std;
inline void pisz(int n) { printf("%d\n", n); }
const int dr[4] = {-1, 0, 1, 0};
const int dc[4] = {0, -1, 0, 1};
const int drr[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
const int dcc[8] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename TT>
ostream& operator<<(ostream& s, pair<T, TT> t) {
  return s << "(" << t.first << "," << t.second << ")";
}
template <typename T>
ostream& operator<<(ostream& s, vector<T> t) {
  for (int i = 0; i < ((int)((t).size())); i++) s << t[i] << " ";
  return s;
}
class UnionFind {
 private:
  vector<int> rank, p;

 public:
  UnionFind(int n) {
    for (int i = 0; i < n; i++) p.push_back(i);
    rank.assign(n, 0);
  }
  int findSet(int s) { return (p[s] == s) ? s : (p[s] = findSet(p[s])); }
  bool isSameSet(int i, int j) { return findSet(i) == findSet(j); }
  void unionSet(int i, int j) {
    if (isSameSet(i, j)) return;
    int x = findSet(i), y = findSet(j);
    if (rank[x] > rank[y])
      p[y] = x;
    else {
      p[x] = y;
      if (rank[x] == rank[y]) rank[y]++;
    }
  }
};
double x_y(int x1, int y1, int x2, int y2) {
  return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
int main() {
  int(testow);
  scanf("%d", &(testow));
  while (testow--) {
    int(n);
    scanf("%d", &(n));
    int dt[2][n], spl[n], res[n];
    set<int> usedl, usedr, spacesl, spacesr;
    priority_queue<pair<pair<int, int>, int> > pq;
    long long ans = 0;
    for (int i = 0; i < n; i++) {
      scanf("%d%d%d", &spl[i], &dt[1][i], &dt[0][i]);
      spl[i]--;
      pq.push(make_pair(
          make_pair(-abs(dt[0][i] - dt[1][i]), dt[0][i] > dt[1][i] ? 0 : 1),
          i));
      spacesl.insert(-i);
      spacesr.insert(i);
    }
    memset(res, 0, sizeof(res));
    while (!pq.empty()) {
      int lft = pq.top().first.second, ind = pq.top().second;
      pq.pop();
      if (lft) {
        auto it = spacesl.lower_bound(-spl[ind]);
        if (it != spacesl.end()) {
          ans += (dt[0][ind] < dt[1][ind] ? dt[1][ind] : dt[0][ind]);
          int pt = *it;
          spacesl.erase(pt);
          spacesr.erase(-pt);
          usedl.insert(pt);
          continue;
        }
        if (!usedr.empty()) {
          auto it2 = usedr.begin();
          if (*it2 > spl[ind]) {
            ans += min(dt[0][ind], dt[1][ind]);
            continue;
          }
          if (!spacesr.empty()) {
            auto it3 = spacesr.lower_bound(spl[ind]);
            if (it3 == spacesr.end()) {
              ans += min(dt[0][ind], dt[1][ind]);
              continue;
            }
            int pt = *it3;
            int pt2 = *it2;
            spacesr.erase(pt);
            spacesl.erase(-pt);
            usedr.erase(pt2);
            usedr.insert(-pt);
            usedl.insert(-(pt2));
            ans += (dt[0][ind] < dt[1][ind] ? dt[1][ind] : dt[0][ind]);
          } else {
            ans += min(dt[0][ind], dt[1][ind]);
            continue;
          }
        } else {
          ans += min(dt[0][ind], dt[1][ind]);
          continue;
        }
      } else {
        auto it = spacesr.lower_bound(spl[ind] + 1);
        if (it != spacesr.end()) {
          ans += (dt[0][ind] < dt[1][ind] ? dt[1][ind] : dt[0][ind]);
          int pt = *it;
          spacesr.erase(pt);
          spacesl.erase(-pt);
          usedr.insert(pt);
          continue;
        }
        if (!usedl.empty()) {
          auto it2 = usedl.begin();
          int pt2 = *it2;
          if ((-pt2) < spl[ind] + 1) {
            ans += min(dt[0][ind], dt[1][ind]);
            continue;
          }
          if (!spacesl.empty()) {
            auto it3 = spacesl.lower_bound(-spl[ind] - 1);
            if (it3 == spacesl.end()) {
              ans += min(dt[0][ind], dt[1][ind]);
              continue;
            }
            int pt = *it3;
            spacesl.erase(pt);
            spacesr.erase(-pt);
            usedl.erase(pt2);
            usedl.insert(-pt);
            usedr.insert(-(pt2));
            ans += (dt[0][ind] < dt[1][ind] ? dt[1][ind] : dt[0][ind]);
          } else {
            ans += min(dt[0][ind], dt[1][ind]);
            continue;
          }
        } else {
          ans += min(dt[0][ind], dt[1][ind]);
          continue;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
