Traveling Merchant
题号:NC219655
时间限制:C/C++/Rust/Pascal 2秒,其他语言4秒
空间限制:C/C++/Rust/Pascal 256 M,其他语言512 M
64bit IO Format: %lld

题目描述

There is a long east-west road which has  towns situated along it, numbered  to  from west to east. All towns buy and sell the same kind of goodie. The value of a goodie fluctuates according to a weekly schedule. A town buys and sells a goodie at its value in that town on that particular day. At town , the value of a goodie changes by  every day in the first half of a week, and changes by  every day in the second half of a week. In other words, the value of a goodie at town  is  on Mondays and Sundays,  +  on Tuesdays and Saturdays,   on Wednesdays and Fridays, and  on Thursdays.

A merchant is making a business travel plan. His trip begins at a starting town  and ends at a destination town , visiting each town from  to  (inclusive) exactly once. The merchant starts the trip on a Monday. It takes him exactly one day to travel between adjacent towns and every day he travels to the next town on the path to the destination. He may buy exactly one goodie at a town along the trip and sell that goodie at a town he visits later. He can only buy once and sell once. The merchant would like to know the maximum possible profit of  travel plans with different choices of town  and town .

输入描述:

The first line of the input has a single integer . The next  lines each have two integers. The -th line has  and . The next line has a single integer . The following  lines each give a pair of integers  and , representing one travel plan from town  to town . If  the merchant travels west to east, otherwise he travels east to west.

输出描述:

For each travel plan, output the maximum profit the merchant can make on a single line. If the merchant cannot make any profit, output .
示例1

输入

复制
5
1 2
2 1
5 0
4 -1
7 -2
5
1 5
5 1
3 1
4 5
5 4

输出

复制
4
2
2
1
0