#include <bits/stdc++.h>

using namespace std;

#define int long long

void solve() {
    int n;
    cin >> n;
    map<int, int> mp;
    map<pair<int, int>, int> mmp;
    map<int, vector<int>> lst;
    for(int i = 0; i < n; ++i) {
        int a, b, c;
        cin >> a >> b >> c;
        mp[a] += c;
        mp[b] += c;
        lst[a].emplace_back(b);
        lst[b].emplace_back(a);
        mmp[make_pair(a, b)] += c;
    }
    int ans = 0;
    for(auto& i : lst) {
        if((i.second).size() < 2) {
            ans = max(ans, mp[i.first]);
        } else {
            for(int j = 0; j < lst[i.first].size(); ++j) {
                for(int k = j + 1; k < lst[i.first].size(); ++k) {
                    int x = min(lst[i.first][j], lst[i.first][k]);
                    int y = max(lst[i.first][j], lst[i.first][k]);
                    ans = max(ans, mp[i.first] + mmp[make_pair(x, y)]);
                }
            }
        }
    }
    cout << ans << '\n';
}

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