// Assessment: medium-hard
// Estimated time: 20 min thinking + 40 min coding = 60 min

#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;

const int MAXN = 1000000;

typedef vector<pair<int, int> > Adj;
vector<Adj> adj;    // List of adjacent node indices

int memo1[MAXN];
double memo2[MAXN], memo3[MAXN];

/* Returns the number of nodes in the subtree starting at cur */
int num_points(int cur, int parent)
{
    int &res = memo1[cur];
    if(res < 0)
    {
        res = 1;
        for(Adj::const_iterator i = adj[cur].begin(); i != adj[cur].end(); ++i)
            if(i->first != parent)
                res += num_points(i->first, cur);
    }
    return res;
}

/* Returns the sum of dist(i,cur) for all i in the subtree starting at cur */
double sum_depth(int cur, int parent)
{
    double &res = memo2[cur];
    if(res < 0)
    {
        res = 0;
        for(Adj::const_iterator i = adj[cur].begin(); i != adj[cur].end(); ++i)
            if(i->first != parent)
                res += sum_depth(i->first, cur) +
                       (i->second)*num_points(i->first, cur);
    }
    return res;
}

/* Returns the sum of dist(i,j) for all i,j in the subtree starting at cur */
double sum_dist(int cur, int parent)
{
    double &res = memo3[cur];
    if(res < 0)
    {
        res = 0;
        double D = 0;
        int N = 1;
        for(Adj::const_iterator i = adj[cur].begin(); i != adj[cur].end(); ++i)
            if(i->first != parent)
            {
                res += sum_dist(i->first, cur);
                double d = sum_depth(i->first, cur) +
                           num_points(i->first, cur)*i->second;
                int    n = num_points(i->first, cur);
                res += D*n + d*N;
                D += d, N += n;
            }
    }
    return res;
}

int main()
{
    int cases;
    cin >> cases;
    while(cases--)
    {
        int N;
        cin >> N;
        adj = vector<Adj>(N);
        for(int n = 0; n < N - 1; ++n)
        {
            int a, b, d;
            cin >> a >> b >> d;
            adj[a].push_back(make_pair(b, d));
            adj[b].push_back(make_pair(a, d));
        }
        for(int n = 0; n < N; ++n)
            memo1[n] = -1, memo2[n] = memo3[n] = -1.0;

        /* The average path length is the sum of dist(i,j) for all nodes
           i, j in the tree, divided by the number of pairs i,j
           (N*(N-1)/2) */
        cout << setprecision(10) << sum_dist(0, -1)*2/N/(N-1) << '\n';
    }
	return 0;
}
