PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FW0000093. Chuyến tham quan các tuyến đường (Sightseeing Tour)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Chuyến tham quan các tuyến đường (Sightseeing Tour)

Sightseeing Tour

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

Given an undirected weighted graph and multiple queries, each query specifies kk required edge IDs. Starting at vertex 11 and ending at vertex nn, traverse each required edge at least once, in either direction. Any roads may be used additionally. Find the minimum length for each query.

Input

First n,mn,m and mm indexed edges u,v,wu,v,w. Next integer qq; each query contains kk followed by kk distinct required edge IDs.

Output

Print the minimum total distance per query, or -1 if impossible.

Subtasks

  • Subtask 1 (40 points): 3≤n≤6,  1≤q≤3,  1≤k≤33\le n\le 6,\;1\le q\le 3,\;1\le k\le 3. All input-format and graph conditions above apply.

  • Subtask 2 (60 points): 3≤n≤16,  1≤q≤9,  1≤k≤63\le n\le 16,\;1\le q\le 9,\;1\le k\le 6. All input-format and graph conditions above apply.

Examples

Input

3 3
1 2 2
2 3 3
1 3 12
1
1 2

Output

5

Explanation

Required edge 2 joins vertices 2 and 3 with length 3. Go from 1 to 2 along edge 1 of length 2, then cross the required edge to vertex 3. Total distance 2+3=52+3=5.