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 n islands numbered 1,2,\dots,n and the keel depth limit for a path between islands i and j is a_{i,j}. As Uolevi gets constantly peppered with questions like "can we get from island a to island b with our ship?", he decides to write a program that calculates the maximum keel depth for q of the most popular routes numbered 1,2,\dots,q. The i-th route goes from island x_i to island y_i.

Input

The first line contains two integers n and q. The i-th of next n lines contains n integers a_{i,1},\,a_{i,2},\dots,\,a_{i, n}. The i-th of next q lines contains two integers x_i and y_i.

Output

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

Constraints

  • 2 \leq n \leq 500
  • 1 \leq q \leq 10^5
  • 1 \leq a_{i, j} \leq 10^9 if i \neq j
  • a_{i, j} = a_{j, i}
  • a_{i, i} = 0
  • x_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