### constant

INF = 1 << 30

### subroutines

def sec2hms(sec)
  sprintf("%02d:%02d:%02d", sec / 3600, (sec / 60) % 60, sec % 60)
end

### main

loop do
  n = gets.to_i
  break if n == 0

  clks = []
  for k in (0...n)
    si, ti, ui = gets.split.map(&:to_i)
    ci = []
    for i in (0...60)
      [si, ti, ui].permutation do |pi|
        if pi[0] % 5 == pi[1] / 12
          ci << (pi[0] / 5) * 3600 + pi[1] * 60 + pi[2]
        end
      end

      si = (si + 1) % 60
      ti = (ti + 1) % 60
      ui = (ui + 1) % 60
    end
    clks += ci.uniq.sort.map{|c| [c, k]}
  end
  clks.sort!
  #p clks.length

  allbits = (1 << n) - 1
  bits = 0
  subclks = []

  min_sec = INF
  sec0 = sec1 = nil
  
  clks.each do |ci, k|
    b = (1 << k)

    if (bits & b) != 0
      subclks.delete_if{|sci| sci[1] == k}
    else
      bits |= b
    end
    subclks << [ci, k]
    
    if bits == allbits
      sec = ci - subclks.first[0]
      if min_sec > sec
        min_sec = sec
        sec0 = subclks.first[0]
        sec1 = ci
      end
    end
  end

  puts [sec2hms(sec0), sec2hms(sec1)].join(' ')
end