#include <bits/stdc++.h>
using namespace std;
template <class T>
void binstf(T &found, T a, T b, function<bool(T)> test, bool TF = true) {
  T i = a, j = b;
  bool da = test(a), double = test(b);
  if ((da != TF) or (double == TF)) {
    cerr << "---[BS: TF = " << TF << "]---" << endl;
    cerr << a << "->" << da << endl;
    cerr << b << "->" << double << endl;
  }
  while (j - i > 1) {
    T m = (i + j) / 2;
    if (test(m) == TF)
      i = m;
    else
      j = m;
  }
  found = i;
  if (not TF) found++;
}
template <class T>
T dpow(T a, T power) {
  T result = 1;
  while (power) {
    if (power % 2) result *= a;
    power /= 2, a = a * a;
  }
  return result;
}
const long double PI = acos((long double)-1);
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
template <class T>
bool ckmin(T &a, const T &b) {
  return b < a ? a = b, 1 : 0;
}
template <class T>
bool ckmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <class T>
bool reorder(T &a, T &b) {
  return (a > b) ? swap(a, b), 1 : 0;
}
template <class A>
void re(complex<A> &c);
template <class A, class B>
void re(pair<A, B> &p);
template <class A>
void re(vector<A> &v);
template <class A, size_t SZ>
void re(array<A, SZ> &a);
template <class T>
void re(T &x) {
  cin >> x;
}
void re(double &d) {
  string t;
  re(t);
  d = stod(t);
}
void re(long double &d) {
  string t;
  re(t);
  d = stold(t);
}
template <class H, class... T>
void re(H &h, T &...t) {
  re(h);
  re(t...);
}
template <class A>
void re(complex<A> &c) {
  A a, b;
  re(a, b);
  c = {a, b};
}
template <class A, class B>
void re(pair<A, B> &p) {
  re(p.first, p.second);
}
template <class A>
void re(vector<A> &x) {
  for (auto &a : x) re(a);
}
template <class A, size_t SZ>
void re(array<A, SZ> &x) {
  for (auto &a : x) re(a);
}
template <class T>
T gcd(T a, T b) {
  a = a < 0 ? -a : a;
  b = b < 0 ? -b : b;
  if (a * b == 0)
    return max(a, b);
  else
    return gcd(min(a, b), max(a, b) % min(a, b));
}
const int MODE107 = 1000000007;
template <class T>
T modop(T a, T mode = MODE107) {
  a = a % mode;
  if (a < 0) a += mode;
  return a;
}
template <class T>
T inv(T a, T mode = MODE107) {
  T coefa = 1, coefb = 0;
  T aa = a, bb = mode;
  while (aa != 1) {
    if (aa > bb) swap(coefa, coefb), swap(aa, bb);
    T cc = bb / aa;
    bb -= cc * aa, coefb -= cc * coefa;
    swap(coefa, coefb), swap(aa, bb);
  }
  return coefa;
}
const int uplrDX[] = {-1, 1, 0, 0};
const int uplrDY[] = {0, 0, -1, 1};
string to_string(char c) { return string(1, c); }
string to_string(bool b) { return b ? "true" : "false"; }
string to_string(const char *second) { return (string)second; }
string to_string(string second) { return second; }
template <class A>
string to_string(complex<A> c) {
  stringstream ss;
  ss << c;
  return ss.string();
}
string to_string(vector<bool> v) {
  string res = "{";
  for (int i = (0); i < ((int)v.size()); ++i) res += char('0' + v[i]);
  res += "}";
  return res;
}
template <size_t SZ>
string to_string(bitset<SZ> b) {
  string res = "";
  for (int i = (0); i < (SZ); ++i) res += char('0' + b[i]);
  return res;
}
template <class A, class B>
string to_string(pair<A, B> p);
template <class T>
string to_string(T v) {
  bool fst = 1;
  string res = "{";
  for (const auto &x : v) {
    if (!fst) res += ", ";
    fst = 0;
    res += to_string(x);
  }
  res += "}";
  return res;
}
template <class A, class B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <class A>
void pr(A x) {
  cout << to_string(x);
}
template <class H, class... T>
void pr(const H &h, const T &...t) {
  pr(h);
  pr(t...);
}
void ps() { pr("\n"); }
template <class H, class... T>
void ps(const H &h, const T &...t) {
  pr(h);
  if (sizeof...(t)) pr(" ");
  ps(t...);
}
void DBG() { cerr << "]" << endl; }
template <class H, class... T>
void DBG(H h, T... t) {
  cerr << to_string(h);
  if (sizeof...(t)) cerr << ", ";
  DBG(t...);
}
int sgn(int a) { return (a > 0) - (a < 0); }
int sq(int a) { return a * a; }
namespace Point {
int norm(const pair<int, int> &p) { return sq(p.first) + sq(p.second); }
int abs(const pair<int, int> &p) { return sqrt(norm(p)); }
int arg(const pair<int, int> &p) { return atan2(p.second, p.first); }
pair<int, int> conj(const pair<int, int> &p) {
  return pair<int, int>(p.first, -p.second);
}
pair<int, int> perp(const pair<int, int> &p) {
  return pair<int, int>(-p.second, p.first);
}
pair<int, int> dir(int ang) { return pair<int, int>(cos(ang), sin(ang)); }
pair<int, int> operator-(const pair<int, int> &l) {
  return pair<int, int>(-l.first, -l.second);
}
pair<int, int> operator+(const pair<int, int> &l, const pair<int, int> &r) {
  return pair<int, int>(l.first + r.first, l.second + r.second);
}
pair<int, int> operator-(const pair<int, int> &l, const pair<int, int> &r) {
  return pair<int, int>(l.first - r.first, l.second - r.second);
}
pair<int, int> operator*(const pair<int, int> &l, const int &r) {
  return pair<int, int>(l.first * r, l.second * r);
}
pair<int, int> operator*(const int &l, const pair<int, int> &r) {
  return r * l;
}
pair<int, int> operator/(const pair<int, int> &l, const int &r) {
  return pair<int, int>(l.first / r, l.second / r);
}
pair<int, int> operator*(const pair<int, int> &l, const pair<int, int> &r) {
  return pair<int, int>(l.first * r.first - l.second * r.second,
                        l.second * r.first + l.first * r.second);
}
pair<int, int> operator/(const pair<int, int> &l, const pair<int, int> &r) {
  return l * conj(r) / norm(r);
}
pair<int, int> &operator+=(pair<int, int> &l, const pair<int, int> &r) {
  return l = l + r;
}
pair<int, int> &operator-=(pair<int, int> &l, const pair<int, int> &r) {
  return l = l - r;
}
pair<int, int> &operator*=(pair<int, int> &l, const int &r) {
  return l = l * r;
}
pair<int, int> &operator/=(pair<int, int> &l, const int &r) {
  return l = l / r;
}
pair<int, int> &operator*=(pair<int, int> &l, const pair<int, int> &r) {
  return l = l * r;
}
pair<int, int> &operator/=(pair<int, int> &l, const pair<int, int> &r) {
  return l = l / r;
}
pair<int, int> unit(const pair<int, int> &p) { return p / abs(p); }
int dot(const pair<int, int> &a, const pair<int, int> &b) {
  return a.first * b.first + a.second * b.second;
}
int cross(const pair<int, int> &a, const pair<int, int> &b) {
  return a.first * b.second - a.second * b.first;
}
int cross(const pair<int, int> &p, const pair<int, int> &a,
          const pair<int, int> &b) {
  return cross(a - p, b - p);
}
pair<int, int> reflect(const pair<int, int> &p, const pair<int, int> &a,
                       const pair<int, int> &b) {
  return a + conj((p - a) / (b - a)) * (b - a);
}
pair<int, int> foot(const pair<int, int> &p, const pair<int, int> &a,
                    const pair<int, int> &b) {
  return (p + reflect(p, a, b)) / (int)2;
}
bool onSeg(const pair<int, int> &p, const pair<int, int> &a,
           const pair<int, int> &b) {
  return cross(a, b, p) == 0 && dot(p - a, p - b) <= 0;
}
ostream &operator<<(ostream &os, const pair<int, int> &p) {
  return os << "(" << p.first << "," << p.second << ")";
}
};  // namespace Point
class trie {
 public:
  map<char, trie> l;
  void add(string t) {
    if (t.length() > 0) {
      l[t[0]].add(t.substr(1));
    }
  }
  bool contains(string t) {
    if (not l.count(t[0])) return false;
    return l[t[0]].contains(t.substr(1));
  }
};
template <class int>
class rq {
  vector<vector<int>> content;
  function<int(int, int)> op;

 public:
  rq(vector<int> &orig, function<int(int, int)> operation) {
    op = operation;
    int size = 1;
    content.emplace_back(orig);
    while (size < orig.size()) {
      size *= 2;
      content.emplace_back(0);
      auto &todo = content.back(), &from = content[content.size() - 2];
      for (int i = (0); i < ((int)from.size() - size / 2); ++i) {
        todo.emplace_back(op(from[i], from[i + size / 2]));
      }
    }
  }
  int query(int i, int j) {
    if (j <= i or j > content[0].size() or i < 0 or i >= content[0].size()) {
      cerr << "i=" << i << endl;
      cerr << "j=" << j << endl;
      throw "bad i j";
    }
    tie(i, j) = make_pair(min(i, j), max(i, j));
    int size = 1, pow = 0;
    while (size * 2 < j - i) {
      size *= 2, pow++;
    }
    assert(pow < content.size());
    auto &c = content[pow];
    assert(i < c.size() and i >= 0);
    assert(j - size < c.size() and j - size >= 0);
    int dbg = op(content[pow][i], content[pow][j - size]);
    return dbg;
  }
};
template <class int>
class maxflow {
 public:
  int src, drn;
  vector<vector<int>> adj, radj;
  map<pair<int, int>, int> lims;
  map<pair<int, int>, int> rlims;
  void genradj() {
    radj.resize((int)adj.size());
    for (int from = (0); from < ((int)adj.size()); ++from) {
      for (auto &to : adj[from]) {
        radj[to].emplace_back(from);
      }
    }
  }
  int solve() {
    while (true) {
      map<int, pair<pair<int, int>, int>> backtrack;
      bool found = false;
      set<int> seen;
      queue<pair<int, int>> todo;
      todo.emplace(src, -1);
      while (not todo.empty()) {
        int from = todo.front().first;
        int bottleneck = todo.front().second;
        todo.pop();
        if (from == drn) {
          found = true;
          break;
        }
        for (auto &to : adj[from]) {
          pair<int, int> edge = make_pair(from, to);
          int bn = min(bottleneck, lims[edge]);
          if (bn and not seen.count(to)) {
            seen.emplace(to);
            todo.emplace(to, bn);
            backtrack[to] = make_pair(edge, bn);
          }
        }
        for (auto &to : radj[from]) {
          pair<int, int> edge = make_pair(to, from);
          int bn = min(bottleneck, rlims[edge]);
          if (bn and not seen.count(to)) {
            seen.emplace(to);
            todo.emplace(to, bn);
            backtrack[to] = make_pair(edge, bn);
          }
        }
      }
      if (not found)
        break;
      else {
        list<pair<int, int>> edges;
        int to = drn;
        int flow = backtrack[to].second;
        while (to != src) {
          pair<int, int> edge = backtrack[to].first;
          int from = edge.first + edge.second - to;
          edges.emplace_front(edge);
          to = from;
        }
        int from = src;
        for (auto &e : edges) {
          if (e.first == from)
            rlims[e] += flow, lims[e] -= flow;
          else
            rlims[e] -= flow, lims[e] += flow;
          from = e.first + e.second - from;
        }
      }
    }
    int res = 0;
    for (auto &to : adj[src]) res += rlims[make_pair(src, to)];
    return res;
  }
};
auto __tic__ = chrono::high_resolution_clock::now();
void tic() { __tic__ = chrono::high_resolution_clock::now(); }
void toc() {
  ps(chrono::duration_cast<chrono::microseconds>(
         chrono::high_resolution_clock::now() - __tic__)
         .count(),
     "us");
  cout << endl;
}
void precalc() {}
void solve(int ti) {
  int n;
  n = 1 + rand() % 5000;
  vector<pair<int, int>> front, back;
  unsigned long long base = 0;
  for (int i = (0); i < (n); ++i) {
    int k, l, r;
    re(k, l, r);
    base += (k == n) ? l : min(l, r);
    if (l == r or k == n) continue;
    if (l > r) front.emplace_back(max(l, r) - min(l, r), k);
    if (l < r) back.emplace_back(max(l, r) - min(l, r), n - k);
  }
  function<bool(pair<int, int> a, pair<int, int> b)> sortbyrange =
      [&](pair<int, int> a, pair<int, int> b) {
        if (a.second < b.second) return true;
        if (a.second == b.second) return a.first < b.first;
        return false;
      };
  sort(begin(front), end(front), sortbyrange);
  sort(begin(back), end(back), sortbyrange);
  for (auto addr : {&front, &back}) {
    set<pair<pair<int, int>, int>> heap;
    auto &front_or_back = *addr;
    for (int i = (0); i < (front_or_back.size()); ++i) {
      int freespace = front_or_back[i].second;
      heap.emplace(front_or_back[i], i);
      if (heap.size() > freespace) heap.erase(*heap.begin());
    }
    for (auto &e : heap) base += e.first.first;
  }
  ps(base);
}
int main() {
  precalc();
  bool online = true;
  int int = 1;
  { re(int); }
  for (int i = (1); i < (int + 1); ++i) {
    solve(i);
  }
}
