The Final Countdown

Time limit1sMemory limit128 MB

Problem

When Doctor Doom modeled LAI-BACH (the Latverian Aerospace Institute, Bolograd Active Control Headquarters) on NASA's Mission Command, he perhaps copied from the source a little too closely.

NASA — and therefore LAI-BACH — handle countdowns in a peculiar way. Even though the clock may say, for example, that liftoff is thirty minutes away, there are built-in holds during which the clock is stopped for a fixed amount of time. For instance, there may be a 15-minute hold when the countdown reaches the 8-minute mark. As a result, more real time passes from the beginning of the countdown to the end than the numbers on the clock would suggest.

LAI-BACH manages the holds in its countdowns with a simple "command file" format. Every command begins with a positive integer no greater than 1440 (countdowns longer than a day make Doctor Doom angry), followed by a directive. The commands have the following forms:

CommandMeaning
t STARTt is the start time on the countdown clock, in minutes.
t HOLD nA mandatory hold of n minutes at countdown clock time t.
t HOLD n IF conditionA hold of n minutes at countdown clock time t if condition is true.
t HOLD n IF NOT conditionA hold of n minutes at countdown clock time t if condition is not true.

Each condition is a short string of lowercase letters, at most 20 characters long. Its actual value is not given in the command file, because it depends on real launch circumstances (whether the fuel tanks are topped off, whether the Fantastic Four are currently fighting Doctor Doom, and so on). Even so, every condition is always either true or false, and does not change during a single countdown. A hold time n is a positive integer no greater than 60 minutes, since long delays also irritate Victor.

A hold only affects the countdown if the clock actually reaches its time t. Because the clock counts down from the start time to 0, a hold whose time t is greater than or equal to the start time never occurs.

Given a command file, determine both the shortest possible and the longest possible total time that the countdown can take. The commands may appear in any order, there is always exactly one START directive, each line contains precisely the whitespace shown in the format above, and no two commands reference the same minute t.

Input

The first line of input contains a single integer N (1 ≤ N ≤ 100), the number of data sets. Each data set consists of the following:

  • a line containing a single integer L (1 ≤ L ≤ 100), the number of lines in the "command file"; followed by
  • L lines forming the command file, in the format described above.

Output

For each data set, print a line of the form min TO max, where min is the shortest possible duration of the countdown in minutes and max is the longest possible duration of the countdown in minutes.