Chef and odd queries

All submissions for this problem are available.
Read problems statements in Mandarin chinese, Russian and Vietnamese as well.
Chef has N segments. For each i (1 ≤ i ≤ N), the ith segment starts at point L_{i} and ends at point R_{i} (let's denote it by [L_{i}, R_{i}]).
Let's say that a segment [l, r] crosses a point x if l ≤ x ≤ r.
Chef wants you to answer Q queries. In each query, you are given a set of M distinct points X_{1}, X_{2}, ..., X_{M}; you should compute the number of good segments [L_{i}, R_{i}] among the given N segments. A segment is good if it crosses an odd number of points out of the given M points.
Input
 The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
 The first line of each test case contains a single integer N denoting the number of segments.
 The following N lines describe the segments. For each i, the ith of these lines contains two spaceseparated integers L_{i} and R_{i}.
 The next line contains a single integer Q denoting the number of queries.
 The following Q lines describe the queries. Each of these lines starts with an integer M — the number of points in this query, followed by M spaceseparated integers X_{1}, X_{2}, ..., X_{M}.
Constraints
 1 ≤ T ≤ 100
 1 ≤ Q ≤ N ≤ 100,000
 sum of M for all queries ≤ N
 sum of N over all test cases ≤ 200,000
 1 ≤ X_{i} ≤ N for each valid i
 all X_{i} in each query are distinct
 1 ≤ L_{i} ≤ R_{i} ≤ N for each valid i
Subtasks
Subtask #1 (10 points): N, Q ≤ 300
Subtask #2 (90 points): original constraints
Output
For each query, print a single line containing the number of good segments for that query.
Example
Input: 2 5 4 5 3 5 2 4 1 3 5 5 2 4 1 2 3 4 1 4 5 4 5 3 5 2 4 2 3 5 5 2 2 2 5 3 1 2 5 Output: 3 3 5 5
Author:  markysha 
Editorial  https://discuss.codechef.com/problems/CHANOQ 
Tags  feb18, markysha, mediumhard, persistence, segmenttree, sqrtdecomp 
Date Added:  21012018 
Time Limit:  4 sec 
Source Limit:  50000 Bytes 
Languages:  C, CPP14, JAVA, PYTH, PYTH 3.6, PYPY, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, rust, SCALA, swift, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, kotlin, PERL6, TEXT, SCM chicken, PYP3, CLOJ, COB, FS 
Comments
 Please login at the top to post a comment.
SUCCESSFUL SUBMISSIONS
Fetching successful submissions
HELP
If you are still having problems, see a sample solution here. 