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

typedef long long ll;
const ll DIM = 14e5+7;

ll t,n,g1,g2,ma,mi,s;
ll c[DIM];
vector<int> a[DIM], b[DIM];
set<int> res[DIM];
void dfs(int v, int h, int cur) {
    if(h==0){
        for(auto to:a[v]) {
            dfs(to, h+1, cur);
        }
    }
    else if(h==1) {
        for(auto to:b[v]) {
            dfs(to, h+1, cur);
        }
    }
    else if(h==2) {
        res[cur].insert(v);
    }
}

int main() {
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin>>t;
    while(t--) {
        for(int i=1;i<=n;i++) {
            a[i].clear();
            b[i].clear();
            res[i].clear();
        }
        cin>>n;
        for(int i=1;i<=n;i++) {
            cin>>g1>>g2>>c[i];
            a[i].push_back(g1);
            a[i].push_back(g2);
            b[g1].push_back(i);
            b[g2].push_back(i);

        }

        for(int i=1;i<=n;i++) {
            dfs(i, 0, i);
        }



        for(int i=1;i<=n;i++) {
            s=0;
            for(auto x:res[i]) {
                s+=c[x];
            }
            if(s>ma) {
                ma=s;
                mi=i;
            }
        }
        cout<<ma<<endl;
    }
}

