PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GL0000311. Các trạm xe buýt (Bus Stops)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Các trạm xe buýt (Bus Stops)

Bus Stops

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

Takahashi wants to visit Aoki by walking and taking buses. There are NN bus stops numbered from 11 through NN along the route.

Walking from Takahashi's house to stop 11 takes XX units of time. For every 1≤i<N1\le i<N, buses depart from stop ii for stop i+1i+1 at every time that is a multiple of PiP_i, and the trip takes TiT_i units of time. After reaching stop NN, walking to Aoki's house takes another YY units of time.

Takahashi may board a bus even when he arrives at the stop exactly at its departure time. He may wait at stops, but wishes to arrive as early as possible.

You are given QQ departure times q1,…,qQq_1,\ldots,q_Q. For each departure time, determine the earliest possible arrival time at Aoki's house.

Input

  • The first line contains three integers N,X,YN,X,Y.
  • The next N−1N-1 lines each contain Pi,TiP_i,T_i, the departure interval and travel time between stops ii and i+1i+1.
  • The next line contains QQ.
  • Each of the following QQ lines contains one departure time qiq_i.

Output

Print QQ lines, where line ii is the earliest arrival time when departing at qiq_i.

Subtasks

  • Subtask 0 (20 points): 2≤N≤122\le N\le 12; 1≤Q≤201\le Q\le 20; 1≤X,Y≤10001\le X,Y\le 1000; 1≤Pi≤81\le P_i\le8 and 1≤Ti≤10001\le T_i\le 1000 for all 1≤i<N1\le i<N; 0≤qi≤10000\le q_i\le 1000 for all 1≤i≤Q1\le i\le Q.
  • Subtask 1 (30 points): 2≤N≤15002\le N\le 1500; 1≤Q≤20001\le Q\le 2000; 1≤X,Y≤10000000001\le X,Y\le 1000000000; 1≤Pi≤81\le P_i\le8 and 1≤Ti≤10000000001\le T_i\le 1000000000 for all 1≤i<N1\le i<N; 0≤qi≤10000000000\le q_i\le 1000000000 for all 1≤i≤Q1\le i\le Q.
  • Subtask 2 (50 points): 2≤N≤1000002\le N\le 100000; 1≤Q≤2000001\le Q\le 200000; 1≤X,Y≤10000000001\le X,Y\le 1000000000; 1≤Pi≤81\le P_i\le8 and 1≤Ti≤10000000001\le T_i\le 1000000000 for all 1≤i<N1\le i<N; 0≤qi≤10000000000\le q_i\le 1000000000 for all 1≤i≤Q1\le i\le Q.

Examples

Example 1

Input

4 2 3
5 4
6 6
3 1
7
13
0
710511029
136397527
763027379
644706927
447672230

Output

34
22
710511052
136397548
763027402
644706946
447672250

Explanation

For q1=13q_1=13, he walks to stop 1 at time 1515, takes a bus to stop 2 arriving at 1919, waits until 2424 for the next leg, reaches stop 3 at 3030, boards immediately and reaches stop 4 at 3131, then walks for 33 units, arriving at 3434. For q2=0q_2=0, he reaches stop 1 at 22, boards buses at 55, 1212, and 1818, and arrives at 2222. The remaining five lines correspond to the other five departure times.