Points

Time limit1sMemory limit128 MB

Summary
Given up to 10000 directional rules between n points, decide whether real coordinates satisfy all of them at once.
Level

Medium7 of 10

Topics
Graph, DFS, Implementation
Solved
No attempts yet

Problem

There are nn points p1,p2,…,pnp_1, p_2, \ldots, p_n on the plane. Let the coordinates of point pip_i be (xi,yi)(x_i, y_i). You are given mm rules of the form pi  rel  pjp_i \; rel \; p_j, each stating that the relation relrel holds between the positions of points pip_i and pjp_j. For example, "pip_i NE pjp_j" indicates that point pjp_j lies to the NorthEast of point pip_i.

The relation relrel is one of eight kinds {N,E,S,W,NE,NW,SE,SW}\{N, E, S, W, NE, NW, SE, SW\}, corresponding to the eight directions on the plane. Depending on the value of relrel, pi  rel  pjp_i \; rel \; p_j means exactly one of the following:

  1. N (North): xj=xix_j = x_i and yj>yiy_j > y_i
  2. E (East): xj>xix_j > x_i and yj=yiy_j = y_i
  3. S (South): xj=xix_j = x_i and yj<yiy_j < y_i
  4. W (West): xj<xix_j < x_i and yj=yiy_j = y_i
  5. NE (NorthEast): xj>xix_j > x_i and yj>yiy_j > y_i
  6. NW (NorthWest): xj<xix_j < x_i and yj>yiy_j > y_i
  7. SE (SouthEast): xj>xix_j > x_i and yj<yiy_j < y_i
  8. SW (SouthWest): xj<xix_j < x_i and yj<yiy_j < y_i

Determine whether it is possible to place the points p1,p2,…,pnp_1, p_2, \ldots, p_n on the plane so that all given rules are satisfied.

Input

The first line contains a single integer tt (1≤t≤201 \le t \le 20), the number of test cases. The first line of each test case contains two integers nn (2≤n≤5002 \le n \le 500), the number of points, and mm (1≤m≤1041 \le m \le 10^4), the number of rules. Each of the following mm lines contains one rule of the form i  rel  ji \; rel \; j, meaning that point pip_i has relation relrel with point pjp_j.

Output

For each test case, print a single line containing either POSSIBLE or IMPOSSIBLE, indicating whether the points can be placed on the plane according to the given rules.

Examples1

  1. Example 1

    Input
    2
    3 2
    1 N 2
    2 N 1
    6 6
    1 E 2
    1 E 3
    2 N 4
    3 NW 5
    4 SW 6
    6 NE 5
    
    Expected output
    IMPOSSIBLE
    POSSIBLE