[Question]
  [
*The shortest code to generate the correct times on the clocks wins.*
You are a seasoned time traveler and have been known to stop at many planets during your journies. Each planet rotates at a different rate and because of this, the length of a day is different than our usual 24-hour day. As a result, the planets use clocks with different numbers of hours. The hours on a clock with *x* hours are arranged similar to ours (1, 2, 3, ... , *x*) with the number rotating clockwise and *x* being at the top.
Additionally, each planet has a different amount of minutes in an hour, and a different number of seconds in a minute. You will be given a starting time and a number of elapsed seconds from which you must determine the ending time.
Input can be taken directly from a file passed as an argument, or as standard input. The first line of input will be the number of clocks you need to process. After that, each clock has three lines of input that contains integers in the following format:
```
x y z
h m s
t
```
The meaning of each letter is below.
> 
> x = The number of hours in a day (2 <= x <= 99)  
> 
>  y = The number of minutes in an hour (2 <= y <= 100)  
> 
>  z = The number of seconds in a minute (2 <= z <= 100)  
> 
>  h = The hour of the starting time (1 <= h <= x)  
> 
>  m = The minute of the starting time (0 <= m < y)  
> 
>  s = The second of the starting time (0 <= s < z)  
> 
>  t = The number of seconds that have elapsed
> 
> 
> 
The output must be the ending time for each clock after *t* seconds have passed since the starting time. Your output must be formatted as standard clock time (HH:MM:SS). Numbers should be padded, when necessary, to ensure that all numbers are two-digits.
## Test Cases
---
**Input**
```
2
5 20 10
1 10 5
2633
6 25 5
6 0 3
290
```
**Output**
```
04:13:08
02:08:03
```
---
**Input**
```
1
14 17 11
12 16 10
1530
```
**Output**
```
07:03:00
```
---
**Input**
```
2
8 40 25
3 1 15
10620
14 15 20
1 14 0
-580
```
**Output**
```
05:26:10
14:00:00
```
  
[Answer]
## Python, 142 characters
```
R=raw_input
for i in' '*input():x,y,z,h,m,s=map(int,(R()+i+R()).split());t=input()+h*y*z+m*z+s;print'%02d:%02d:%02d'%((t/y/z-1)%x+1,t/z%y,t%z)
```
[Answer]
## GolfScript - 50 chars
```
~](;7/{{~+.4$/\4$%])\}3*3<)@\or\+{100+`(;}%':'*n}%
```
The values (H/M/S) are collected by moving them to the front of the stack (`])\`). The hour 'underflow' at 0 is handled with `or`. Zero padding is handled with `100+`(;`, although I suppose `0`\+-2>` is the same length.
[Answer]
# GolfScript 62 60 characters
Edit: I managed to get the array formerly stored in a to reside on the stack, it takes a little extra switching that way though so no major improvement.
```
~](\7/\{(~+[]\{.5$/@@5$%\+@@+}3*;\;(@or\+{'0'\+-2>}%':'*n@}*
```
62 version:
```
~](\7/\{[]:a;(~{+.4$/\4$%a+:a;}3*;;;a(@or\+{'0'\+-2>}%':'*n@}*
1______a2____3_b4_5___6__7____8__9__10_____11_________12____13
```
I'm sure it can be done a lot better, I just couldn't think of anything better.
1: Make an array of all input, pick off the first element, group the rest into blocks of 7.  
a/13: Consume the first number from the input to run the loop that number of times.  
2: Store an empty array in a.  
3: Pick a block of 7 and expand it to 7 individual numbers.  
b/8: Run a loop 3 times, once for each of seconds, minutes and hours.  
4: Add the last two numbers together, for the first iteration that is seconds and time to shift, for the following it is minutes and hour with the overflow from the previous cycle. Make a second copy of the result.  
5: Divide the copy by it's limit to produce the overflow and shift the result back one space.  
[Answer]
## J (172/35) 137 99 107
**Now passes all given test cases.**
```
4(1!:2)~LF,~"1([,':',])/"2,"2":"0(10 10#:1 0 0+{.#:{.&{:+{.#.1 0 0-~1&{)"2&(,&}.$~,&3 3&{.&{.)".;._2(1!:1)3
```
172 is the whole thing; 35 is the number of characters I would give if I was really snooty and refused to do the IO as indicated. (I still modified it a little bit; clocks is a function taking a filename meant to be used interactively within J.)
I sure hope this is a lot easier in J than I make it look.
Edit: Figured out how to do better input parsing in J, eliminated charsub, switched to command line invocation & output.
Edit 2: Changed input of central function to 3x3 matrix, eliminated many pesky parentheses, eliminated names
Edit 3: 0-o'clock handled.
Explanation:
My J still isn't great, and IO is a pain as always. So bits of this are loony.
* The verb `1 0 0+{.#:{.&{:+{.#.1 0 0-~1&{` takes a three by three matrix (consisting of the input lines, the last two elements are garbage)
* The h/m/s is gotten with {. (head), the actual time with 1&{ (second element), and the second count with {.&{: (head of tail).
* The verb uses [#.](http://www.jsoftware.com/help/dictionary/d401.htm) to transform the clock time to seconds. (See documenation.)
* It adds the second count and then uses [#:](http://www.jsoftware.com/help/dictionary/d402.htm) to get the 3 element answer.
* The 0 o'clock case is handled by subtracting 1 from the hour before the base change and adding 1 back after. (the two bits with `1 0 0`)
* The rest is input and output, which is really grubby (as always).
* `".;._2(1!:1)3` gets a 3 'column' matrix of the input with 0s in unfilled positions.
* `,&}.$~,&3 3&{.&{.` cuts the first row off the input and shapes the remaining rows into Nx3x3.
* The `"2` modifies the central verb to take the 3x3 cases.
* `10 10&#:` gives 2 decimal digits for each number giving an Nx3x2 matrix. (Getting 0s for padding was a *pain*.)
* `,"2":"0` converts the digits to ASCII (Nx3x2x1) and ravels the last column, giving Nx3x2 again as ASCII.
* `LF,~"1([,':',])/"2` inserts : between each element and appends them (Nx7) and adds a line feed per for (Nx8).
* `4(1!:2)~` prints each row.
[Answer]
## Haskell, 159 characters
```
v(_:x:y:z:h:m:s:t:r)=(w%x+1)&":"$z%y&":"$1%z&"\n"$v$t:r where w=y*z;(%)=mod.div(t+h*w-w+m*z+s)
v _=""
d&c=tail.(shows(d+100)c++)
main=interact$v.map read.words
```
---
* Edit: (207 -> 200) sometimes `divMod` isn't worth it!
* Edit: (200 -> 178) succumbed to not using the elegant `foldr` approach (which works for time systems with any number of components!)
* Edit: (178 -> 164) inlined `f`
* Edit: (164 -> 158) removed unnecessary parentheses
* Edit: (158 -> 160) fixed a bit introduced three edits ago: hours are now correct again
* Edit: (160 -> 159) dropped a call to `tail`
[Answer]
## Ruby, 128 chars
Shamelessly copies from the python one:
```
d=$<.read.split.map(&:to_i);d[0].times{|o|x,y,z,h,m,s,t=d[o*7+1,7];t+=z*(y*h+m)+s;puts ["%02d"]*3*':'%[(t/y/z-1)%x+1,t/z%y,t%z]}
```
[Answer]
## Haskell - 219 necessary characters
```
import Text.Printf
(#)=div
(%)=mod
n?d=(n-1)%d+1
e a n=mapM(\_->a)[1..n]
main=readLn>>=(e$do{
 a<-e getLine 3;
 let[x,y,z,h,m,s,t]=map read.words=<<a;
    w=y*z;e=h*w+m*z+s+t::Int
  in printf"%02d:%02d:%02d\n"(e#w?x)(e#z%y)(e%z)})
```
[Answer]
## PHP (241 chars)
Takes input from a file passed as an argument.
```
foreach(array_chunk(array_slice(file($argv[1]),1),3)as$v){list($x,$y,$z)=split(" ",$v[0]);list($h,$m,$s)=split(" ",$v[1]);$e=($v[2]+$s+$z*($m+$h*$y))%($x*$y*$z);$s=$e%$z;$e/=$z;$m=$e%$y;$h=($e/$y)%$x;printf("%02d:%02d:%02d\n",$h?:$x,$m,$s);}
```
And ungolfed:
```
$input = array_chunk(array_slice(file($argv[1]),1),3);
foreach($input as $v){
    list($x,$y,$z)=split(" ",$v[0]);
    list($h,$m,$s)=split(" ",$v[1]);
    $t = $v[2];
    $seconds_in_day = $x * $y * $z;
    $total_elapsed = $t + $s + $m*$z + $h*$y*$z;
    $elapsed = $total_elapsed % $seconds_in_day;
    $sec = $elapsed % $z;
    $elapsed /= $z;
    $min = $elapsed % $y;
    $elapsed /= $y;
    $hours = $elapsed % $x;
    if ($hours == 0) $hours = $x;
    printf("%02d:%02d:%02d\n",$hours,$min,$sec);
}
```
And just to note, without sigils (the dollar sign), this comes out to 205 characters.
[Answer]
## Java, ~~486~~ 371 characters
Ungolfed version: <http://pastebin.com/6LiTdGyi>
This gives the same output as in the provided examples.
But I disagree on that behavior: a clock does not have as many numbers as there are hours in a day: it has half of them.
Meaning that if you add 3600 seconds to 12:50:12, it should print 01:50:12, not 13:50:12 (in our standard 24/60/60 system).
I handled that in my code but commented it out in my solution for it to match the examples.
Of course if you consider this, then the input times could be considered ambiguous unless you add some AM/PM marker.
But in any case, the puzzle has an inconsistency: if 00 hours should be replaced by x, then hours > (x/2) should be replaced by hours - (x/2).
Edit:
Golfed version:
```
import java.io.File;import java.util.Scanner;public class U{static int i(Scanner s){return
s.nextInt();}public static void main(String[]g)throws Exception{Scanner s=new Scanner(new File(g[0
]));int n=i(s);while(0!=n--){int J=i(s),K=i(s),L=i(s),P=(i(s)*K*L+i(s)*L+i(s)+i(s))%(J*K*L);System.
out.println(String.format("%02d:%02d:%02d",(0==P/L/K%J)?J:P/L/K%J,P/L%K,P%L));}}}
```
[Answer]
## Bash - 189 characters:
```
read n
for((i=0;i<n;i++));do
read x y z
read h m s
read t
R=$(((s+m*z+h*y*z+t)%(x*y*z)))
H=$((R/y/z))
R=$((R-H*y*z))
M=$((R/z))
printf"%02d:%02d:%02d\n"$((((H-1)%x+x)%x+1))$M$((R-M*z))
done
```
[Answer]
## PHP, ~~229~~ 228 characters
```
<?$v=file($argv[1]);while(++$i<$v[0]*3){list($x,$y,$z)=split(" ",$v[$i++]);list($h,$m,$s)=split(" ",$v[$i++]);$s=($e=($v[$i]+$s+$m*$z+$h*$y*$z)%($x*$y*$z))%$z;$m=($e/=$z)%$y;printf("%02d:%02d:%02d\n",($e/$y)%$x?$e%$x:$x,$m,$s);}
```
File must be passed into the script as an argument
**Ungolfed:**
```
<?php
$v = file($argv[1]); // Automatically break the file into an array by line
while(++$i < $v[0]*3){ // Loop for every three lines
  list($x, $y, $z) = explode(" ", $v[$i++]); // Break apart the first line by space
  list($h, $m, $s) = explode(" ", $v[$i++]); // Break apart the second line
  /*
    Add the starting time to the total number of seconds that have passed
    Divide by total amount of seconds in a day
  */
  $time = ($v[$i] + $s + $m * $z + $h * $y * $z) % ($x * $y * $z);
  $seconds = $time % $z;  // Get the number of seconds
  $minutes = ($time /= $z) % $y; // Remove the end amount of seconds, then get the minutes
  /*
    Remove the end amount of hours
    Determine how many hours there would be
    If the number is zero, then output the max hours
    If the number is not zero, output the amount of hours left
  */
  $hours = ($time / $y) % $x? $e % $x : $x;
  // Display the time in the correct format
  printf("%02d:%02d:%02d\n", $hours, $minutes, $seconds);
}
```
**Changelog:**
229 -> 228: No need to set time remaining while performing division on the hours
[Answer]
## Bash, 139 characters
```
read n
while((n--));do
read x y z;read h m s;read t
((t+=z*(y*h+m)+s,a=(t/y/z-1)%x+1,b=t/z%y,c=t%z))
printf %02d:%02d:%02d\\n $a $b $c
done
```
[Answer]
## Scala 184 chars:
```
object C extends App{val r=new java.util.Scanner(System.in)
def n=r.nextInt
for(j<-1 to n;h=n;m=n;s=n;x=n;y=n;z=n;t=n;d=(x*m+y)*s+z+t){printf("%02d:%02d:%02d\n",d/(m*s)%h,d/s%m,d%s)}
}
```
In conflict to the rules, I claim, that for
```
14 15 20
1 14 0
-580
```
The output shouldn't be
```
14:00:00
```
but
```
00:00:00
```
and that's what my code produces. Please show me a clock which displays 24:00:00 on earth instead of 00:00:00 - maybe 24:59:59. Or do you expect the sequence:
```
23:59:59
24:00:00
00:00:01
```
instead of
```
23:59:59
00:00:00
00:00:01
```
[Answer]
# [Python 2](https://docs.python.org/2/), 137 bytes
```
lambda T:["%02d:%02d:%02d"%((s/z/y%x,x)[s%x<1],s/z%y,s%z)for x,y,z,h,m,s,t in[T[i:i+7]for i in range(1,len(T),7)]for s in[s+m*z+h*y*z+t]]
```
[Try it online!](https://tio.run/##dVHbboMwDH0eX2ExIZHWVZNwa6PxF31jPHQqrEgtrQiTgJ9nNtBKm1QUJ8e3c0xy79vzrdZjmX6Ol@P163SEg8lcT@qTeW6u5/t2O2x7r8NOZNbrPlSOFPF6tN4gylsDHfY44BmvaLGFqs4OWWWqdZJzsqIANMf6u/AVXoraPwhMxJSyXGvX19WwPq962ts8H9vCthZS8B2gz4dMI0QIWiIotvngSBwECDGBaPIJUYJCei9zhMyVoVGBkTsXwZWagJGBm4PABzNzhWQJGWNSUvEiEwULSUJdRso/jVS4QwjlrB3MU0XcGU9zhrOrl3nDabJNtFsoI6NjoyTPpULi/k/P/0A9sZztuTbqRUK@iKtJTwcm2tNiPf3Q4zuRE1ZuLhzhOPwifPkITWF/LvyQk2@N83ZvqrqF0mdfvOMDpelcOv4C "Python 2 – Try It Online")
Only slightly shorter than the [other Python answer](https://codegolf.stackexchange.com/a/1703/73368), but takes a different route to get there.
**Ungolfed Explanation:**
```
def f(T):
    # ignore first list element, split list into even chunks of length 7
    for i in range(1, len(T), 7):
        # get variables for sublist
        for x, y, z, h, m, s, t in [T[i:i + 7]]:
            # get total time in seconds, inside a list so that we can use list comprehension
            for s in [s + m*z + h*y*z + t]:
                # split total time into parts
                # seconds: convert seconds to minute, take remainder
                sec = s % z
                # minutes: convert seconds to minutes (discard remainder), convert minutes to hours, take remainder
                min = s / z % y
                # hours: convert seconds to minutes (discard remainder),
                #        convert minutes to hours (discard remainder),
                #        convert hours to days, take remainder
                # if seconds are evenly divisible by total hours, use number of hours in day instead ("midnight")
                hr = (s / z / y % x, x)[s % x < 1]
                print "%02d:%02d:%02d"%(hr, min, sec)
```
[Answer]
## Haskell (~~815~~ 624 chars non-golfed, blank lines excluded)
~~Mine prints 00:00:00 instead of 12:00:00 or similar for "midnight"-like times.~~ Edit: changed that.
```
main = readFile "in.txt" >> mapM_ print . times . map (map read . words) . tail . lines
times [] = []
times ([x,y,z]:[h,m,s]:[t]:xs) = Time x y z h m s +++ t : times xs
data Time = Time {x,y,z,h,m,s :: Int}
hr t | h t == 0 = x t | otherwise = h t
instance Show Time where show t = pad2 (hr t) ++ ':':pad2 (m t) ++ ':':pad2 (s t)
pad2 x | x < 10 = '0':show x | otherwise = show x
t +++ ss | ss < 0  = t +++ (ss + x'*y'*z') | otherwise = Time x' y' z' h' m' s'
  where (x',y',z') = (x t, y t, z t)
        (ms, s') = (s t + ss) `quotRem` z'
        (hs, m') = (m t + ms) `quotRem` y'
        (_,  h') = (h t + hs) `quotRem` x'
```
Could've abstracted a few things more, but w/e. It completely ignores the first line of the input file, and generally screams at you for improperly-formatted files.
]