#include <bits/stdc++.h>
#define int long long
#define elif else if
#define pb push_back

using namespace std;
vector<vector<int>> adj;

void solve(){
    int n; cin >> n;

    vector<pair<pair<int, int>, int>> bb;
    map<int, int> aa;
    for(int i = 0; i < n; i++){
        int a, b, c; cin >> a >> b >> c;
        bb.push_back({{a, b}, c});
    }
    map<int, int> gg;

    for(int i = 0; i < n; i++){
        int x1 = bb[i].first.first;
        int x2 = bb[i].first.second;
        gg[x1]++;
        gg[x2]++;
    }
    int mx = 0;
    for(auto x: gg){
        mx = max(mx, x.second);
    }
    int ans = 0;
    if(mx == 1){
        int cnt = 0;
        for(auto x: bb){
            cnt = max(cnt, x.second);
        }
        cout << cnt << endl;
        return;
    }
    for(auto x: gg){
        if((x.second == mx) || (x.second == mx-1)){
            ans += bb[x.first].second;
        }
    }
    cout << ans << endl;



}

signed main()
{
    int t = 1; cin >> t;
    while(t--) solve();
    return 0;
}
