#include <bits/stdc++.h>
#include <iostream>

using namespace std;
void solve()
{
int t, n, i, e;
        long long dist, j, ma, q, d;
        cin >> t;
        for(i = 0; i < t; i++)
        {
                ma = 0;
                dist = 0;
                cin >> n;
                long long w[n][3];
                for(j = 0; j < n; j++)
                {
                        for(e = 0; e < 3; e++)
                                cin >> w[j][e];
                        ma = max(ma, max(w[j][0], w[j][1]));
                }
                for(j = 0; j < n; j++)
                {
                        if(w[j][0] > ma)
                        {
                                ma  = w[j][0];
                        }
                        if(w[j][1] > ma)
                        {
                                ma = w[j][1];
                        }
                }
                //cout << ma << endl;
                vector<vector<pair<long long, long long>>> g(ma);
                g.clear();
                for(auto& v: w)
                {
                     g[v[0] - 1].push_back({v[1] - 1, v[2]});
                     g[v[1] - 1].push_back({v[0] - 1, v[2]});
                }
                for(d = 0; d < ma; d++)
                {
                        if(g[d].size() != 0)
                        {
                              for(auto& s: g[d])
                              {
                                      for(auto& r: g[s.first])
                                      {
                                              for(j = 0; j < g[d].size(); j++)
                                              {
                                                      if(r.first == g[d][j].first)
                                                      {
                                                              dist = max(dist, g[d][j].second + r.second + s.second);
                                                      }
                                              }
                                      }
                              }
                        }
                }
                for(j = 0; j < ma; j++)
                {
                        q = 0;
                        for(auto& v: g[j])
                        {
                                q += v.second;
                        }
                        dist = max(q, dist);
                }
                cout << dist << endl;
                g.clear();
        }
}

int main()
{       
        solve();
        return 0;
}
