The Avengers love trees so Thanos decides to give them the Infinity Gauntlet if they solve this problem :
Given a tree with N nodes where each edge has a cost for traversal in a given direction such that if there is an edge between u and v then there is cost corresponding to traversal from u to v and v to u. The root of the tree is the vertex x. Now some of the directed edges are marked that means you have to traverse each of those edges at least once. Output the minimum possible value of cost of the path starting from root and ending somewhere in the tree and satisfying the conditions for traversal.
Input format :
First line contains an integer t , the number of test cases.
First line of each test case contains two integers N and x .
Next N-1 lines contain 5 integers i, j, w1, w2 and p ; where i and j are nodes, w1 is the cost of edge from i to j, w2 is the cost of edge from j to i and p tells which of these two edges is marked or not.
p=0 means no edge is marked. p=1 means only edge i to j is marked. p=2 means only edge j to i is marked. p=3 means both the edges are marked.
Output format :
Output one line for each test case containing the minimum cost for the traversal.
1 <= t <= 5
1 <= N <= 1e5
1 <= w1,w2 <= 1e9
0 <= p <= 3
1 <= i,j <= N
i is not equal to j.
Sum of N over all test cases <= 1e5