Under construction

Still being built. Things may change

Evil Problemsetters

Time limit10sMemory limit1024 MB

Summary
Given a grid with up to 42 black holes, find the shortest path from the entrance to the exit for each query, or -1 if it is unreachable.
Level

Hard9 of 10

Topics
Shortest path, Graph, Implementation
Solved
No attempts yet

Problem

Some contestants decide to escape from this contest because of the challenging problems. To stop them, the EVIL problem setters built a labyrinth at the stadium's exit. The labyrinth is an n×mn \times m grid with an entrance, an exit, and kk black holes. A contestant who accidentally steps into a black hole falls into it and can never escape from the contest.

Worse, the problem setters may change the coordinates of the entrance and the exit. You are a poor contestant who starts at the entrance and wants to reach the exit without stepping into any black hole. Each step moves you to one of the four adjacent cells. After each time the problem setters change the coordinates of the entrance and the exit, find the minimum number of steps needed to get from the entrance to the exit.

Input

The first line contains four integers n,m,k,qn, m, k, q (1≤n,m≤200 000,nm≤200 000,0≤k≤42,1≤q≤100 000)(1 \leq n, m \leq 200\,000, nm \leq 200\,000, 0 \leq k \leq 42, 1 \leq q \leq 100\,000), the number of rows, the number of columns, the number of black holes, and the number of queries.

The next kk lines describe the black holes. Each line contains two integers x,yx, y (1≤x≤n,1≤y≤m)(1 \leq x \leq n, 1 \leq y \leq m), the coordinates of a black hole. No two black holes share a position.

The last qq lines describe the queries. Each line contains four integers xs,ys,xt,ytx_s, y_s, x_t, y_t (1≤xs,xt≤n,1≤ys,yt≤m)(1 \leq x_s, x_t \leq n, 1 \leq y_s, y_t \leq m), where (xs,ys)(x_s, y_s) is the entrance and (xt,yt)(x_t, y_t) is the exit.

Output

For each query, print the minimum number of steps needed to get from the entrance to the exit, one per line. If the exit cannot be reached, print −1-1. If the entrance or the exit is at the same position as a black hole, the exit is considered unreachable.

Hint

The layout of the labyrinth and one possible shortest path for the first query of the first sample are shown below.

Examples2

  1. Example 1

    Input
    5 5 4 7
    2 2
    2 3
    3 2
    3 3
    2 1 3 4
    1 1 1 1
    2 2 2 2
    1 1 1 5
    2 2 5 5
    2 1 2 4
    1 1 3 3
    
    Expected output
    6
    0
    -1
    4
    -1
    5
    -1
    
  2. Example 2

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