Skyscraper Floors

Time limit1sMemory limit128 MB

Summary
Given elevators each stopping on an arithmetic progression of floors starting at Y with step X, determine whether furniture can travel from floor A to floor B by switching elevators only on shared stop floors.
Level

Medium6 of 10

Topics
Union-find, Number theory, Graph
Solved
No attempts yet

Problem

Skyscrapers make excellent use of scarce, expensive land, but reaching the upper floors is slow. This particular skyscraper has an unusual elevator system: an elevator does not stop on every floor. Each elevator is described by two numbers X and Y. It has a starting floor Y and stops on every X-th floor from there, that is on floors Y, Y + X, Y + 2X, and so on. An elevator can never go below its starting floor Y.

The building has floors numbered 0 to F - 1. You may ride an elevator between any two floors on which it stops. To change from one elevator to another you must be on a floor where both of them stop. (There used to be a single high-capacity elevator that stopped on every elevator's starting floor and made transfers easy, but it is currently out of order.)

A heavy piece of furniture must be moved from floor A to floor B. It is too large to fit through the stairway door, so it can travel only by elevator. Decide whether it is possible to move the furniture from floor A to floor B.

Input

The first line contains one positive integer N, the number of test cases. Each test case is given as follows.

The first line of a test case contains four integers F, E, A, B:

  • F (1 <= F < 50000000) is the number of floors; the floors are numbered 0 to F - 1.
  • E (0 < E < 100) is the number of elevators.
  • A and B (0 <= A, B < F) are the floors between which the furniture must be moved.

Each of the next E lines describes one elevator with two integers X and Y (X > 0, Y >= 0): the elevator starts on floor Y and stops on every X-th floor, i.e. on floors Y, Y + X, Y + 2X, .... For example, X = 3, Y = 7 means the elevator stops on floors 7, 10, 13, 16, ....

Output

For each test case, print exactly one line. If floor B can be reached from floor A without using the stairway, print It is possible to move the furniture.; otherwise print The furniture cannot be moved.

Examples3

  1. Example 1

    Input
    2
    22 4 0 6
    3 2
    4 7
    13 6
    10 0
    1000 2 500 777
    2 0
    2 1
    
    Expected output
    It is possible to move the furniture.
    The furniture cannot be moved.
    
  2. Example 2

    Input
    1
    100 2 0 1
    2 0
    2 1
    
    Expected output
    The furniture cannot be moved.
    
  3. Example 3

    Input
    1
    100 1 10 40
    10 0
    
    Expected output
    It is possible to move the furniture.