Under construction

Still being built. Things may change

Time is Money

Time limit2sMemory limit1024 MB

Summary
Find the shortest time from cabstand 1 to cabstand n when the k-th taxi pickup costs 2^(k-1) minutes of waiting, modulo 1e9+7.
Level

Hard8 of 10

Topics
Shortest path, Graph, Math
Solved
No attempts yet

Problem

The city of ICPC has excessive pedestrian separation, which makes transportation in the city quite complicated. Citizens want to save time, so they try to reach their destinations as fast as possible by alternating between taxi rides on driveways and walks on footpaths.

Many cabstands are in the city. Footpaths and driveways connect some pairs of cabstands directly, and both allow traffic in both directions. Some pairs of cabstands may have no direct or indirect path between them.

When you are on foot at a cabstand, you can either:

  • walk a footpath, if one exists, to another cabstand, or
  • pick up a taxi, if a driveway connects to the cabstand.

When you are in a taxi, you can either:

  • keep riding to another cabstand via a driveway, or
  • get out of the taxi.

You can get out of a taxi only at cabstands.

Traveling a footpath or a driveway takes the time given for that road. In addition, you wait one minute the first time you pick up a taxi. The wait doubles with each later pickup: two minutes the second time, four minutes the third time, and 2992^{99} minutes the 100th time.

You start on foot at cabstand 1. Find the shortest possible time to reach cabstand nn. Output the answer modulo 109+710^9+7.

Input

The input consists of multiple datasets, each in the following format.

n p q
a1 b1 c1
…
ap bp cp
d1 e1 f1
…
dq eq fq

Here, nn, pp, and qq are the numbers of cabstands, footpaths, and driveways. nn is an integer from 2 to 20000, inclusive. Cabstands are numbered 1 through nn. pp and qq are positive integers not exceeding 20000. Footpath ii connects cabstands aia_i and bib_i and takes cic_i minutes to walk. Driveway jj connects cabstands djd_j and eje_j and takes fjf_j minutes to ride. All aia_i, bib_i, djd_j, and eje_j are positive integers not exceeding nn. cic_i and fjf_j are positive integers not exceeding 10610^6.

The input ends with a line containing three zeros. The totals of nn, of pp, and of qq over all datasets each do not exceed 200000.

Output

For each dataset, output one line with the shortest time in minutes to travel from cabstand 1 to cabstand nn, modulo 109+710^9+7. If the trip is impossible, output -1.

Examples1

  1. Example 1

    Input
    5 4 2
    1 2 1
    2 3 100
    3 4 1
    4 5 100
    2 3 10
    4 5 10
    4 1 1
    1 2 100
    2 3 100
    4 3 3
    1 2 10
    2 3 1
    3 4 10
    2 1 2
    3 2 2
    4 3 2
    0 0 0
    
    Expected output
    25
    -1
    7