#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

bool isSame(int s, int t, int u, int sec, int min, int hour)
{
    vector<int> a(3), b(3);
    a[0] = s;
    a[1] = t;
    a[2] = u;
    b[0] = sec;
    b[1] = min;
    b[2] = hour;
    sort(a.begin(), a.end());

    for(int i=0; i<3; ++i){
        vector<int> c = b;
        for(int j=0; j<3; ++j){
            c[j] += a[i] - b[0] + 60;
            c[j] %= 60;
        }
        sort(c.begin(), c.end());
        if(a == c)
            return true;
    }
    return false;
}

int main()
{
    for(;;){
        int n;
        cin >> n;
        if(n == 0)
            return 0;

        vector<vector<int> > s(n, vector<int>(3));
        for(int i=0; i<n; ++i){
            for(int j=0; j<3; ++j){
                cin >> s[i][j];
            }
            sort(s[i].begin(), s[i].end());
        }

        multimap<int, int> mm;
        for(int i=0; i<n; ++i){
            do{
                for(int j=0; j<60; ++j){
                    int hour = (s[i][0] + j) % 60;
                    int min = (s[i][1] + j) % 60;
                    int sec = (s[i][2] + j) % 60;
                    if(hour % 5 != min / 12)
                        continue;
                    int k = (hour / 5 * 60 + min) * 60 + sec;
                    mm.insert(make_pair(k, i));
                }
            }while(next_permutation(s[i].begin(), s[i].end()));
        }

        vector<int> m(n, 0);
        int numZero = n;
        multimap<int, int>::iterator it1, it2;
        it1 = it2 = mm.begin();
        int ret = -1;
        int len = INT_MAX;
        int curr;
        while(it2 != mm.end()){
            while(it2 != mm.end() && numZero != 0){
                curr = it2->first;
                if(m[it2->second] == 0)
                    -- numZero;
                ++ m[it2->second];
                ++ it2;
            }

            while(numZero == 0){
                if(curr - it1->first < len){
                    len = curr - it1->first;
                    ret = it1->first;
                }
                if(it2 == mm.end())
                    break;
                if(m[it1->second] == 1)
                    ++ numZero;
                -- m[it1->second];
                ++ it1;
            }
        }

        for(int i=0; i<2; ++i){
            int sec = (ret + i*len) % 60;
            int min = (ret + i*len) / 60 % 60;
            int hour = (ret + i*len) / (60 * 12);
            ostringstream oss;
            oss << setfill('0');
            oss << setw(2) << (hour/5) << ':' << setw(2) << min << ':' << setw(2) << sec;
            cout << oss.str();
            if(i == 0)
                cout << ' ';
        }
        cout << endl;
    }


    for(;;){
        int n;
        cin >> n;
        if(n == 0)
            return 0;

        vector<int> s(n), t(n), u(n);
        for(int i=0; i<n; ++i)
            cin >> s[i] >> t[i] >> u[i];

        queue<pair<int, int> > q;
        vector<int> m(n, 0);
        int numZero = n;
        int ret = -1;
        int len = INT_MAX;
        for(int i=0; i<12*60*60; ++i){
            int sec = i % 60;
            int min = i / 60 % 60;
            int hour = i / (60 * 12);

            for(int j=0; j<n; ++j){
                if(isSame(s[j], t[j], u[j], sec, min, hour)){
                    if(m[j] == 0)
                        -- numZero;
                    ++ m[j];
                    q.push(make_pair(i, j));
                }
            }

            while(numZero == 0){
                if(i - q.front().first < len){
                    len = i - q.front().first;
                    ret = q.front().first;
                }
                if(m[q.front().second] == 1)
                    ++ numZero;
                -- m[q.front().second];
                q.pop();
            }
        }

        for(int i=0; i<2; ++i){
            int sec = (ret + i*len) % 60;
            int min = (ret + i*len) / 60 % 60;
            int hour = (ret + i*len) / (60 * 12);
            ostringstream oss;
            oss << setfill('0');
            oss << setw(2) << (hour/5) << ':' << setw(2) << min << ':' << setw(2) << sec;
            cout << oss.str();
            if(i == 0)
                cout << ' ';
        }
        cout << endl;
    }
}

Measured memory_bytes: 0