Hide

Problem D
Match the Clocks

/problems/matchtheclocks/file/statement/en/img-0001.jpg
Image by Bukhavets Mikhail (Shutterstock); Used under license

Toto and Moto are two retired brothers who are passionate about digital electronics, so they decide to enroll in studies at the Atlantic Circuit Programming College (ACPC). For their graduation project, they each choose to make a digital clock. As they often disagree, they decide to work separately, i.e., each one makes his own clock without consulting the other.

At the end of their studies, Toto and Moto proudly hang their clocks for their classmates to see. Unfortunately, they soon realize that the two clocks are not updating their displays at the same rate. To avoid a new conflict between them about which clock is working correctly and which one isn’t, Toto and Moto decide to call on their friend Goto, who has a very accurate digital watch. A few days later, Goto arrives and starts her investigation. She soon realizes that one of the clocks has a flaw in how it keeps time. Goto knows that if she simply declares one clock to be accurate and the other to be inaccurate, the brother with the inaccurate clock will get angry and argue with her, unless she provides convincing proof, and the best way to provide convincing proof is to wait until the first instant when both clocks are displaying the same time, and then start monitoring them, keeping a close eye on her digital watch, which both Toto and Moto agree is 100% accurate. After a while, the two clocks will no longer display the same time, at which point Goto can use her watch to convince the brother with the inaccurate clock that he made an error when programming his clock.

It is helpful to understand a little about how Toto’s and Moto’s clocks work. Each clock has a very precise internal quartz oscillator that produces a signal with frequency exactly $f~ \textrm{Hz}$ $(f$ “ticks” per second), and the two oscillators are perfectly synchronized (they were synchronized at the factory, and have never deviated since). The accurate clock is correctly programmed to advance the time it displays by $1~ \textrm{second}$ every $f~ \textrm{ticks}$ of its oscillator, but the inaccurate clock advances by $1~ \textrm{second}$ every $f'$ ticks, where $f' \neq f.$

Given the relevant information about Toto’s and Moto’s clocks, please help Goto determine when both clocks will first display the same time, so that she knows how long she needs to wait before she can begin her experiment.

Input

The first line of input contains an integer, $T,$ the number of test cases $(1 \leq T \leq 100).$ Each test case consists of $4~ \textrm{lines:}$

  • The first line contains $3$ space-separated integers, $h~ m~ s,$ giving the time displayed on Toto’s clock in the $24\textrm{-hour}$ [hours, minutes, seconds] format $(0 \leq h < 24, 0 \leq m,s < 60).$

  • The second line gives the time displayed on Moto’s clock in the same format.

  • The third line contains two space-separated integers, $f$ and $e,$ the first of which is the oscillator frequency, $f,$ described above $(1 \leq f \leq 1\, 000\, 000),$ and the second of which is the “time shift” or “time error” of the inaccurate clock, i.e., the amount by which the value $f'$ (also described above) differs from $f.$ A negative (respectively, positive) value $\textrm{of}~ e$ means that $f'$ is less than (respectively, greater than) $f.$ Note that $e \neq 0$ and $e \leq 1\, 000\, 000$. If $e$ is negative, then $e$ is less than $f$ in absolute value.

  • The fourth line contains the name of the brother with the accurate clock, either “Toto” or “Moto”.

In each test case, the two clocks have just advanced their displayed times on the same oscillator tick, and the two displayed times are different.

Output

For each test case, output a line of the form

$D$day(s) $H$hour(s) $M$minute(s) $S$second(s)

giving the minimum amount of time (rounded up to the nearest second) that Goto needs to wait before both clocks display the same time, in days, hours, minutes, and seconds, respectively $(0 \leq D, 0 \leq H < 24, 0 \leq M,S < 60).$

Fine point: Note that “display the same time” means “begin to display the same time for a time interval of non-zero length.” So, for example, if on a certain oscillator tick Toto’s clock display changes from $[11,59,59]$ to $[12,0,0],$ and on the same tick Moto’s clock display changes from $[12,0,0]$ to $[12,0,1],$ we do not consider that they have displayed the same time (even though one could argue that they both displayed $[12,0,0]$ for an infinitesimally small time interval).

Sample Input 1 Sample Output 1
2
12 59 55
13 0 0
100 2
Toto
9 17 50
6 17 40
60 -13
Moto
0 day(s) 0 hour(s) 3 minute(s) 25 second(s)
3 day(s) 3 hour(s) 54 minute(s) 44 second(s)

Please log in to submit a solution to this problem

Log in