Japanese version is here

Master Grus is a famous *origami* (paper folding) artist, who is
enthusiastic about exploring the possibility of origami art.
For future creation, he is now planning fundamental
experiments to establish the general theory of origami.

One rectangular piece of paper is used in each of his experiments. He folds it horizontally and/or vertically several times and then punches through holes in the folded paper.

The following figure illustrates the folding process of a simple experiment, which corresponds to the third dataset of the Sample Input below. Folding the 10 × 8 rectangular piece of paper shown at top left three times results in the 6 × 6 square shape shown at bottom left. In the figure, dashed lines indicate the locations to fold the paper and round arrows indicate the directions of folding. Grid lines are shown as solid lines to tell the sizes of rectangular shapes and the exact locations of folding. Color densities represent the numbers of overlapping layers. Punching through holes at A and B in the folded paper makes nine holes in the paper, eight at A and another at B.

Your mission in this problem is to write a computer program to count the number of holes in the paper, given the information on a rectangular piece of paper and folding and punching instructions.

The input consists of at most 1000 datasets, each in the following format.

nmtp

d_{1}c_{1}

...

d_{t}c_{t}

x_{1}y_{1}

...

x_{p}y_{p}

*n* and *m* are the width and the height, respectively, of
a rectangular piece of paper.
They are positive integers at most 32.
*t* and *p* are the numbers of folding and punching
instructions, respectively.
They are positive integers at most 20.
The pair of *d _{i}* and

*d*is either 1 or 2._{i}*c*is a positive integer._{i}- If
*d*is 1, the left-hand side of the vertical folding line that passes at_{i}*c*right to the left boundary is folded onto the right-hand side._{i} - If
*d*is 2, the lower side of the horizontal folding line that passes at_{i}*c*above the lower boundary is folded onto the upper side._{i}

The end of the input is indicated by a line containing four zeros.

For each dataset, output *p* lines, the *i*-th of which contains
the number of holes punched in the paper by the *i*-th punching instruction.

2 1 1 1 1 1 0 0 1 3 2 1 2 1 2 1 0 0 10 8 3 2 2 2 1 3 1 1 0 1 3 4 3 3 3 2 1 2 2 1 1 1 0 1 0 0 0 0 0 0

2 3 8 1 3 6

Source: ACM International Collegiate Programming Contest
, Japan Domestic Contest, Yokohama, Japan, 2018-07-6

https://icpc.iisf.or.jp/2018-yokohama/

https://icpc.iisf.or.jp/2018-yokohama/