No Wormholes Were Harmed...

Time limit3sMemory limit128 MB

Problem

As director of the Causality Infraction Agency, your job is to track down and arrest people who try to alter the course of history.

A mission briefing names the exact year a time agent must reach, but time travel is not that simple. An agent can only move through wormholes, each of which connects two specific years. To reach a destination year an agent usually has to pass through several wormholes in sequence, and may have to wait in the past or future for the next wormhole to appear. Passing through a wormhole is not free either: moving forward in time instantly ages the traveller, while moving backward makes the traveller slightly younger.

Because the agency pays agents according to how many years they have aged since joining, you must minimize the aging of every agent. Write a program that finds the itinerary of wormhole jumps that minimizes aging and reports the total number of years each agent ages after completing the mission.

Aging is computed as follows.

  • Waiting. Waiting from an origin year until a later destination year ages the body by

    $$\text{destination} - \text{origin}$$

    For example, waiting from year 1785 until year 1793 ages you 8 years. (Waiting only moves time forward; you cannot wait your way into the past.)

  • Traveling forward through a wormhole whose arrival year is later than its departure year ages the body by

    $$\left\lfloor \frac{\text{arrival} - \text{departure}}{2} \right\rfloor$$

    that is, half (rounded down) of the years you would have aged by simply waiting. Over a small enough gap this may round down to zero.

  • Traveling backward through a wormhole whose arrival year is earlier than its departure year makes the body younger, gaining back

    $$\left\lfloor \frac{\text{departure} - \text{arrival}}{4} \right\rfloor$$

    years — a quarter of the difference, rounded down. Over a small enough gap this may round down to zero.

  • A wormhole whose departure and arrival years are equal is allowed, but it causes neither aging nor travel.

Each dataset gives a single starting year from which every agent begins, and a list of missions, one per agent. Each mission names a final destination year. A mission can be completed only if the agent can make the round trip: travel from the starting year to the destination year and then back to the starting year. If no such round trip exists, the mission is invalid. You do not need to consider any agent's maximum lifespan — a mission is valid as long as the round trip is possible, no matter how large the total aging.

Input

The first line contains a single integer $N$ ($1 \le N \le 100$), the number of datasets. Each dataset has the following form:

  • A line with a single integer $W$ ($1 \le W \le 100$), the number of wormholes in this dataset.
  • $W$ lines, each of the form D A ($1 \le D, A \le 9999$), describing one wormhole whose departure year is $D$ and whose arrival year is $A$. A wormhole is one-directional: it allows travel only from year $D$ to year $A$, never the reverse.
  • A line with a single integer $S$ ($1 \le S \le 9999$), the starting year shared by all agents.
  • A line with a single integer $M$ ($1 \le M \le 100$), the number of missions to analyze.
  • $M$ lines, each with a single integer $F$ ($1 \le F \le 9999$), the final destination year of one mission.

Output

For each dataset, first output a line DATA SET #k, where $k$ is $1$ for the first dataset, $2$ for the second, and so on. Then output $M$ lines, one per mission, in the same order as the input. Each line contains either a single integer — the number of years that agent ages — or the word IMPOSSIBLE if the mission cannot be completed (because the destination is unreachable or the return trip is not possible).