PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#SRD0000082. Demen và những truy vấn lẻ (Demen and Odd Queries)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Demen và những truy vấn lẻ (Demen and Odd Queries)

Demen and Odd Queries

Source: VNOJ

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn integer intervals [Li,Ri][L_i,R_i] on positions 1 through nn. Each query specifies mm pairwise distinct integer points. Count how many of the original intervals contain an odd number of these points, with endpoints included. Queries are independent.

Input

Read nn, then nn pairs Li,RiL_i,R_i, then qq. Each query consists of mm followed by exactly mm distinct points; there is no leading test-case count TT.

Output

Print the number of original intervals containing an odd number of query points.

Subtasks

  • Subtask 1 (10 points): 1≤n≤3001\le n\le 300, 1≤q≤3001\le q\le 300, q≤nq\le n. 1≤Li≤Ri≤n1\le L_i\le R_i\le n; 1≤Xj≤n1\le X_j\le n, các XjX_j đôi một khác nhau; ∑m≤n\sum m\le n; 1≤q≤1051\le q\le10^5.
  • Subtask 2 (90 points): 1≤n≤1000001\le n\le 100000, 1≤q≤1000001\le q\le 100000, q≤nq\le n. 1≤Li≤Ri≤n1\le L_i\le R_i\le n; 1≤Xj≤n1\le X_j\le n, các XjX_j đôi một khác nhau; ∑m≤n\sum m\le n; 1≤q≤1051\le q\le10^5.

Examples

Input

5
4 5
3 5
2 4
1 3
5 5
2
4 1 2 3 4
1 4

Output

3
3

Explanation

For points {1,2,3,4}\{1,2,3,4\}, the five intervals contain respectively 1,2,3,3,0 points, giving three odd counts. With the single point 4, exactly intervals [4,5][4,5], [3,5][3,5], and [2,4][2,4] contain it.