#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL MOD = 1000000007;
#define lcm(x, y) ((x) * (y) / __gcd((x), (y)))
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define debug(x) cout << #x << " : " << x << endl
#define part cout << "----------------------------------\n";
#include <iostream>

int dx[] = {1, 1, 0, -1, -1, -1, 0, 1}; // trick to explore an implicit 2D grid
int dy[] = {0, 1, 1, 1, 0, -1, -1, -1}; // S,SE,E,NE,N,NW,W,SW neighbors  //GO FOR EVEN FOR 4 moves

#define fastinput                     \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL);

void solve()
{
    LL n, i, j, k, t, temp;
    cin >> n;
    //debug(n);
    vector<LL> left, right, pref;
    vector<int> visited(n + 1, -1);
    map<int, vector<pair<int, int>>> start;
    map<int, vector<pair<int, int>>> last;
    left.resize(n + 1);
    right.resize(n + 1);
    visited.resize(n + 1);
    pref.resize(n + 1);
    priority_queue<pair<int, int>> q;
    for (i = 1; i <= n; i++)
    {
        cin >> k >> left[i] >> right[i];
        pref[i] = k;
        if (left[i] > right[i])
        {
            start[k].pb({left[i] - right[i], i});
        }
        else if (left[i] != right[i])
        {
            last[k+1].pb({-left[i] + right[i], i});
        }
    }
    vector<LL> allot(n + 1, -1);
    LL final = 0;
    for (i = n; i >= 1; i--)
    {
        LL now = i;
        for (auto x : start[now])
        {
            q.push(x);
        }

        if (!q.empty())
        {
            auto x = q.top();
            LL top_val = x.first;
            LL idx = x.second;
            q.pop();
            //cout<<"setting "<<i<<" as "<<idx<<endl;
            allot[i] = idx;
            visited[idx] = i;
        }
    }
    LL prev = -1;
    /*cout << "allot before is " << endl;
    for (i = 1; i <= n; i++)
    {
        cout << allot[i] << " ";
    }
    cout << endl;*/
    LL curr = 1;
    for (i = 1; i <= n; i++)
    {
        if (allot[i] == -1)
        {
            //cout<<"hunter is "<<i<<endl;
            curr=max(curr,i+1);
            while (curr <= n && allot[curr] == -1)
            {
                curr++;
            }
           // debug(curr);

            if (curr <= n)
            {
                LL idx_trans = allot[curr];
                allot[i] = idx_trans;
                allot[curr] = -1;
                visited[idx_trans] = i;
            }
        }
    }
    /*cout << "allot after is " << endl;
    for (i = 1; i <= n; i++)
    {
        cout << allot[i] << " ";
    }
    cout << endl;*/
    while (!q.empty())
    {
        q.pop();
    }
    for (i = 1; i <= n; i++)
    {
        LL now = i;
        for (auto x : last[now])
        {
            q.push(x);
        }

        if (!q.empty() && allot[i] == -1)
        {
            auto x = q.top();
            LL top_val = x.first;
            LL idx = x.second;
            q.pop();
            allot[i] = idx;
            visited[idx] = i;
        }
    }
   /* cout << "final allot is " << endl;
    for (i = 1; i <= n; i++)
    {
        cout << allot[i] << " ";
    }
    cout << endl;
    LL ans = 0;
    for (i = 1; i <= n; i++)
    {
        LL a1 = min(left[i], right[i]);
        LL a2 = left[i] + right[i] - a1;
        debug(i);
        if (visited[i] == -1)
        {
            cout << "failed" << endl;
            cout << "adding " << a1 << endl;
            ans += a1;
        }
        else
        {
            ans += a2;
            cout << "adding " << a2 << endl;
        }
        part;
    }*/
    LL ans2 = 0;
    LL addend = 0;
    /*cout<<"visiteds are "<<endl;
    for(i=1;i<=n;i++)
    {
        cout<<visited[i]<<" ";
    }
    cout<<endl;*/
    for (i = 1; i <= n; i++)
    {
        //debug(i);
        if (left[i] > right[i])
        {
            if (visited[i] == -1)
            {
                addend = min(left[i], right[i]);
            }
            else if (visited[i] <= pref[i])
            {
                addend = left[i];
            }
            else
            {
                addend = right[i];
            }
        }
        else if (left[i] < right[i])
        {
            if (visited[i] == -1)
            {
                addend = min(left[i], right[i]);
            }
            else if (visited[i] > pref[i])
            {
                addend = right[i];
            }
            else
            {
                addend = left[i];
            }
        }
        else
        {

            addend = min(left[i], right[i]);
        }
       // debug(addend);
        //part;
        ans2+=addend;
    }
    //cout << ans << endl;
    cout<<ans2<<endl;
    //cout << "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" << endl;
}

int main()
{
    fastinput;
    LL n, i, j, k, t, temp, tc;
    cin >> tc;
    while (tc--)
    {
        solve();
    }

    return 0;
}


Measured memory_bytes: 46848