欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF1501A.Alexey and Train
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 . Also, let's say that the train will visit stations numbered from to along its way, and that Alexey destination is the station .
Alexey learned from the train schedule integer pairs where is the expected time of train's arrival at the -th station and is the expected time of departure.
Also, using all information he has, Alexey was able to calculate integers where is the extra time the train need to travel from the station to the station . Formally, the train needs exactly time to travel from station to station (if then is the moment the train leave the terminal, and it's equal to ).
The train leaves the station , if both conditions are met:
- it's on the station for at least units of time (division with ceiling);
- current time .
Since Alexey spent all his energy on prediction of time delays, help him to calculate the time of arrival at the station .
Input
The first line contains one integer () — the number of test cases.
The first line of each test case contains the single integer () — the number of stations.
Next lines contain two integers each: and (). It's guaranteed that .
Next line contains integers ().
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 without any delay at the moment (since ). After that, he departs at moment . Finally, he arrives at station with extra time, or at the moment .
In the second test case, Alexey arrives at the first station with extra time, or at moment . The train, from one side, should stay at the station at least units of time and from the other side should depart not earlier than at moment . As a result, the trains departs right at the moment .
Using the same logic, we can figure out that the train arrives at the second station at the moment and departs at the moment ; at the third station: arrives at and departs at ; at the fourth: arrives at and departs at . And, finally, arrives at the fifth station at .
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
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |