NeoMole Synthesis
题号:NC210241
时间限制:C/C++/Rust/Pascal 1秒,其他语言2秒
空间限制:C/C++/Rust/Pascal 256 M,其他语言512 M
64bit IO Format: %lld

题目描述

The chemists have just conceived a large category of molecules, named NeoMole. The exceptionally outstanding properties of these molecules make them potential medicine for the current influenzal pneumonia.

Theoretically speaking, a NeoMole molecule consists of only one chemical element, neonium. The neonium atoms are connected by regular chemical bonds in a NeoMole molecule. Unlike carbon, the number of atoms connected to a neonium atom by chemical bonds is, surprisingly, unlimited. Also, the molecule structure is acyclic.

The scientists want to synthesize some NeoMoles, using small NeoMole molecules as substrates (i.e., raw materials). To synthesize big molecules, one can choose two molecules, which can be either substrates or previously synthesized molecules, and specify an atom for each molecule. After some mysterious chemical reactions, a new chemical bond will form between the specified atoms.



Because the NeoMoles are really precious, scientists want to minimize their total cost on substrates. Can you write a program to help them find the optimal synthesis plan? Do not miss the opportunity to earn big money!

输入描述:

The first line contains a integer n , denoting the number of atoms in the molecule to synthesize. The atoms are numbered 1 through n. This is followed by n-1 lines, each containing two integer u, v , denoting a chemical bond between the u-th and the v-th atoms.

Then follows a line containing a single integer m , denoting the number of substrates. The remaining part of the input specifies these substrate molecules. The specification of each substrate molecule begins with a line containing two integers n, c , denoting the number of atoms in the molecule and the cost of the molecule per copy. It is then followed by n-1 lines, specifying the bonds between molecules, in the same way as above. It is guaranteed that the sum of the numbers of atoms in all substrate molecules does not exceed 500. You may use arbitrarily many copies of any substrate molecule in your synthesis.

It is guaranteed that, in all molecules, the atoms are connected acyclicly.

输出描述:

Output a single line of an integer, denoting the minimum total cost on raw materials. If it is impossible to synthesize the target molecule, output ‘impossible’ instead.
示例1

输入

复制
8
1 2
2 3
2 4
4 5
4 6
6 7
6 8
3
3 2
1 2
2 3
2 3
1 2
1 5

输出

复制
7
示例2

输入

复制
5
1 2
2 3
3 4
4 5
1
3 1
1 2
2 3

输出

复制
impossible