#P1843E. Tracking Segments
Tracking Segments
Description
You are given an array consisting of zeros. You are also given a set of not necessarily different segments. Each segment is defined by two numbers and () and represents a subarray of the array .
Let's call the segment beautiful if the number of ones on this segment is strictly greater than the number of zeros. For example, if , then the segment is beautiful (the number of ones is , the number of zeros is ), but the segment is not is beautiful (the number of ones is , the number of zeros is ).
You also have changes. For each change you are given the number , which means that you must assign an element the value .
You have to find the first change after which at least one of given segments becomes beautiful, or report that none of them is beautiful after processing all changes.
The first line contains a single integer () — the number of test cases.
The first line of each test case contains two integers and () — the size of the array and the number of segments, respectively.
Then there are lines consisting of two numbers and () —the boundaries of the segments.
The next line contains an integer () — the number of changes.
The following lines each contain a single integer () — the index of the array element that needs to be set to . It is guaranteed that indexes in queries are distinct.
It is guaranteed that the sum of for all test cases does not exceed .
For each test case, output one integer — the minimum change number after which at least one of the segments will be beautiful, or if none of the segments will be beautiful.
Input
The first line contains a single integer () — the number of test cases.
The first line of each test case contains two integers and () — the size of the array and the number of segments, respectively.
Then there are lines consisting of two numbers and () —the boundaries of the segments.
The next line contains an integer () — the number of changes.
The following lines each contain a single integer () — the index of the array element that needs to be set to . It is guaranteed that indexes in queries are distinct.
It is guaranteed that the sum of for all test cases does not exceed .
Output
For each test case, output one integer — the minimum change number after which at least one of the segments will be beautiful, or if none of the segments will be beautiful.
Note
In the first case, after first 2 changes we won't have any beautiful segments, but after the third one on a segment there will be 3 ones and only 2 zeros, so the answer is 3.
In the second case, there won't be any beautiful segments.