The problem is to process Q queries of the form X on N pairs of integers a[i] and b[i] (N, Q <= 2e5 and a[i], b[i] <= 1e6).
Assume there is a pointer initially pointing to a[i] or b[i]. For each query, if the number being pointed to is less than X, the pointer switches between a[i] and b[i]. For each query X, find the sum of the entire N-array based on the value pointed to by the pointer. Calculate the sum of all numbers in all configurations after going through Q queries.
Example input: 5 3
10 20
15 25
5 3
7 12
1 4
5
25
8
Output: 168 Explanation: (10+15+5+7+4) + (20+25+3+12+1) + (20+25+5+12+4)