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>
using namespace std;

struct Team {
    int num;
    int time;
};

int main() {
    while (true) {
        int n;
        cin >> n;
        if (n == 0) {
            break;
        }
        Team team[] = {
            {0, 100000, },
            {0, 100000, },
            {0, 0, },
            {0, 0, },
        };
        for (int i = 0; i < n; i++) {
            int c, m[4], s[4];
            cin >> c;
            for (int j = 0; j < 4; j++) {
                cin >> m[j] >> s[j];
            }
            int time = 0;
            for (int j = 0; j < 4; j++) {
                time += m[j] * 60 + s[j];
            }
            if (time < team[0].time) {
                team[1].num = team[0].num;
                team[1].time = team[0].time;
                team[0].num = c;
                team[0].time = time;
            } else if (time < team[1].time) {
                team[1].num = c;
                team[1].time = time;
            }
            if (team[3].time < time) {
                team[2].num = team[3].num;
                team[2].time = team[3].time;
                team[3].num = c;
                team[3].time = time;
            } else if (team[2].time < time) {
                team[2].num = c;
                team[2].time = time;
            }
        }
        for (int i = 0; i < 3; i++) {
            cout << team[i].num << endl;
        }
    }
}

C++: #include <cstdio>
#include <cctype>
#include <utility>
#include <vector>
#include <algorithm>
using namespace std;

const auto gcu = getchar_unlocked;
const auto pcu = putchar_unlocked;
#define _T template <typename T>
#define _HT template <typename H, typename... T>
#define _il inline
#define _in _il int in
#define _sc _il bool scan
_T T in(int c){T n=0;bool m=false;while(isspace(c)){c=gcu();}if(c=='-')m=true,c=gcu();
	do{n=10*n+(c-'0'),c=gcu();}while(c>='0'&&c<='9');return m?-n:n;}
_in() {return in<int>(gcu());}
_T T scan(T &n){int c=gcu();return c==EOF?false:(n=in<T>(c),true);}
_sc(char &c){c=gcu();gcu();return c!=EOF;}
#ifdef _GLIBCXX_STRING
_sc(string &s){int c;s="";
	for(;;){c=gcu();if(c=='\n'||c==' ')return true;else if(c==EOF)return false;s+=c;}}
#endif
_HT _sc(H &h, T&&... t){return scan(h)&&scan(t...);}
#define _vo _il void out
#define _vl _il void outl
_vo(bool b) {pcu('0'+b);}
_vo(const char *s){while(*s)pcu(*s++);}
_vo(char c){pcu(c);}
#ifdef _GLIBCXX_STRING
_vo(string s){for(char c:s)pcu(c);}
#endif
_T _vo(T n){static char buf[20];char *p=buf;if(n<0)pcu('-'),n*=-1;
	if(!n)*p++='0';else while(n)*p++=n%10+'0',n/=10; while (p!=buf)pcu(*--p);}
_vl(){out('\n');}
#ifdef _GLIBCXX_VECTOR
_T _vo(vector<T> &v){for(T &x:v)out(&x == &v[0]?"":" "),out(x);outl();}
#endif
_HT _vo(H&& h, T&&... t){out(h);out(move(t)...);}
template <typename... T> _vl(T&&... t){out(move(t)...);outl();}

struct T {
	int i, s;
	bool operator < (const T a) const { return s < a.s; }
};

int main() {
	for (int n; (n = in());) {
		vector<T> l(n);
		for (T &t:l) {
			t.i = in();
			for (int i = 0; i < 4; i++) {
				t.s += in() * 60;
				t.s += in();
			}
		}
		partial_sort(l.begin(), l.begin() + 2, l.end());
		outl(l[0].i);
		outl(l[1].i);
		nth_element(l.begin(), l.begin() + n - 2, l.end());
		outl(l[n - 2].i);
	}
}

C++: #include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main(){

	int n;
	while(cin>>n&&n!=0){
		vector<pair<int,int> > ts;
		for(int i = 0; i < n; i++){
			int team;
			int m[4];
			int s[4];
			cin>>team>>m[0]>>s[0]>>m[1]>>s[1]>>m[2]>>s[2]>>m[3]>>s[3];
			int time=0;
			for(int i = 0; i < 4; i++){
				time+=m[i]*60;
				time+=s[i];
			}
			ts.push_back(make_pair(time,team));
		}
		sort(ts.begin(),ts.end());
		cout<<ts[0].second<<endl;
		cout<<ts[1].second<<endl;
		cout<<ts[ts.size()-2].second<<endl;

	}

	return 0;
}

C++: #include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int a, b, n;
pair<int, int>x[8000000];
int main() {
	while (true) {
		memset(x, 0, sizeof(x));
		cin >> n;
		if (n == 0) { break; }
		for (int i = 0; i < n; i++) {
			cin >> x[i].second;
			for (int j = 0; j < 4; j++) {
				cin >> a >> b;
				x[i].first += a * 60 + b;
			}
		}
		sort(x, x + n);
		cout << x[0].second << endl << x[1].second << endl << x[n - 2].second << endl;
	}
}

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[1].id = tmp_id;
			   table[1].sum = tmp_sum;
		   }

	   }

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

C++: #include<stdio.h>
int main(void){
   int n, id, m1, s1, m2, s2, m3, s3, m4, s4, i, key, keyid, S, fst, snd, last, fstid, sndid, lastid;
   while (scanf("%d", &n) != 0 ){
      for ( i = 0; i < n; i++ ) {
         scanf("%d %d %d %d %d %d %d",&id, &m1, &s1, &m2, &s2, &m3, &s3);
         fst = fstid = snd = sndid = 0;
         last = lastid = 100000;
         S = ( m1 + m2 + m3 ) * 60 + s1 + s2 + s3;
         if (fst < S) {
            key = fst;
            keyid = id;
            fst = S;
            fstid = id;
            snd = key;
            sndid = keyid;
         } else if (snd < S) {
            snd = S;
            sndid = id;
         } else if (last > S) {
            last = S;
            lastid = id;
         }
      }
      printf("%d\n%d\n%d\n", fstid, sndid, lastid);
   }
   return 0;
}