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 <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
  int n,id,m1,s1,m2,s2,m3,s3,m4,s4,sum;
  vector<pair<int,int>> v;
  pair<int,int> p;

  while(1){
    cin>>n;

    if(n == 0){
      break;
    }

    for(int i = 0;i<n;++i){
      cin>>id>>m1>>s1>>m2>>s2>>m3>>s3>>m4>>s4;
      sum = (m1+m2+m3+m4)*60+(s1+s2+s3+s4);
      v.push_back(make_pair(sum,id));
    }

    sort(v.begin(),v.end());

    p = v[0];

    cout<<p.second<<endl;

    p = v[1];

    cout<<p.second<<endl;

    p = v[n-2];

    cout<<p.second<<endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define all(c) (c).begin(),(c).end()
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define rrep(i,n) for(int i=(int)(n)-1; i>=0; i--)
#define REP(i,m,n) for(int i=(int)(m); i<(int)(n); i++)
#define iter(c) __typeof((c).begin())
#define tr(it,c) for(iter(c) it=(c).begin(); it!=(c).end(); it++)
#define pb(a) push_back(a)
#define pr(a) cout << (a) << endl
#define PR(a,b) cout << (a) << " " << (b) << endl
#define F first
#define S second
typedef long long ll;
typedef pair<int,string> P;
const int MAX=1000000001;
const ll MAXL=1000000000000000001LL;
const ll mod=1000000007;
int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};

int main() {
  int n;
  while(cin >> n && n) {
    P a[n];
    fill(a,a+n,P(0,""));
    rep(i,n) {
      cin >> a[i].S;
      rep(j,8) {
	int x;
	cin >> x;
	if(j%2) a[i].F+=x;
	else a[i].F+=x*60;
      }
    }
    sort(a,a+n);
    pr(a[0].S);
    pr(a[1].S);
    pr(a[n-2].S);
  }
  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;
typedef pair<int, int> P;

int main(void) {
  int n;
  while (cin >> n, n) {
    vector<P> v(n, P(0, 0));
    int id, m, s;    
    for (int i = 0; i < n; i++) {
      cin >> id;
      v[i].second = id;
      for (int j = 0; j < 4; j++) {
        cin >> m >> s;
        v[i].first += m * 60 + s;
      }
    }
    
    sort(v.begin(), v.end());

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

  return 0;
}

C++: #include <cstdio>
#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
using namespace std;

int main(){
	int n,a[100000],b[100000],m,s;
	
	while(1){
		cin >> n  ;
		if(n==0){
			return 0;
		}
		for(int i=0;i<n;i++){
			cin >> a[i] ;
			b[i]=0;
			for(int j=0;j<4;j++){
				cin >> m >>s ;
				b[i]+=60*m+s;
			}
		}
		for(int i=1;i<n;i++){
			for(int j=0;j<n-i;j++){
				if(b[j]<b[j+1]){
					m=b[i]; b[i]=b[i+1]; b[i+1]=m;
					s=a[i]; a[i]=a[i+1]; a[i+1]=s;
				}
			}
		}
		
		cout << a[0] << "\n" << a[1] << "\n" << a[n-2] << "\n" ;
	}
	
return 0;
}

C++: #include <stdio.h>
#include <stack>
#include <math.h>

using namespace std;

struct Info{
	int id,sum;
};

int main(){

   int n,m1,m2,m3,m4,s1,s2,s3,s4,tmp_id,tmp_sum;
   Info table[4];

   while(true){
	   scanf("%d",&n);
	   if(n == 0)break;

	   for(int i=0; i < 4; i++){
		     scanf("%d %d %d %d %d %d %d %d %d",&table[i].id,&m1,&s1,&m2,&s2,&m3,&s3,&m4,&s4);
		     table[i].sum = (60*(m1+m2+m3+m4)+(s1+s2+s3+s4));
	   }

	   for(int i = 1; i <= 3; i++){
		   for(int k=3; k >= 1; k--){
			   if(table[k].sum < table[k-1].sum)swap(table[k],table[k-1]);
		   }
	   }
	   n -= 4;

	   for(int i=0; i < n; i++){
		   scanf("%d %d %d %d %d %d %d %d %d",&tmp_id,&m1,&s1,&m2,&s2,&m3,&s3,&m4,&s4);
		   tmp_sum = (60*(m1+m2+m3+m4)+(s1+s2+s3+s4));

		   if(tmp_sum > table[3].sum){ //??????????°?

			   table[2].id = table[3].id; //?????????????????????
			   table[2].sum = table[3].sum;
			   table[3].id = tmp_id;
			   table[3].sum = tmp_sum;

		   }else if(tmp_sum > table[2].sum){ //tmp???????????????
			   table[2].id = tmp_id;
			   table[2].sum = tmp_sum;

		   }else if(tmp_sum < table[0].sum){

				   table[1].id = table[0].id;
				   table[1].sum = table[0].sum;
				   table[0].id = tmp_id;
				   table[0].sum = tmp_sum;

		   }else if(tmp_sum < table[1].sum){

			   table[2].id = tmp_id;
			   table[2].sum = tmp_sum;
		   }

	   }

	   printf("%d\n",table[0].id);
	   printf("%d\n",table[1].id);
	   printf("%d\n",table[2].id);
   }
    return 0;
}

C++: //01/12/miya
#include<iostream>
#include<algorithm>
using namespace std;

int main(){
while(true){
	int n;
	cin >> n;

	if(n==0)break;

	int c[1000000],t[1000000];
	for(int i=0;i<n;i++){		
		int m1,s1,m2,s2,m3,s3,m4,s4;
		cin >> c[i];
		cin >> m1 >> s1 >> m2 >> s2 >> m3 >> s3 >> m4 >> s4;
		t[i] = m1*60+s1+m2*60+s2+m3*60+s3+m4*60+s4;
	}
	for(int i=0;i<n;i++){
		for (int j=n-1;j>i;j--){
			if(t[j]<t[j-1]){
				swap(t[j],t[j-1]);
				swap(c[j],c[j-1]);
			}
		}
	}
	cout << c[0] << endl << c[1] << endl << c[n-2] << endl;
}
return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
  
// #define int long long
#define pb push_back
#define REP(i, n) for (int i = 0; i < (n); i++)

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vector<int> > vii;

bool is_uruu(int y) {return (y % 4 == 0 && (y % 100 != 0 || y % 400 == 0));}
template <class X> void print(X Target){ cout << Target << '\n'; }

struct PRO{
  int num;
  int t;
};

bool comp(PRO lhs, PRO rhs)
{
  return (lhs.t < rhs.t);
}

signed main()
{
  ios::sync_with_stdio(false);
  int n;

  while (cin >> n, n){
    vector<PRO> rank;
    REP(i, n){
      int c, m1, s1, m2, s2, m3, s3, m4, s4;
      cin >> c >> m1 >> s1 >> m2 >> s2 >> m3 >> s3 >> m4 >> s4;
      rank.pb((PRO){c, (m1 + m2 + m3 + m4) * 60 + s1 + s2 + s3 + s4});
    }
    sort(rank.begin(), rank.end(), comp);
    print(rank[0].num);
    print(rank[1].num);
    print(rank[rank.size() - 2].num);
  }

  return (0);
}

C++: #include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    while(cin>>n,n){
        string str[4];
        int num[4]={1000000,1000000,0,0};
        for(int i=0,sum;i<n;i++){
            string st;
            cin>>st;
            sum=0;
            for(int j=0,minute,second;j<4;j++){
                cin>>minute>>second;
                sum+=60*minute+second;
            }
            if(sum<num[1]){
                num[1]=sum;
                str[1]=st;
                if(num[1]<num[0]){
                    swap(num[0],num[1]);
                    swap(str[0],str[1]);
                }
            }
            if(sum>num[2]){
                num[2]=sum;
                str[2]=st;
                if(num[2]>num[3]){
                    swap(num[2],num[3]);
                    swap(str[2],str[3]);
                }
            }
        }
        for(int i=0;i<3;i++)cout<<str[i]<<endl;
    }
}