Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"strings"
	"sort"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)

type Team struct {
	Id int
	Time int
}

type ByTime []Team
func (a ByTime) Len() int {
	return len(a)
}
func (a ByTime) Swap(i, j int) {
	a[i], a[j] = a[j], a[i]
}
func (a ByTime) Less(i, j int) bool {
	return a[i].Time < a[j].Time
}

func main() {
	for scanner.Scan() {
		n, _ := strconv.Atoi(scanner.Text())
		if n == 0 {
			break
		}
		teams := make([]Team, n)
		for i := 0; i < n; i++ {
			scanner.Scan()
			buf := strings.Split(scanner.Text(), " ")
			id, _ := strconv.Atoi(buf[0])
			m1, _ := strconv.Atoi(buf[1])
			s1, _ := strconv.Atoi(buf[2])
			m2, _ := strconv.Atoi(buf[3])
			s2, _ := strconv.Atoi(buf[4])
			m3, _ := strconv.Atoi(buf[5])
			s3, _ := strconv.Atoi(buf[6])
			m4, _ := strconv.Atoi(buf[7])
			s4, _ := strconv.Atoi(buf[8])
			team := Team{id, m1*60+s1+m2*60+s2+m3*60+s3+m4*60+s4}
			teams[i] = team
		}
		sort.Sort(ByTime(teams))
		fmt.Println(teams[0].Id)
		fmt.Println(teams[1].Id)
		fmt.Println(teams[len(teams)-2].Id)
	}
}

C++: #include <stdio.h>
#include <string>
#include <algorithm>

using namespace std;

struct team {
    int number;
    int time;
};

typedef struct team Team;
bool tfsort (const team &a, const team &b);

int main(){
    int i, n,time, times, second, a, b, c, d, e, f, g, h;
    Team rec[10];
    int number[1000001],team[1000001];
    
    while(scanf("%d", &n), n){
        for(i = 0; i < n; i++){
            scanf("%d %d %d %d %d %d %d %d %d", &number[i], &a, &b, &c, &d, &e, &f, &g, &h);
            times = (b+d+f+h);
            time = times / 60;
            second = times - time * 60;
            team[i] = (a+c+e+g+time)*100 + second;
            rec[i] = (Team){number[i], team[i]};
        }
        sort(rec, rec + n, tfsort);
        
        for(i = 0; i < 2; i++){
            printf("%d\n", rec[i].number);
        }
        printf("%d\n", rec[n -2].number);
    }
    return 0;
}

bool tfsort (const team &a, const team &b){
    if(a.time < b.time) return true;
    return false;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;

struct hoge{
	int num, timem, times;
};

int main(){
	int n, c, m1, s1, m2, s2, m3, s3, m4, s4;
	hoge set[100];
	while(1){
		cin >>n;
		if(n == 0) break;
		for(int i = 0; i < n; i++){
			cin >>c >>m1 >>s1 >>m2 >>s2 >>m3 >>s3 >>m4 >>s4;
			set[i].num = c;
			set[i].timem = m1 + m2 + m3 + m4;
			set[i].times = s1 + s2 + s3 + s4;
			if(set[i].times >= 60){
				set[i].timem += set[i].times / 60;
				set[i].times %= 60;
			}
		}
		
		for(int i = 0; i < (n - 1); i++){
			for(int j = (i + 1); j < n; j++){
				if(set[j].timem < set[i].timem){
					swap(set[i].num, set[j].num);
					swap(set[i].timem, set[j].timem);
					swap(set[i].times, set[j].times);
				} else if(set[j].timem == set[i].timem){
					if(set[j].times < set[i].times){
						swap(set[i].num, set[j].num);
						swap(set[i].timem, set[j].timem);
						swap(set[i].times, set[j].times);
					}
				}
			}
		}
		
		cout <<set[0].num <<endl <<set[1].num <<endl <<set[n - 2].num <<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
using namespace std;

int main(int argc, const char * argv[])
{
    while (1) {
        int n; cin >> n;
        if (n == 0) break;
        int result[2][n];
        for (int i = 0; i < n; i++) {
            int m1, s1, m2, s2, m3, s3, m4, s4;
            cin >> result[0][i] >> m1 >> s1 >> m2 >> s2 >> m3 >> s3 >> m4 >> s4;
            result[1][i] = m1*60 + s1 + m2*60 + s2 + m3*60 + s3 + m4*60 + s4;
        }
        for (int i = 0; i < n-1; i++) {
            for (int j = 0; j < n-1; j++) {
                if (result[1][j] > result[1][j+1]) {
                    int tmp[2];
                    tmp[0] = result[0][j];
                    tmp[1] = result[1][j];
                    result[0][j] = result[0][j+1];
                    result[1][j] = result[1][j+1];
                    result[0][j+1] = tmp[0];
                    result[1][j+1] = tmp[1];
                }
            }
        }
        cout << result[0][0] << endl << result[0][1] << endl << result[0][n-2] << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;

int main(){
	while(true){
		int n;
	       cin >> n;
		if(n==0)
			break;

		int *tn=new int[n];
		int *tt1=new int[n];
		int *tt2=new int[n];

		for(int i=0; i<n; i++){
			tt1[i]=0;	
			tt2[i]=0;		
		}

		for(int i=0; i<n; i++){
			cin >> tn[i];
			for(int j=0; j<4; j++){
				int m,s;
				cin >> m >> s;
				tt1[i]=tt1[i]+60*m+s;
				tt2[i]=tt2[i]+60*m+s;
			}
		}
		
		stable_sort(tt1,tt1+n);

		int a,b,c;

		for(int i=0; i<n;i++){
			if(tt2[i]==tt1[0]){
				a=tn[i];
				break;
			}
		}
		for(int i=0; i<n;i++){
			if(tt2[i]==tt1[1]){
				b=tn[i];
				break;
			}
		}
		for(int i=0; i<n;i++){
			if(tt2[i]==tt1[n-2]){
				c=tn[i];
				break;
			}
		}
		
		cout << a << endl << b << endl << c << endl;

		delete[] tn;
		delete[] tt1;
		delete[] tt2;
	}
		

}

C++: #include <iostream>
using namespace std;

int main(void){
	int i,sum;
	int n,c,m,s;

	while(cin>>n && n){
		int name[4],time[4]={999999999,999999999,-1,-1};

		while(n--){
			cin>>c;
			for(i=sum=0;i<4;i++){
				cin>>m>>s;
				sum += m*60 + s;
			}
			if(time[0]>sum){
				name[1] = name[0];
				time[1] = time[0];
				name[0] = c;
				time[0] = sum;
			}
			else if(time[1]>sum){
				name[1] = c;
				time[1] = sum;
			}

			if(time[3]<sum){
				name[2] = name[3];
				time[2] = time[3];
				name[3] = c;
				time[3] = sum;
			}
			else if(time[2]<sum){
				name[2] = c;
				time[2] = sum;
			}
		}

		for(i=0;i<3;i++) cout<<name[i]<<endl;
	}

	return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<vector>
#include<cmath>
#include<cstdio>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define ite ::iterator
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double ESP=1e-10;
using namespace std;
int main(){
  int n;
  while(cin>>n,n){
    map<int,int>m;
    rep(i,n){
      int a,b,c,d,e,f,g,h,i;
      cin>>a>>b>>c>>d>>e>>f>>g>>h>>i;
      m[(b+d+f+h)*60+c+e+g+i]=a;
    }
    map<int,int>ite it=m.begin();
    cout<<it->second<<endl;
    it++;
    cout<<it->second<<endl;
    it=m.end();
    it--;it--;
    cout<<it->second<<endl;
    //cout<<"!"<<endl;
  }
}

C++: #include <iostream>
#include <algorithm>
using namespace std;

int main(){
  int n;

  while(cin >> n){
    if(n == 0){
      break;
    }

    pair<int,int> team[n];

    for(int i = 0; i < n; i++){
      cin >> team[i].second;
      team[i].first = 0;

      for(int j = 0; j < 4; j++){
        int m, s;
        cin >> m >> s;
        team[i].first += s + m * 60;
      }
    }

    sort(team, team + n);

    cout << team[0].second << endl;
    cout << team[1].second << endl;
    cout << team[n - 2].second << endl;
  }
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
using namespace std;

#define rep(i,n) for(int (i)=0; (i)<(int)(n); ++(i))
#define foreach(c,i) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)

struct Team {
  int id, time;
  bool operator < (const Team& a) const { return time < a.time; }
};

int n;
Team teams[1000002];

void solve() {
  rep (i,n) {
    cin >> teams[i].id;
    teams[i].time = 0;
    int m, s;
    rep (j,4) {
      cin >> m >> s;
      teams[i].time += m * 60 + s;
    }
  }
  sort(teams, teams + n);
  const int ar[3] = { 0, 1, n-2 };
  rep (i,3) cout << teams[ ar[i] ].id << endl;
}

int main() {
  while (cin >> n, n) { 
    solve();
  }
  return 0;
}