Do you know Just Odd Ink Way? It is a national road of length 10100 in Republic of EGOI from the east end to the west end. It is famous because there are several painting on the road painted by “Just Odd Ink.” In the following, we abbreviate it, and call it JOI Way.
There are several painting of various sizes on JOI Way. Characters are written on some of them.
Rie is a tour guide working on JOI Way. She plans to guide the participants of JOIG Spring Training Camp. In order to cheer the participants, she plans to choose the paintings on which ‘J’, ‘O’, ‘I’, ‘G’ are written, and visit them in this order. There are N candidates of paintings. The i-th painting (1≤i≤N) is located at the place on JOI Way at a distance of A_i from the west end. In this painting, the character C_i is written.
Rie has Q plans. In the j-th plan (1≤j≤Q), she will travel as follows.
J’ is written, and moves to its location.O’ is written, and moves to its location.I’ is written, and moves to its location.G’ is written, and moves to its location.During the tour, it is not allowed to go outside JOI Way.
Under the above conditions, Rie wants to minimize the total travel distance for each plan.
Write a program which, given information on the paintings on JOI Way and Rie’s plans, calculates the minimum possible value of the total travel distance for each plan.
Read the following data from the standard input.
N
A_1 C_1
A_2 C_2
⋮
A_N C_N
Q
S_1 T_1
S_2 T_2
⋮
S_Q T_Q
Write Q lines to the standard output. The j-th line (1≤j≤Q) of the output should contain the minimum possible value of the total travel distance for the j-th plan.
J’,‘O’,‘I’,or ‘G’.J’ for at least one i (1≤i≤N).O’ for at least one i (1≤i≤N).I’ for at least one i (1≤i≤N).G’ for at least one i (1≤i≤N).