Under construction

Still being built. Things may change

JOIG Tour

Time limit1sMemory limit1024 MB

Summary
Given paintings labeled J, O, I, G on a line, find for each query the shortest walk from S that visits a J, an O, an I, and a G painting in that order, then ends at T.
Level

Hard8 of 10

Topics
Dynamic programming, Segment tree
Solved
No attempts yet

Problem

Do you know Just Odd Ink Way? It is a national road of length 1010010^{100} in Republic of EGOI, running from the east end to the west end. It is famous for several paintings made by "Just Odd Ink" along the road. Below, we call it JOI Way.

Several paintings of various sizes are on JOI Way. Some of them have characters written on them.

Rie is a tour guide on JOI Way. She plans to guide the participants of the JOIG Spring Training Camp. To cheer them up, she will pick paintings with 'J', 'O', 'I', and 'G' written on them, and visit them in this order. There are NN candidate paintings. The ii-th painting (1≤i≤N1 \le i \le N) is located at distance AiA_i from the west end, and the character CiC_i is written on it.

Rie has QQ plans. In the jj-th plan (1≤j≤Q1 \le j \le Q), she travels as follows.

  1. Rie starts the tour at the place at distance SjS_j from the west end.
  2. She picks a painting with 'J' written on it and moves to its location.
  3. She picks a painting with 'O' written on it and moves to its location.
  4. She picks a painting with 'I' written on it and moves to its location.
  5. She picks a painting with 'G' written on it and moves to its location.
  6. She moves to the place at distance TjT_j from the west end and ends the tour.

Rie may not go outside JOI Way.

For each plan, find the minimum possible total travel distance.

Input

The input is given in the following format.

NN

A1A_1 C1C_1

A2A_2 C2C_2

⋮\vdots

ANA_N CNC_N

QQ

S1S_1 T1T_1

S2S_2 T2T_2

⋮\vdots

SQS_Q TQT_Q

Output

Print QQ lines. The jj-th line contains the minimum possible total travel distance for the jj-th plan.

Constraints

  • 4≤N≤100 0004 \le N \le 100\,000.
  • 1≤Ai≤10151 \le A_i \le 10^{15} (1≤i≤N1 \le i \le N).
  • Ai<Ai+1A_i < A_{i+1} (1≤i≤N−11 \le i \le N - 1).
  • CiC_i (1≤i≤N1 \le i \le N) is one of 'J', 'O', 'I', or 'G'.
  • CiC_i is 'J' for at least one ii (1≤i≤N1 \le i \le N).
  • CiC_i is 'O' for at least one ii (1≤i≤N1 \le i \le N).
  • CiC_i is 'I' for at least one ii (1≤i≤N1 \le i \le N).
  • CiC_i is 'G' for at least one ii (1≤i≤N1 \le i \le N).
  • 1≤Q≤100 0001 \le Q \le 100\,000.
  • 1≤Sj≤10151 \le S_j \le 10^{15} (1≤j≤Q1 \le j \le Q).
  • 1≤Tj≤10151 \le T_j \le 10^{15} (1≤j≤Q1 \le j \le Q).
  • (Sj,Tj)≠(Sk,Tk)(S_j, T_j) \ne (S_k, T_k) (1≤j<k≤Q1 \le j < k \le Q).
  • NN, QQ, AiA_i, SjS_j, TjT_j are integers.

Examples5

  1. Example 1

    Input
    7
    1 J
    2 O
    3 G
    4 I
    5 O
    8 G
    10 J
    2
    3 2
    7 5
    
    Expected output
    7
    12
    
  2. Example 2

    Input
    10
    5 J
    7 O
    10 J
    11 G
    12 J
    13 I
    17 J
    18 J
    19 J
    20 J
    4
    4 9
    15 14
    6 20
    7 20
    
    Expected output
    13
    19
    20
    21
    
  3. Example 3

    Input
    10
    1 G
    2 J
    3 G
    4 O
    7 G
    9 J
    10 G
    14 I
    17 G
    19 G
    7
    11 6
    6 3
    17 19
    1 18
    17 17
    20 1
    20 10
    
    Expected output
    25
    27
    28
    17
    26
    39
    30
    
  4. Example 4

    Input
    10
    3 J
    5 G
    6 I
    7 I
    8 J
    9 I
    10 O
    14 G
    16 I
    19 J
    6
    4 4
    20 3
    18 5
    15 4
    20 11
    10 8
    
    Expected output
    12
    17
    15
    15
    19
    12
    
  5. Example 5

    Input
    12
    179948747891578 I
    263779425244614 I
    320153642407146 G
    383698990675423 J
    478483318441339 J
    505589213620811 G
    507468309040564 O
    530441288489671 J
    730036011088087 O
    896127332008998 I
    899298512463927 O
    915990785839829 J
    10
    744829561026263 366031656398270
    700496830781726 684771674298690
    314138534887378 222241904398827
    695615197615084 632164325876673
    418419052313523 409258287819812
    932490604948180 62799105708059
    738126150487131 45378717857226
    320047965627255 918203067583346
    859632377126681 967370566306944
    115848334010451 834089404672067
    
    Expected output
    583302366935305
    805077987955000
    591304613119987
    757352272699625
    478217003098189
    869691499240121
    805495866954969
    1085532869547991
    928541333618299
    1205618838253516