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

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

ll t,n,g1,g2,ma,mi,s,k;
ll c[DIM];
vector<int> a[DIM], b[DIM];
set<int> res[DIM], ans;

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);
    }
}

bool check(ll b1, ll b2) {
    return (res[b1].find(b2)!=res[b1].end()) && (res[b2].find(b1)!=res[b2].end());
}

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();
        }
        ans.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++) {
            ans.insert(i);
        }
        for(int i=1;i<=n;i++) {

            for(int j=i+1; j<=n;j++) {
                if(check(i, j) != 1 && ans.find(i)!=ans.end() && ans.find(j)!=ans.end()) {
                        if(c[i]<c[j]) ans.erase(i);
                        else ans.erase(j);
                        break;
                }
            }
        }
        k=0;
        for(auto x:ans) {
            k+=c[x];
        }
        cout<<k<<endl;
    }
    /*t = (int) rand()%10+1;
    cout<<t<<endl;
    while(t--) {
        n = (int) rand()%10+1;
        cout<<n<<endl;
        for(int i=1;i<=n;i++) {
            cout<<(int) rand()%10+1<<" "<< (int) rand()%10+1<< " "<< (int) rand()%20+1<<endl;
        }
    }*/
}

