JOIG Tour

아직 제출이 없습니다시간 제한1초메모리 제한1024 MB

문제

Do you know Just Odd Ink Way? It is a national road of length 1010010^{100} 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 NN candidates of paintings. The ii-th painting (1iN1 ≤ i ≤ N) is located at the place on JOI Way at a distance of A_iA\_i from the west end. In this painting, the character C_iC\_i is written.

Rie has QQ plans. In the jj-th plan (1jQ1 ≤ j ≤ Q), she will travel as follows.

  1. Rie starts a tour from the place on JOI Way at a distance of S_jS\_j from the west end.
  2. She chooses a painting on which ‘J’ is written, and moves to its location.
  3. She chooses a painting on which ‘O’ is written, and moves to its location.
  4. She chooses a painting on which ‘I’ is written, and moves to its location.
  5. She chooses a painting on which ‘G’ is written, and moves to its location.
  6. She moves to the place on JOI Way at a distance of T_jT\_j from the west end, and finishes the tour.

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.

NN

A_1A\_1 C_1C\_1

A_2A\_2 C_2C\_2

\vdots

A_NA\_N C_NC\_N

QQ

S_1S\_1 T_1T\_1

S_2S\_2 T_2T\_2

\vdots

S_QS\_Q T_QT\_Q

출력

Write QQ lines to the standard output. The jj-th line (1jQ1 ≤ j ≤ Q) of the output should contain the minimum possible value of the total travel distance for the jj-th plan.

제한

  • 4N100,0004 ≤ N ≤ 100\\,000.
  • 1A_i1,000,000,000,000,000(=1015)1 ≤ A\_i ≤ 1\\,000\\,000\\,000\\,000\\,000 (= 10^{15}) (1iN1 ≤ i ≤ N).
  • A_i<A_i+1A\_i < A\_{i+1} (1iN11 ≤ i ≤ N - 1).
  • C_iC\_i (1iN1 ≤ i ≤ N) is either ‘J’,‘O’,‘I’,or ‘G’.
  • C_iC\_i is equal to ‘J’ for at least one ii (1iN1 ≤ i ≤ N).
  • C_iC\_i is equal to ‘O’ for at least one ii (1iN1 ≤ i ≤ N).
  • C_iC\_i is equal to ‘I’ for at least one ii (1iN1 ≤ i ≤ N).
  • C_iC\_i is equal to ‘G’ for at least one ii (1iN1 ≤ i ≤ N).
  • 1Q100,0001 ≤ Q ≤ 100\\,000.
  • 1S_j1,000,000,000,000,000(=1015)1 ≤ S\_j ≤ 1\\,000\\,000\\,000\\,000\\,000 (= 10^{15}) (1jQ1 ≤ j ≤ Q).
  • 1T_j1,000,000,000,000,000(=1015)1 ≤ T\_j ≤ 1\\,000\\,000\\,000\\,000\\,000 (= 10^{15}) (1jQ1 ≤ j ≤ Q).
  • (S_j,T_j)(S_k,T_k)(S\_j , T\_j) \ne (S\_k, T\_k) (1j<kQ1 ≤ j < k ≤ Q).
  • NN, QQ are integers.
  • A_iA\_i is an integer (1iN1 ≤ i ≤ N).
  • S_jS\_j, T_jT\_j are integers (1jQ1 ≤ j ≤ Q).