#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <random>

using namespace std;

#define int long long

int inf = 1000000000;

mt19937 rnd_(12);


vector <vector <int>> g;

int hs(int u , int v) {
    return u*inf+ v;
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);


    int times;
    cin >> times;

    while (times--) {
        int n;
        cin >> n;

        map <pair <int , int> , int> mp;


        vector <vector <pair <int , int>>> g(2 * n);
        vector <int> sum(2 * n);
        for (int i= 0 ; i < n ; i++) {
            int a , b , c;
            cin >> a >> b >> c;

            a--;
            b--;


            sum[a] += c;
            sum[b] += c;

            mp[{a , b}]  =c;
            mp[{b , a}]  =c;

            g[a].push_back({c , b});
            g[b].push_back({c , a});
        }

        for (int i = 0 ; i < g[i].size() ; i++) {
           if (g[i].size() < 2)
                continue;

            sort(g[i].begin() , g[i].end());
            reverse(g[i].begin() , g[i].end());
        }

        int ans = 0;

        for (int i= 0 ; i < 2 * n ; i++)
            ans= max(ans , sum[i]);

        for (int i = 0 ; i <2*n ;  i++) {
            if (g[i].size() < 2)
                continue;

            int i1 = g[i][0].second;
            int i2 = g[i][1].second;
            int w1 = g[i][0].first;
            int w2 = g[i][1].first;

            ans = max(ans , w1+w2 + mp[{i1 , i2}]);
        }

        cout << ans << "\n";
    }
}