
#include <bits/stdc++.h>
//#pragma GCC optimize("O3")
//#pragma GCC optimize("Ofast")

using namespace std;
typedef long long ll;
const ll dim=6007;
ll n,a[dim],T[dim],b[dim], c[dim],ma, G[dim], dep[dim];
string s;
map<pair<ll,ll>,ll>mp;
vector<ll>g[dim];
/*ll build(int v, int tl, int tr){
   if(tl==tr){
        T[v]=a[tl];
        return T[v];
   }
   int tm=(tl+tr)>>1;

    T[v]=build(2*v+1, tl, tm)+build(2*v+2, tm+1, tr);
}

ll get(int v, int tl, int tr, int l, int r)
void solve(){
    cin>>n;
}*/

void dfs(int v, int par){
    dep[v]=1+dep[par];


     for(auto to : a[v]){
          if(!used[to])dfs(to, v);
     }

       for(auto to : a[v]){
          if(dep[to]==dep[v]-2){
        
        
            ma=max(mp[{a[v], a[to]}]+mp[{a[v],a[par]}]+mp[{a[to],a[par]}], ma); //cout << "TEST: " << i << " " << p1 << " " << p2 << " " << p3 << " " << p4 << " " << mp[{p1,p2}]+mp[{p3,p4}] << endl;
          }
     }
}
void solve(){

          

    cin>>n;
     ma=0;

       mp.clear();
     for(int i=1; i<=2*n; i++){G[i]=0;}
    for(int i=1; i<=n; i++){cin>>a[i]>>b[i]>>c[i]; ma=max(ma, c[i]);
     G[a[i]]+=c[i];
     G[b[i]]+=c[i];
     //ist[a[i]].inser(G[a[]]);
      g[a[i]].push_back(b[i]);
      g[b[i]].push_back(a[i]);
    }
    for(int i=1; i<=2*n; i++){
        if(!dep[i])dfs(i, i);
    }
    for(int i=1; i<=2*n; i++)ma=max(ma, G[i]);

   for(int i=1; i<=n; i++){
      mp[{a[i], b[i]}]=c[i];
      mp[{b[i], a[i]}]=c[i];
   }
   
     

    cout << ma << endl;
}
int main()
{
ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    //t=1;
    cin>>t;
    while(t--)solve();

    return 0;
}
