This page is still under construction.

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

Increasing Shortest Path

Time limit15sMemory limit256 MB

Summary
Find the cheapest A to B path using at most C edges whose weights strictly increase.
Level

Hard8 of 10

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

Problem

"Life is too short to make a story", said Ahmed Aly, so this problem gets straight to the point.

You are given a weighted directed graph with NN nodes numbered from 11 to NN. Every edge weight is a positive integer, and all edge weights are distinct.

A query is three integers AA, BB, CC. Consider the paths that start at node AA, end at node BB, use at most CC edges, and whose edge weights grow along the path: every edge must weigh more than the edge taken just before it. The first edge of a path carries no such restriction.

For each query, find the smallest possible sum of edge weights over those paths.

Input

The first line contains one integer TT, the number of test cases (1≤T≤1001 \le T \le 100).

Each test case begins with a line holding three integers separated by a single space, NN, MM and QQ (2≤N≤1502 \le N \le 150, 0≤M≤30000 \le M \le 3000, 1≤Q≤10001 \le Q \le 1000): the number of nodes, edges and queries.

The next MM lines each contain three integers separated by a single space, XX, YY and ZZ (1≤X,Y≤N1 \le X, Y \le N, 1≤Z≤30001 \le Z \le 3000, X≠YX \ne Y), an edge going from node XX to node YY with weight ZZ. There might be multiple edges between the same pair of nodes.

The next QQ lines each contain three integers separated by a single space, AA, BB and CC (1≤A,B≤N1 \le A, B \le N, 0≤C≤M0 \le C \le M, A≠BA \ne B), one query as described above.

Output

For each query, print one line with the minimum sum of edge weights of a path that satisfies the constraints, or −1-1 if no such path exists. Do not print blank lines between test cases.

Examples2

  1. Example 1

    Input
    1
    8 9 3
    1 2 1
    2 3 2
    3 4 3
    4 5 12
    5 8 7
    1 6 8
    6 4 9
    1 7 5
    7 4 4
    1 4 2
    1 4 3
    1 4 1
    
    Expected output
    17
    6
    -1
    
  2. Example 2

    Input
    2
    3 3 3
    1 2 5
    2 3 7
    1 3 20
    1 3 2
    1 3 1
    3 1 3
    2 2 2
    1 2 4
    2 1 9
    1 2 1
    2 1 1
    
    Expected output
    12
    20
    -1
    4
    9