CSES - Aalto Competitive Programming 2024 - wk7 - Wed - Shallow waters
  • Time limit: 1.00 s
  • Memory limit: 512 MB

Uolevi is working at a holiday resort in a tropical archipelago. His job is to guide incoming cruise ships through the shallow waters. The archipelago consists of nn islands numbered 1,2,,n1,2,\dots,n and the keel depth limit for a path between islands ii and jj is ai,ja_{i,j}. As Uolevi gets constantly peppered with questions like "can we get from island aa to island bb with our ship?", he decides to write a program that calculates the maximum keel depth for qq of the most popular routes numbered 1,2,,q1,2,\dots,q. The ii-th route goes from island xix_i to island yiy_i.

Input

The first line contains two integers nn and qq. The ii-th of next nn lines contains nn integers ai,1,ai,2,,ai,na_{i,1},\,a_{i,2},\dots,\,a_{i, n}. The ii-th of next qq lines contains two integers xix_i and yiy_i.

Output

Print the maximum keel depth for the ii-th route on the ii-th line.

Constraints

  • 2n5002 \leq n \leq 500
  • 1q1051 \leq q \leq 10^5
  • 1ai,j1091 \leq a_{i, j} \leq 10^9 if iji \neq j
  • ai,j=aj,ia_{i, j} = a_{j, i}
  • ai,i=0a_{i, i} = 0
  • xiyix_i \neq y_i

Example

Input:

5 5
0 13 10 67 42 
13 0 40 40 32 
10 40 0 94 69 
67 40 94 0 53 
42 32 69 53 0 
1 3
1 4
1 5
4 5
5 2

Output:

67
67
67
69
40