#P9824. Teyberrs

Teyberrs

Teyberrs

Problem Description

Teyberrs is a paradise for birds to live in. Assume you are a bird in Teyberrs, you are now flying somewhere like the game ''Flappy Bird''. You start flying at (0,s)(0,s), and every time when you are at (x1,y)(x-1,y) (1xn1\leq x\leq n), you must fly to either (x,y1)(x,y-1) with cost axa_x or (x,y+1)(x,y+1) with cost bxb_x. Like the map in ''Flappy Bird'', you can not hit obstacles at (x,y)(x,y) where y<lxy < l_x or y>rxy > r_x. You will be given qq queries. In each query, you will be given two integers xx and yy. Assume your target is at (x,y)(x,y), can you find the path with the minimum cost, or determine it is impossible?

Input

The first line contains a single integer TT (1T2001 \leq T \leq 200), the number of test cases. For each test case: The first line of the input contains three integers nn, qq and ss (1n,q2000001 \leq n,q \leq 200\,000, 1sn1\leq s\leq n), denoting the size of the map, the number of queries, and the start point. In the next nn lines, the ii-th line contains four integers aia_i, bib_i, lil_i and rir_i (1ai,bi1091\leq a_i,b_i\leq 10^9, 1lirin1\leq l_i\leq r_i\leq n). In the next qq lines, the ii-th line contains two integers xx and yy (1x,yn1\leq x,y\leq n), describing a target point. It is guaranteed that the sum of all nn is at most 10000001\,000\,000, and the sum of all qq is at most 10000001\,000\,000.

Output

For each query, print a single line containing an integer, denoting the minimum total cost. When it is impossible to reach the target, please print ''-1\texttt{-1}'' instead.

Sample Input

1
3 9 2
1 2 1 3
3 1 2 3
4 3 1 2
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3

Sample Output

1
-1
2
-1
2
-1
6
-1
-1

Source

2023“钉耙编程”中国大学生算法设计超级联赛(3)