This page is still under construction.

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

Night Letter

Time limit1sMemory limit1024 MB

Summary
For each query (C, s, e), find the shortest path from s to e that avoids intermediate houses with index at least C.
Level

Hard8 of 10

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

Problem

In Seonrin Village, there is a tradition of sending fireflies to someone dear every night.

Seonrin Village consists of NN houses numbered 11 to NN and bidirectional roads connecting pairs of houses. A firefly may pass through other houses when there is no road directly connecting its starting house and destination, or when a more efficient route exists. House XX holds 2X2^{X} drops of dew, and a firefly must drink all the dew of every house it passes through during its journey, excluding the starting house and the destination. Unfortunately, each firefly has a constant CC, and if it drinks 2C2^{C} drops of dew or more, it stops flying and falls asleep.

Chanwoo, a resident of Seonrin Village, wondered QQ times about the minimum time for a firefly that cannot drink 2C2^{C} drops of dew or more to travel from house ss to house ee.

Write a program that answers Chanwoo's questions.

Input

The first line gives the number of houses NN and the number of questions QQ.

Starting from the second line, NN lines give the road information. Let Di,jD_{i,j} be the jj-th number on the ii-th line. If Di,jD_{i,j} is a positive integer, it is the time to pass through the road connecting house ii and house jj; if it is 00, there is no road connecting house ii and house jj.

Starting from the next line, QQ lines give integers CC, ss, ee separated by spaces.

This is a question asking for the minimum time for a firefly that cannot drink 2C2^{C} drops of dew or more to travel from house ss to house ee.

Output

Print the answers to the questions over QQ lines, one per line, in order. If reaching the destination is impossible, print −1-1.

Constraints

  • 2≤N≤3002 \leq N \leq 300
  • For all ii, jj with 1≤i,j≤N1 \leq i, j \leq N: 0≤Di,j≤170 3240 \leq D_{i,j} \leq 170\,324, Di,j=Dj,iD_{i,j} = D_{j, i}, Di,i=0D_{i,i} = 0
  • 1≤Q≤500 0001 \leq Q \leq 500\,000
  • 1≤s,e≤N1 \leq s, e \leq N
  • 1≤C≤N+11 \leq C \leq N + 1

Examples1

  1. Example 1

    Input
    4 2
    0 100 1 0
    100 0 0 100
    1 0 0 1
    0 100 1 0
    3 1 4
    2 4 1
    
    Expected output
    200
    -1