Hide

Problem E
Origami

/problems/origami/file/statement/en/img-0001.jpg

Origami is the old Japanese art of folding single sheets of paper into the forms of animals, flowers and other figures. Recently a programmable machine was constructed to make some of the simpler kinds of origami. This machine has a large board with coordinate system drawn on it. First, a square sheet of paper is placed on the board so that the lower left corner has coordinates $(0, 0)$ and the upper right corner has coordinates $(1000, 1000)$. Then the machine follows the program. The program consists of several steps. Each step is given as a line in the coordinate system at which the origami is to be folded. The line is represented by two points: the paper is folded from right to left when one is facing the second point from the first. The paper on the left side of the line remains in place and the paper on the right side is folded flat on top of it. After the execution of the last step one gets a resulting origami. It is necessary to pierce the origami in order make a hole for hanging. It is important to choose the place for making a hole carefully, because if the origami is too thick in this place it is difficult to make a hole, and if it is too thin it is not strong enough for hanging. Your task is to write a program that computes the number of layers of a paper in the given point.

\includegraphics[width=0.33\textwidth ]{sample1}
Figure 1: Illustration of the origami in Sample Input 1

Input

The first part of the input file contains a program for the origami machine. The first line contains the number of steps $0 \le n \le 8$ of the program. One line follows for each step of the program. Each line contains fold-line coordinates $x_1, y_1, x_2, y_2$ separated by spaces.

The second part of the input file contains several potential points for making a hole. The first line contains the number $1 \le m \le 100$ of such places and each of the next lines contains coordinates $x, y$ of the point.

All coordinates in the input are integers with absolute value at most $2000$.

Output

For each of the potential points write a line containing the number of layers pierced through in this point.

Note: Assume that the paper has zero thickness and therefore folds ideally. The pierced hole is a single point. The layers pierced exactly at the border of the paper or at the folded edge (within a distance of $0.000001$) do not count as pierced layers.

Sample Input 1 Sample Output 1
2
-5 -5 10 10
10 750 0 750
6
100 600
800 600
300 400
100 100
500 500
200 500
4
2
2
0
0
2

Please log in to submit a solution to this problem

Log in