This page is still under construction.

Parts of this page are still being built. What you see may change.

Walking Plan

Time limit2sMemory limit512 MB

Summary
For each query, find the minimum total length of a walk from s to t that uses at least k edges in a directed weighted graph.
Level

Hard8 of 10

Topics
Graph, Shortest path, Dynamic programming, Matrix
Solved
No attempts yet

Problem

There are nn intersections in Bytetown, connected by mm one-way streets. The intersections are labeled 1,2,…,n1,2,\dots,n. Little Q likes sport walking very much, and he plans to walk for qq days. On the ii-th day, Little Q plans to start walking at the sis_i-th intersection, move along a street at least kik_i times, and finally arrive at the tit_i-th intersection. Here kik_i is the required number of moves, not streets: the same street may be used more than once.

Little Q's smartphone records his walking route. Little Q cares more about statistics than about staying healthy. So he wants to minimize the total walking length on each day. Write a program to help him find the best route.

Input

The first line contains a single integer TT (1≤T≤101 \leq T \leq 10), the number of test cases. For each test case:

The first line contains two integers nn and mm (2≤n≤502 \leq n \leq 50, 1≤m≤10 0001 \leq m \leq 10\,000), the number of intersections and one-way streets.

Each of the next mm lines contains three integers uiu_i, viv_i, wiw_i (1≤ui,vi≤n1 \leq u_i, v_i \leq n, ui≠viu_i \neq v_i, 1≤wi≤10 0001 \leq w_i \leq 10\,000), denoting a one-way street from intersection uiu_i to intersection viv_i with length wiw_i.

The next line contains an integer qq (1≤q≤100 0001 \leq q \leq 100\,000), the number of days.

Each of the next qq lines contains three integers sis_i, tit_i, kik_i (1≤si,ti≤n1 \leq s_i, t_i \leq n, 1≤ki≤10 0001 \leq k_i \leq 10\,000), describing the walking plan.

Output

For each walking plan, print one line containing a single integer: the minimum total walking length. If there is no solution, print "-1".

Examples1

  1. Example 1

    Input
    2
    3 3
    1 2 1
    2 3 10
    3 1 100
    3
    1 1 1
    1 2 1
    1 3 1
    2 1
    1 2 1
    1
    2 1 1
    
    Expected output
    111
    1
    11
    -1