This page is still under construction.

Parts of this page are still being built. What you see may change.

Call Forwarding

Interview

Time limit1sMemory limit128 MB

Summary
Given timestamped call forwarding rules, follow each call through the active chain, printing the final extension or 9999 if a loop is entered.
Level

Medium5 of 10

Topics
Simulation, Hash map, Graph, Implementation
Solved
No attempts yet

Problem

Modern office phone systems can automatically forward incoming calls. For example, when an employee goes on vacation, they can arrange for every call that reaches their extension to be forwarded to a colleague. This problem simulates how such a system keeps track of call forwarding.

Every phone at the company has a four-digit extension. To set up call forwarding, an employee enters:

  • their own extension (the source),
  • the time at which forwarding begins (time),
  • how long the forwarding lasts (its duration),
  • the extension that calls should be forwarded to (the target).

The rules are:

  • Every extension has exactly four digits.
  • The extensions 0000 and 9999 are reserved for special use and are never entered by an employee.
  • Time is measured in whole hours on a clock that starts at 0000 at midnight on New Year's Eve, so every time is an integer from 0000 to 8784 (which is 366×24366 \times 24). The system is completely reset at the start of each year.
  • A forwarding rule that begins at time XX with duration YY is in effect for every time TT with X≤T≤X+YX \le T \le X + Y (both ends inclusive).

Employees always enter well-formed requests: they follow the format, no request runs past the end of the year, and no employee enters two requests for their own extension that overlap in time. As a result, at any moment each extension has at most one active forwarding rule.

Even so, forwarding can form a loop. For example, if A forwards to B, B forwards to C, and C forwards back to A, then a call to any of them would be forwarded forever. To handle this, whenever the forwarding chain followed from a called extension reaches an extension it has already passed through (that is, the chain enters a cycle), the call instead rings the special dead-end extension 9999.

Input

The first line contains an integer NN (1≤N≤101 \le N \le 10): the number of independent call-forwarding systems to simulate.

Each system is described in two parts.

First come 0 to 100 forwarding requests, one per line, each in the form source time duration target (four four-digit numbers). They are listed in the order received. A line whose first field is 0000 ends this part.

Then come one or more calls, one per line, each in the form time extension, listed in non-decreasing order of time. Each such line means a call is placed at that time to that extension. A line whose first field is 9000 ends this part.

Output

For each system, in order, print a line SYSTEM N, where N is the system's number (1, 2, ...). After that header, print one line for every call placed into that system, in input order, in the format:

AT tttt CALL TO eeee RINGS rrrr

where tttt is the call's time, eeee is the dialed extension, and rrrr is the extension the call finally rings. Starting from eeee, follow the active forwarding rules: if the chain reaches an extension with no active forwarding, that extension rings; if the chain enters a loop, the call rings 9999. All four values are printed as four digits.

Examples1

  1. Example 1

    Input
    2
    1111 0100 0200 2222
    1111 0301 0500 4444
    2222 0200 0200 3333
    3333 0250 1000 1111
    7777 1000 2000 7777
    0000
    0050 1111
    0150 1111
    0200 1111
    0225 2222
    0270 1111
    0320 1111
    0320 3333
    0900 3000
    1250 3333
    1250 7777
    9000
    0000
    3000 1111
    9000
    
    Expected output
    SYSTEM 1
    AT 0050 CALL TO 1111 RINGS 1111
    AT 0150 CALL TO 1111 RINGS 2222
    AT 0200 CALL TO 1111 RINGS 3333
    AT 0225 CALL TO 2222 RINGS 3333
    AT 0270 CALL TO 1111 RINGS 9999
    AT 0320 CALL TO 1111 RINGS 4444
    AT 0320 CALL TO 3333 RINGS 4444
    AT 0900 CALL TO 3000 RINGS 3000
    AT 1250 CALL TO 3333 RINGS 1111
    AT 1250 CALL TO 7777 RINGS 9999
    SYSTEM 2
    AT 3000 CALL TO 1111 RINGS 1111