#P1916G. Optimizations From Chelsu

    ID: 8937 Type: RemoteJudge 2000ms 256MiB Tried: 0 Accepted: 0 Difficulty: 10 Uploaded By: Tags>divide and conquerdpnumber theorytrees*3500

Optimizations From Chelsu

No submission language available for this problem.

Description

You are given a tree with nn vertices, whose vertices are numbered from 11 to nn. Each edge is labeled with some integer wiw_i.

Define len(u,v)len(u, v) as the number of edges in the simple path between vertices uu and vv, and gcd(u,v)gcd(u, v) as the Greatest Common Divisor of all numbers written on the edges of the simple path between vertices uu and vv. For example, len(u,u)=0len(u, u) = 0 and gcd(u,u)=0gcd(u, u) = 0 for any 1un1 \leq u \leq n.

You need to find the maximum value of len(u,v)gcd(u,v)len(u, v) \cdot gcd(u, v) over all pairs of vertices in the tree.

Each test consists of multiple test cases. The first line contains a single integer tt (1t1041 \leq t \leq 10^4) — the number of test cases. This is followed by their description.

The first line of each test case contains the number nn (2n1052 \leq n \leq 10^5) — the number of vertices in the tree.

The next n1n-1 lines specify the edges in the format uu, vv, ww (1u,vn1 \leq u, v \leq n, 1w10121 \leq w \leq 10^{12}).

It is guaranteed that the sum of nn over all test cases does not exceed 10510^5.

For each test case, output a single number equal to the maximum value of len(u,v)gcd(u,v)len(u, v) \cdot gcd(u, v) over all pairs of vertices in the tree.

Input

Each test consists of multiple test cases. The first line contains a single integer tt (1t1041 \leq t \leq 10^4) — the number of test cases. This is followed by their description.

The first line of each test case contains the number nn (2n1052 \leq n \leq 10^5) — the number of vertices in the tree.

The next n1n-1 lines specify the edges in the format uu, vv, ww (1u,vn1 \leq u, v \leq n, 1w10121 \leq w \leq 10^{12}).

It is guaranteed that the sum of nn over all test cases does not exceed 10510^5.

Output

For each test case, output a single number equal to the maximum value of len(u,v)gcd(u,v)len(u, v) \cdot gcd(u, v) over all pairs of vertices in the tree.

Sample Input 1

4
2
1 2 1000000000000
4
3 2 6
2 1 10
2 4 6
8
1 2 12
2 3 9
3 4 9
4 5 6
5 6 12
6 7 4
7 8 9
12
1 2 12
2 3 12
2 4 6
2 5 9
5 6 6
1 7 4
4 8 12
8 9 4
8 10 12
2 11 9
7 12 9

Sample Output 1

1000000000000
12
18
24