#include <bits/stdc++.h>
#define IO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define DB(x) cerr << __LINE__ << ": " << #x << " = " << (x) << "\n"
using namespace std;
// using ll = long long;
#define int long long

int n;

void update(int x, int delta, vector<int>& BIT) {      //add "delta" at index "x"
    for( ; x <= n; x += x & -x) {
        BIT[x] += delta;
    }
}

int query(int x, vector<int>& BIT) {     //returns the sum of first x elements in given array a[]
    int sum = 0;
    for( ; x > 0; x -= x & -x) {
        sum += BIT[x];
    }
    return sum;
}

int back_query(int x, vector<int>& BIT) {      //returns the sum of last x elements in given array a[]
    return (query(n, BIT) - query(x - 1, BIT));
}

signed main() {
    IO;
    int tests = 1;
    cin >> tests;
    for(int test = 1; test <= tests; ++test) {
        cin >> n;
        vector<int> da[n];
        vector<pair<int, int> > up, down; 
        vector<int> filled(n + 1, -1);
        vector<int> BIT(n + 1, 0);
        for(int i = 1; i <= n; ++i) update(i, 1, BIT);
        int ans = 0;
        for(int i = 0; i < n; ++i) {
        	int k, l, r;
        	cin >> k >> l >> r;
        	da[i].push_back(k); da[i].push_back(l); da[i].push_back(r);
        	if(l == r || k == n) {
        		ans += l;
        	}
        	else if(l > r) {
        		up.push_back({l - r, i});
        	}
        	else {
        		down.push_back({r - l, i});	
        	}
        }
        sort(up.begin(), up.end()); reverse(up.begin(), up.end());
        sort(down.begin(), down.end()); reverse(down.begin(), down.end());
        for(int i = 0; i < int(up.size()); ++i) {
        	if(query(da[up[i].second][0], BIT) > 0) {
        		int st = 0, low = 1, high = da[up[i].second][0];
        		while(low <= high) {
        			int mid = (low + high) / 2;
        			if(query(mid, BIT) > 0) {
        				st = mid;
        				low = mid + 1;
        			}
        			else high = mid - 1;
        		}
                assert(st != 0);
        		filled[st] = up[i].second;
        		update(st, 1, BIT);
        		ans += da[up[i].second][1];
        	}
        	else {
        		ans += da[up[i].second][2];
        	}
        }

        // reorder
        vector<int> pos; vector<pair<int, int> > cam;
        for(int i = 1; i <= n; ++i) {
        	if(filled[i] == -1) pos.push_back(i);
        	else cam.push_back({i, filled[i]});
        }
        reverse(cam.begin(), cam.end());
        int cpos = 0;
        if(!cam.empty()) {
	        for(int i = 0; i < int(pos.size()); ++i) {
	        	if(pos[i] < cam[cpos].first) {
	        		filled[pos[i]] = cam[cpos].second;
	        		filled[cam[cpos].first] = -1;
	        		update(cam[cpos].first, -1, BIT);
	        		update(pos[i], 1, BIT);
	        		++cpos;
	        	}
	        	else break;
	        	if(cpos == int(cam.size())) break;
	        }
    	}
        // reorder over
        
        for(int i = 0; i < int(down.size()); ++i) {
        	if(back_query(1 + da[down[i].second][0], BIT) > 0) {
        		int st = 0, low = n, high = da[down[i].second][0] + 1;
        		while(low >= high) {
        			int mid = (low + high) / 2;
        			// DB(mid);
                    if(back_query(mid, BIT) > 0) {
        				st = mid;
        				low = mid - 1;
        			}
        			else high = mid + 1;
        		}
                assert(st != 0);
        		filled[st] = down[i].second;
        		update(st, 1, BIT);
        		ans += da[down[i].second][2];

        	}
        	else {
        		ans += da[down[i].second][1];
        	}
        }
        cout << ans << "\n";
    }
    return 0;
}