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

using namespace std;
using namespace __gnu_pbds;


#define int 					long long int
#define f(i,x,n)			    for(int i=x;i<n;++i)

// ============================================================================================================

class Graph {

	int V; //vertices
	list<pair<int, int>> *l; //adjacency list list of pointers to list
	// since it is a weighted graph hence we are storeing pair
	//x --> (y,cost)

public:
	Graph(int v) {
		V = v;
		l = new list<pair<int, int>>[V];
	}

	void addEdge(int u, int v, int cost) {
		l[u].push_back({v, cost}); //Segmentation fault
		l[v].push_back({u, cost});
	}

	int min(int a, int b) {
		if (a > b) return b;
		else return a;
	}

	int dfsHelper(int n, bool *visited, int *count, int &ans) {

		visited[n] = true;
		int res;
		for (auto i = l[n].begin(); i != l[n].end(); ++i) {
			if (!visited[n]) {
				res = dfsHelper((*i).first, visited, count, ans);
				ans += 2 * ((*i).second) * min(res, V - res);
				count[n] += res;
			}
		}
		return count[n];
	}

	int dfs() {
		bool *visited = new bool[V];
		int *count = new int[V]; // count is for storing the count
		// of each node

		for (int i = 0; i < V; ++i) {
			visited[i] = false;
			count[i] = 1;
		}

		int ans = 0;
		dfsHelper(0, visited, count, ans);
		return ans;
	}
};

int32_t main() {

	int t;
	cin >> t;
	while (t--) {
		int n;
		cin >> n;

		int x, y, z;
		Graph g(n);
		f(i, 0, n - 1) {
			cin >> x >> y >> z;
			g.addEdge(x, y, z);
		}

		int ans = g.dfs();

		cout << ans << endl;
	}
	return 0;
}
