//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>

using namespace std;
//using namespace __gnu_pbds;

#define int long long

//typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

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

    map<int, vector<int>> mp;
    //c, a, b
    map<pair<int, int>, int> mmp;
    vector< pair<int, pair<int, int>> > bs(n + 1);
    for(int i = 1; i <= n; ++i) {
        int a, b, c;
        cin >> a >> b >> c;
        bs[i] = make_pair(c, make_pair(a, b));
        mp[a].emplace_back(i);
        mp[b].emplace_back(i);
        mmp[make_pair(a, b)] = c;
    }
    int ans = 0;
    for(auto& i : mp) {
        if((i.second).size() != 2) {
            int sm = 0;
            for(auto& j : i.second) sm += bs[j].first;
            ans = max(ans, sm);
        } else {
            set<int> st;
            for(auto& b : i.second) {
                st.insert(bs[b].second.first);
                st.insert(bs[b].second.second);
            }
            st.erase(i.first);
            int sm = 0;
            for(auto& j : i.second) sm += bs[j].first;
            sm += mmp[make_pair(*st.begin(), *st.rbegin())];
            ans = max(sm, ans);
        }
    }
    cout << ans << '\n';
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int tt = 1;
    cin >> tt;
    while(tt--) {
        solve();
    }
}
