Under construction

Still being built. Things may change

Grid Game

Interview

Time limit1sMemory limit1024 MB

Summary
For each start cell on a grid with blocked cells, decide the winner of a game that moves a piece to the bottom-right corner with down, right, or diagonal moves of up to K cells.
Level

Medium4 of 10

Topics
Dynamic programming, Game theory
Solved
No attempts yet

Problem

Hanguk and Jeongol play a game on a grid board. Hanguk moves first, and the two players take turns moving a piece. Neither player may skip a turn.

The board has NN rows and MM columns, and some cells are blocked. The piece cannot move into a blocked cell. For convenience, the cell where the ii-th row (counting from the top) meets the jj-th column (counting from the left) is written as (i,j)(i, j).

In one move, the piece moves down by one cell, right by one cell, or diagonally down and right by 11, 22, ⋯\cdots, KK cells. (The piece cannot move outside the board or into a blocked cell. If K=0K = 0, the piece cannot move diagonally.)

Here are a few examples of the movement rules.

For example, take N=6N = 6, M=8M = 8, K=3K = 3, and a board with no blocked cells. A piece at (2,3)(2, 3) can move to 5 cells, marked with O in the figure below.

Now suppose (2,4)(2, 4) and (4,5)(4, 5) are blocked. Then the piece at (2,3)(2, 3) can move to 3 cells, marked with O in the figure below.

In the next figure, N=6N = 6, M=8M = 8, K=3K = 3, and no cells are blocked. A piece at (5,7)(5, 7) can move to 3 cells, marked with O.

Finally, take N=6N = 6, M=8M = 8, K=0K = 0, and no blocked cells. A piece at (1,1)(1, 1) can move to 2 cells, marked with O.

The goal is to move the piece to the bottom-right cell (N,M)(N, M). The player who makes the last move wins. Both players play optimally.

The winner can depend on the starting position. You are given QQ positions (x1,y1),(x2,y2),⋯ ,(xQ,yQ)(x_1, y_1), (x_2, y_2), \cdots, (x_Q, y_Q) on the board. For each position, find the winner when the game starts there.

Input

The first line contains three integers NN, MM, and KK, separated by spaces.

The next NN lines each contain a string of length MM made only of # and .. If the jj-th character of the ii-th line is #, cell (i,j)(i, j) is blocked. If it is ., the cell is not blocked.

The next line contains an integer QQ.

Each of the next QQ lines contains two integers xix_i and yiy_i, separated by a space.

Output

For each of the QQ positions, print First if Hanguk wins, or Second if Jeongol wins. Print one result per line, in order.

Constraints

  • 2≤N≤3002 \le N \le 300
  • 2≤M≤3002 \le M \le 300
  • K≥0K \ge 0
  • K≤N−1K \le N - 1
  • K≤M−1K \le M - 1
  • Cell (N,M)(N, M) is not blocked.
  • From any unblocked cell, the piece can reach (N,M)(N, M) by following the movement rules.
  • 1≤Q≤3001 \le Q \le 300
  • For every ii (1≤i≤Q1 \le i \le Q):
    • 1≤xi≤N1 \le x_i \le N, 1≤yi≤M1 \le y_i \le M
    • Cell (xi,yi)(x_i, y_i) is not blocked.
    • (xi,yi)(x_i, y_i) is not (N,M)(N, M).

Examples3

  1. Example 1

    Input
    2 2 0
    .#
    ..
    2
    1 1
    2 1
    
    Expected output
    Second
    First
    
  2. Example 2

    Input
    2 2 1
    ..
    ..
    1
    1 1
    
    Expected output
    First
    
  3. Example 3

    Input
    3 4 0
    ....
    .#..
    ....
    1
    3 2
    
    Expected output
    Second