Under construction

Still being built. Things may change

Island Trip

Interview

Time limit1sMemory limit256 MB

Summary
Given an undirected graph with node heights, for each query (A, K) find the minimum height reachable from A in exactly K edge steps, or -1 if impossible.
Level

Medium6 of 10

Topics
Graph, Dynamic programming, BFS, Matrix
Solved
No attempts yet

Problem

Jeongwoo travels for a living and for fun, and this time he plans to tour a group of islands. He picked NN islands, and MM bridges connect them, each bridge passable in both directions. Crossing one bridge counts as one move.

While reading the island heights printed on his map, Jeongwoo started wondering:

"Starting from island AA and crossing exactly KK bridges, what is the lowest height among the islands I can end up on?"

The same bridge may be crossed several times, and an island already visited may be visited again. Answer each of Jeongwoo's queries.

Input

The first line contains the number of islands NN and the number of bridges MM, separated by a space. (2≤N≤1002 \le N \le 100, 1≤M≤100001 \le M \le 10000)

The second line contains the heights HH of islands 1 through NN, separated by spaces. (0≤H≤100000 \le H \le 10000)

Each of the next MM lines contains two integers XX and YY describing one bridge, separated by a space. (1≤X,Y≤N1 \le X, Y \le N) It means island XX and island YY are joined by a bridge passable in both directions. XX and YY may be equal, and the same pair of islands may appear on several lines.

The next line contains the number of queries TT. (1≤T≤100001 \le T \le 10000)

Each of the following TT lines contains the starting island AA and the number of bridges to cross KK, separated by a space. (1≤A≤N1 \le A \le N, 1≤K≤5001 \le K \le 500)

Output

For each query, print on its own line the lowest height among the islands reachable from island AA by crossing exactly KK bridges.

If no island is reachable with exactly KK crossings, print −1-1.

Examples1

  1. Example 1

    Input
    3 2
    1 2 3
    1 2
    2 3
    2
    1 1
    2 3
    
    Expected output
    2
    1