#P3960. The Shifting Matrix

    ID: 17208 Type: Default 1000ms 256MiB

The Shifting Matrix

The Shifting Matrix

Sylvia is a dedicated student who recently took part in her school's military training. In her unit, the students are arranged in a rectangular formation of size \(n \times m\), with \(n\) rows and \(m\) columns. Initially, the students are numbered in row-major order: the student in the \(i\)th row and \(j\)th column is assigned the number \((i-1)\times m+j\).

During training, there occur \(q\) events wherein a student leaves the formation. Each event is specified by a pair of integers \((x, y)\) \( (1 \le x \le n,\ 1 \le y \le m)\), indicating that the student at row \(x\) and column \(y\) leaves.

After a student leaves, the instructor issues two sequential commands:

  1. Align Left: All students in the same row, from column \(y+1\) to \(m\), move one position to the left. As a result, the empty spot shifts to position \((x, m)\).
  2. Align Forward: Next, all students in the last column, from row \(x+1\) to \(n\), move one position upward, causing the empty spot to finally be at position \((n, m)\).

Immediately afterward, the student who left returns and occupies the empty position \((n, m)\). Note that even though the arrangement of the students may change, each student's assigned number remains constant.

Your task is to determine, for each leave event, the number of the student who left.

inputFormat

The first line contains three integers \(n\), \(m\) and \(q\), representing the number of rows, columns, and the number of leave events respectively.

Each of the following \(q\) lines contains two integers \(x\) and \(y\), indicating that the student at row \(x\) and column \(y\) leaves.

outputFormat

For each leave event, output the number of the student who left, each on a separate line.

sample

3 3 1
2 2
5

</p>