Under construction

Still being built. Things may change

Jogging in the Park

Time limit2sMemory limit512 MB

Summary
Extend k walks that start at glade 1 so each ends at glade n with the same total length, or report -1 when that is impossible.
Level

Hard8 of 10

Topics
Graph, Shortest path, Number theory, Math
Solved
No attempts yet

Problem

The Outer Park is very well suited for jogging. Inside the park, there are nn glades, numbered from 1 to nn. The glades are connected by mm trails. The ii-th trail connects glades aia_i and bib_i and has length cic_i meters. All trails can be walked in both directions. The main entrance is at glade 1.

Your friends each prepared a route that starts at the entrance and moves from glade to glade along trails. At the end of the run, all friends want to reach glade nn at the same time, where a cafe is. Some routes do not end at glade nn, and the routes may have different lengths. All friends run at the same speed.

Extend each route so that it starts with the same glades as the original, ends at glade nn, and has exactly the same length as every other extended route. An original or extended route may use the same trail more than once.

Input

The first line contains two integers nn and mm (2≤n≤502 \le n \le 50; 1≤m≤n(n−1)21 \le m \le \frac{n(n-1)}{2}), the number of glades and trails. Each of the next mm lines contains three integers aia_i, bib_i, and cic_i (1≤ai,bi≤n1 \le a_i, b_i \le n; ai≠bia_i \ne b_i; 1≤ci≤1061 \le c_i \le 10^6), the glades connected by the ii-th trail and its length.

The next line contains a single integer kk (2≤k≤502 \le k \le 50), the number of friends. Each of the next kk lines contains lil_i (2≤li≤502 \le l_i \le 50), the number of glades in the route of the ii-th friend, followed by lil_i integers gi,1,…,gi,lig_{i,1}, \ldots, g_{i,l_i} (1≤gi,j≤n1 \le g_{i,j} \le n), the glades of that route.

All unordered pairs (ai,bi)(a_i, b_i) are distinct. For every ii, gi,1=1g_{i,1} = 1. For every jj between 1 and li−1l_i - 1, there is a trail between gi,jg_{i,j} and gi,j+1g_{i,j+1}.

Output

If it is impossible to extend all the routes, print a single integer -1. Otherwise, print kk lines. The ii-th line contains pip_i (pi≥lip_i \ge l_i) followed by pip_i integers hi,1,…,hi,pih_{i,1}, \ldots, h_{i,p_i} (1≤hi,j≤n1 \le h_{i,j} \le n), the glades of the ii-th extended route.

For every ii, hi,pi=nh_{i,p_i} = n. For every jj between 1 and pi−1p_i - 1, there is a trail between hi,jh_{i,j} and hi,j+1h_{i,j+1}. For every jj between 1 and lil_i, hi,j=gi,jh_{i,j} = g_{i,j}. The total length of all extended routes must be the same.

The sum of all pip_i must not exceed 2⋅1062 \cdot 10^6. If a valid extension exists, one exists with the sum of all pip_i at most 2⋅1062 \cdot 10^6.

Examples1

  1. Example 1

    Input
    4 4
    1 2 10
    2 3 30
    2 4 30
    1 4 100
    3
    2 1 2
    3 1 2 3
    2 1 4
    
    Expected output
    5 1 2 4 2 4
    5 1 2 3 2 4
    2 1 4