Consecutive Ordering

No attempts yetTime limit1sMemory limit256 MB

Problem

An interval graph is the intersection graph of a family F={I1,I2,,In}F = \{I_1, I_2, \ldots, I_n\} of closed intervals on the real line. Two vertices viv_i and vjv_j are joined by an edge exactly when the corresponding intervals IiI_i and IjI_j intersect. The family FF is called an interval representation of the graph. A unit interval graph is an interval graph for which some interval representation has all intervals of the same length. Figure 1 shows a unit interval graph together with an interval representation of it.

(a) A unit interval graph GG.

(b) An interval representation of the graph GG.

Figure 1. A unit interval graph and its interval representation.

The closed neighbourhood N[vi]N[v_i] of a vertex viv_i of a graph GG is the set of vertices adjacent to viv_i together with viv_i itself, that is, N[vi]={vi}{vj:vjviE(G)}N[v_i] = \{v_i\} \cup \{v_j : v_j v_i \in E(G)\}. For the graph GG of Figure 1(a), N[v1]={v1,v2,v3}N[v_1] = \{v_1, v_2, v_3\} and N[v5]={v2,v3,v4,v5,v6}N[v_5] = \{v_2, v_3, v_4, v_5, v_6\}.

Write ρ(vi)\rho(v_i) for the position of viv_i in an ordering of the vertices. Under the ordering (v1,v2,,v17)(v_1, v_2, \ldots, v_{17}) of the vertices of GG, in which ρ(vi)=i\rho(v_i) = i for every viv_i, the closed neighbourhood N[vi]N[v_i] of every vertex is consecutive: the positions of the vertices in N[vi]N[v_i] are integers that follow one after another, without gaps, from the smallest to the largest. An ordering of the vertices of a graph is consecutive if the closed neighbourhood of every vertex of the graph is consecutive. For the graph GG of Figure 1(a), the ordering (v1,v2,,v15,v17,v16)(v_1, v_2, \ldots, v_{15}, v_{17}, v_{16}) with v16v_{16} and v17v_{17} exchanged, where ρ(v17)=16\rho(v_{17}) = 16, ρ(v16)=17\rho(v_{16}) = 17, and ρ(vi)=i\rho(v_i) = i for every other vertex, is consecutive as well. The ordering (v2,v1,v3,v4,,v17)(v_2, v_1, v_3, v_4, \ldots, v_{17}) is not consecutive, because the closed neighbourhood of v5v_5 is not consecutive.

You are given nn closed intervals of the same length, I1,I2,,InI_1, I_2, \ldots, I_n, listed in non-decreasing order of their left endpoints, and an ordering (vi1,vi2,,vin)(v_{i_1}, v_{i_2}, \ldots, v_{i_n}) of the vertices of the unit interval graph defined on those nn intervals. Write a program that decides whether the ordering is consecutive.

Input

Your program reads from standard input. The input consists of TT test cases, and TT is given on the first line.

Each test case begins with a line containing two positive integers nn and ll, the number of intervals and their common length, where nn is at most 100,000 and ll is at most 100,000,000. The next nn lines contain the left endpoints of I1,I2,,InI_1, I_2, \ldots, I_n, one per line, and the left endpoint of IiI_i is less than or equal to the left endpoint of IjI_j whenever i<ji < j. Every endpoint, left or right, lies between -100,000,000 and 100,000,000, inclusive. The following nn lines contain an ordering of the vertices of the unit interval graph defined on those intervals, one vertex number per line, from the first position to the last.

Output

Your program writes to standard output. For each test case, print exactly one line containing an integer that tells whether the given ordering is consecutive. Print 1 if it is, and -1 otherwise.