#include <bits/stdc++.h>
using namespace std;

struct S{
    long long a, b, c;
};

long long n, ans;
vector<S> arr;
vector<long long> v1;
vector<S> v2;

long long check1(){
    v1.clear();
    v1.resize(2*n+5);
    for (int i = 0; i < n; i++){
        v1[arr[i].a] += arr[i].c;
        v1[arr[i].b] += arr[i].c;
    }
    long long res = 0;
    for (int i = 1; i <= 2*n; i++)
        res =  max(res, v1[i]);
    return res;
}

bool cmp(S s1, S s2){
    if (s1.a != s2.a)
        return (s1.a < s2.a);
    return (s1.b < s2.b);
}
long long check2(){
    v2.clear();
    long long res = 0, ma1 = arr[0].c, ma2 = arr[1].c;
    if (ma1<ma2) swap(ma1, ma2);
    for (int i = 2; i < n; i++){
        if (arr[i].c > ma2){
            if (arr[i].c > ma1){
                ma2 = ma1;
                ma1 = arr[i].c;
            }
            else ma2 = arr[i].c;
        }
    }
    ma1 += ma2;
    for (auto it = arr.begin(); it != arr.end(); it++)
        if (ma1 + it->c > ans){
            v2.push_back(*it);
        }
    if (v2.size() < 3) return 0;
    sort(v2.begin(), v2.end(), cmp);

    for (int i = 0; i < n-2; i++){
        int p1 = i+1, p2 = i+2;
        while (v2[p2].a < v2[i].b && p2 < n) p2++;
        while (1){
            if (p1 == p2) p2++;
            if (p2 >= n) break;
            if (v2[p1].a > v2[i].a) break;
            if (v2[p2].a > v2[i].b) break;
            if (v2[p1].b > v2[p2].b){
                p2++;
                continue;
            }
            if (v2[p1].b < v2[p2].b){
                p1++;
                continue;
            }
            res = max(res, v2[i].c+v2[p1].c+v2[p2].c);
            p1++;
        }
    }
    return res;
}

int main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);

    long long T = 1;
    cin >> T;
    for (int i = 0; i < T; i++){
        ans = 0;
        arr.clear();


        cin >> n;
        arr.resize(n);

        for (int j = 0; j < n; j++)
            cin >> arr[j].a >> arr[j].b >> arr[j].c;
        ans = check1();
        cout << ans << endl;
    }
    return 0;
}




