Under construction

Still being built. Things may change

Water Bottle

Time limit5sMemory limit512 MB

Summary
Given a grid of buildings, fields, and walls, find for each query the smallest water bottle that lets you walk between two given buildings.
Level

Medium7 of 10

Topics
Graph, BFS, Shortest path, Union-find
Solved
No attempts yet

Problem

JOI's city, IOI, is known to be very hot all year round.

IOI is a rectangle divided into HH rows and WW columns of cells. Each cell is a building, a field, or a wall. There are PP building cells, numbered from 1 to PP.

JOI can enter only building cells and field cells. From a cell, JOI can move directly only to an adjacent cell that shares a side, and JOI cannot leave the city during a move.

JOI often walks between buildings for various errands. Buildings are air-conditioned, but fields are hot and sunny. Each field cell passed costs 1 unit of water. Fields have no vending machines or water fountains, so people usually carry a water bottle. A bottle of size xx holds at most xx units of water. Building cells have taps, so a bottle can be refilled to full there.

Large bottles are hard to carry, so JOI wants to use the smallest bottle possible. Write a program that finds, for each query, the minimum bottle size needed to travel between two given buildings.

You are given the map of IOI and QQ queries. The ii-th query asks: what is the minimum bottle size needed to travel between buildings SiS_i and TiT_i?

Input

The first line contains four integers HH, WW, PP, and QQ separated by spaces. These mean that IOI has HH rows and WW columns, has PP building cells, and has QQ queries.

The next HH lines describe the map. The rr-th of these lines contains a string of WW characters, each either . or #. The character in column cc is . for a building or field and # for a wall.

The next PP lines give the building positions. The jj-th line contains two integers AjA_j and BjB_j. Building jj is in column AjA_j counting from the top, and row BjB_j counting from the left. The corresponding cell on the map is guaranteed to be ..

The next QQ lines each contain two integers SiS_i and TiT_i.

Output

Print QQ lines. On line ii, print the minimum bottle size needed to travel between buildings SiS_i and TiT_i. If the trip is impossible, print -1. If the trip can be made without passing any field cell, print 0.

Constraints

  • 1≤H≤20001 \le H \le 2000
  • 1≤W≤20001 \le W \le 2000
  • 2≤P≤2000002 \le P \le 200000
  • 1≤Q≤2000001 \le Q \le 200000
  • 1≤Aj≤H1 \le A_j \le H for all jj
  • 1≤Bj≤W1 \le B_j \le W for all jj
  • (Ai,Bi)≠(Aj,Bj)(A_i, B_i) \ne (A_j, B_j) for i<ji < j
  • 1≤Si<Ti≤P1 \le S_i < T_i \le P for all ii

Examples2

  1. Example 1

    Input
    5 5 4 4
    .....
    ..##.
    .#...
    ..#..
    .....
    1 1
    4 2
    3 3
    2 5
    1 2
    2 4
    1 3
    3 4
    
    Expected output
    3
    4
    4
    2
    
  2. Example 2

    Input
    5 5 3 2
    ...#.
    ..#..
    #....
    .##..
    ...#.
    1 3
    5 2
    1 5
    1 2
    1 3
    
    Expected output
    -1
    7