CF1501A.Alexey and Train

传统题 时间 2000 ms 内存 256 MiB 3 尝试 1 已通过 1 标签

Alexey and Train

Alexey is travelling on a train. Unfortunately, due to the bad weather, the train moves slower that it should!

Alexey took the train at the railroad terminal. Let's say that the train starts from the terminal at the moment 00. Also, let's say that the train will visit nn stations numbered from 11 to nn along its way, and that Alexey destination is the station nn.

Alexey learned from the train schedule nn integer pairs (ai,bi)(a_i, b_i) where aia_i is the expected time of train's arrival at the ii-th station and bib_i is the expected time of departure.

Also, using all information he has, Alexey was able to calculate nn integers tm1,tm2,,tmntm_1, tm_2, \dots, tm_n where tmitm_i is the extra time the train need to travel from the station i1i - 1 to the station ii. Formally, the train needs exactly aibi1+tmia_i - b_{i-1} + tm_i time to travel from station i1i - 1 to station ii (if i=1i = 1 then b0b_0 is the moment the train leave the terminal, and it's equal to 00).

The train leaves the station ii, if both conditions are met:

  1. it's on the station for at least biai2\left\lceil \frac{b_i - a_i}{2} \right\rceil units of time (division with ceiling);
  2. current time bi\ge b_i.

Since Alexey spent all his energy on prediction of time delays, help him to calculate the time of arrival at the station nn.

Input

The first line contains one integer tt (1t1001 \le t \le 100) — the number of test cases.

The first line of each test case contains the single integer nn (1n1001 \le n \le 100) — the number of stations.

Next nn lines contain two integers each: aia_i and bib_i (1ai<bi1061 \le a_i \lt b_i \le 10^6). It's guaranteed that bi<ai+1b_i \lt a_{i+1}.

Next line contains nn integers tm1,tm2,,tmntm_1, tm_2, \dots, tm_n (0tmi1060 \le tm_i \le 10^6).

Output

For each test case, print one integer — the time of Alexey's arrival at the last station.

Note

In the first test case, Alexey arrives at station 11 without any delay at the moment a1=2a_1 = 2 (since tm1=0tm_1 = 0). After that, he departs at moment b1=4b_1 = 4. Finally, he arrives at station 22 with tm2=2tm_2 = 2 extra time, or at the moment 1212.

In the second test case, Alexey arrives at the first station with tm1=1tm_1 = 1 extra time, or at moment 22. The train, from one side, should stay at the station at least b1a12=2\left\lceil \frac{b_1 - a_1}{2} \right\rceil = 2 units of time and from the other side should depart not earlier than at moment b1=4b_1 = 4. As a result, the trains departs right at the moment 44.

Using the same logic, we can figure out that the train arrives at the second station at the moment 99 and departs at the moment 1010; at the third station: arrives at 1414 and departs at 1515; at the fourth: arrives at 2222 and departs at 2323. And, finally, arrives at the fifth station at 3232.

Samples

2
2
2 4
10 12
0 2
5
1 4
7 8
9 10
13 15
19 20
1 2 3 4 5
12
32

在线编程 IDE

建议全屏模式获得最佳体验