SPACEBRG - Space Bridges

no tags 

Space Bridges

A long time ago, in a galaxy far, far away…. war is going on between the Galactic Empire and the Rebel Alliance. During wars like these, space bridges are really resourceful as they can transfer almost anything including Storm Trooper armies from one space region to another in just mere seconds. Each space region consists of many sectors. The sectors are connected with each other via space roads in such a way that there is exactly one path from one sector to another within a space region. Note that space roads and space bridges are not the same.

The sectors of two space regions of the empire have already been arranged. To find if the arrangement is strong enough, the Supreme Commander of the Imperial Forces has ordered you, their top programmer to calculate the total strength of the two regions.

graph

Two regions can be connected using exactly one Space Bridge from one sector of a region to one sector of the other region. The value of a space bridge connection is the maximum path length you need to travel from any sector of one region to any sector of the other region using any space road or space bridge at most once. The total strength of two regions is the summation of the power of all possible space bridge connections. 

Given the sector arrangements of both space regions, your task is to find the total strength of the two regions. Unfortunately, if the strength isn’t as expected, the Supreme Commander will use his “Force Choke” on you, so good luck!

Input

Input starts with an integer T (≤ 20), denoting the number of test cases.

Each case starts with a line containing two integers n and m (1 ≤ n, m ≤ 105) denoting the number of sectors in the two space regions correspondingly. The next n-1 lines denotes the sector arrangement of the first region. Each line will contain u and v (1 <= u,v <= n, u != v) denoting that there is a space road connection between the sectors u and v of the first region. It is guaranteed that the arrangement is such that there will be exactly one path from any sector to another and no same space road will occur twice in the given input. The next m-1 lines denotes the sector arrangements of the second region containing p and q (1 <=p, q <= m, p!=q) which is analogous to the arrangement of the first region.

Output

For each case, in a single line print the case number and the strength of the combined region the space regions.

Sample Input

Output for Sample Input

2

2 4

2 1

2 3

3 1

3 4

2 1

2 1

Case 1: 30

Case 2: 4


Note: In the second sample, there are 2 possible space bridge connections. The first connection is by connecting the first sector of the first region to the first sector of the second region and the second connection is by connecting the second sector of the first region to the first sector of the second region. The values of both the space bridge connections is 2, as you need no more than 2 space roads and space bridges to travel from any sector of the first region to the only sector of the second region. So the total strength is 2+2=4. Look at the figures for better understanding.

N.B. Dataset is huge, use faster IO

Problem Setter: Aninda Majumder

Special Thanks: Momontho Mashak Monmoy, Nafis Sadique, Ahmad Faiyaz


hide comments
[Rampage] Blue.Mary: 2016-02-03 08:49:14

The value of a space bridge connection is the maximum path length you need to travel from any sector of one region to any sector of the **other** region using any space road or space bridge at most once.

If this changes to "from any sector to any other sector"(not need to be in two different regions), this problem will become more interesting.

Recycle Bin: 2014-08-30 11:39:50

Wow.. it's too easy, ac in one go.. please move to the tutorial.. Thanks


Added by:Faiyaz
Date:2013-12-27
Time limit:2s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64