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

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t;
    cin >> t;

    for (int ti = 0; ti < t; ++ti)
    {
        int n;
        cin >> n;

        map <int, long long> cnt;
        map <pair <int, int>, long long> mp;
        vector <pair <int, int>> v(n);
        for (int i = 0; i < n; ++i)
        {
            int a, b;
            long long c;
            cin >> a >> b >> c;
            if (a > b)
                swap(a, b);
            
            v[i].first = a;
            v[i].second = b;
            cnt[a] += c;
            cnt[b] += c;
            mp[pair(a, b)] = c;
        }

        long long m = cnt.begin()->second;
        for (auto it = cnt.begin(); it != cnt.end(); ++it)
            m = max(m, it->second);

        for (int i = 0; i < n; ++i)
        {
            int a = v[i].first;
            int b = v[i].second;
            long long c = mp[pair(a, b)];
            
            auto it = mp.lower_bound(pair(b, 0));
            //if (it != mp.end())
            //    cout << it->first.first << ' ' << it->first.second << '\n';
            //else
            //    cout << "END\n";
            for (; it != mp.end() && it->first.first == b; ++it)
            {
                int x = it->first.second;
                auto el = mp.find(pair(a, x));
                if (el != mp.end())
                {
                    long long s = c + it->second + el->second;
                    //cout << ">>> s = " << s << '\n';
                    m = max(m, s);
                }
            }
        }
        
        cout << m << '\n';
    }
}
