Go: package main

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

func main() {
	stdin := bufio.NewScanner(os.Stdin)
	index := make(map[string]string)
	keys := []string{}
	for stdin.Scan() {
		l := strings.Split(stdin.Text(), " ")
		if l[0] == "0" {
			break
		}
		if index[l[0]] == "" {
			keys = append(keys, l[0])
		}
		index[l[0]] += l[1] + " "
	}
	sort.Strings(keys)
	for _, k := range keys {
		fmt.Println(k)
		p := strings.Split(strings.TrimRight(index[k], " "), " ")
		pages := []int{}
		for _, v := range p {
			i, _ := strconv.Atoi(v)
			pages = append(pages, i)
		}
		sort.Ints(pages)
		for i, v := range pages {
			if i > 0 {
				fmt.Printf(" ")
			}
			fmt.Printf("%d", v)
		}
		fmt.Println()
	}
}

C++: #include<iostream>
#include<math.h>
#include<string>
#include<vector>
#include<algorithm>
#include<queue>
#include<map>
using namespace std;
#define INF 1<<20

struct edge{
	int to;
	int cost;
	edge() {};
	edge(int a, int b) {
		to = a;
		cost = b;
	}
};

struct Node {
	int pos;
	int cost;
	Node() {};
	Node(int a, int b) {
		pos = a;
		cost = b;
	}
	bool operator <(const Node& rhs) const {
		return cost > rhs.cost;
	}
};

void dijkistra(int st, int TO,int V,vector<vector<edge> > graph) {
	priority_queue<Node> q;
	vector<int> d(V);
	for (int i = 0;i < V;i++) {
		d[i] = INF;
	}
	d[st] = 0;
	q.push(Node(st, 0));
	while (!q.empty()) {
		Node s = q.top(); q.pop();
		int pos = s.pos;
		for (int i = 0;i < graph[pos].size();i++) {
			int to = graph[pos][i].to;
			int cost = graph[pos][i].cost;
			if (d[pos] + cost < d[to]) {
				d[to] = d[pos] + cost;
				q.push(Node(to, d[to]));
			}

		}
	}
	cout << d[TO] << endl;
}
int partition(int A[],int p, int r) {
	int x = A[r];
	int i = p - 1;
	for (int j = p;j < r;j++) {
		if (x >= A[j]) {
			i = i + 1;
			swap(A[i], A[j]);
		}
	}
		swap(A[i + 1], A[r]);
		return i + 1;
}
void quicksort(int A[], int p, int r) {
	if (p < r) {
		int q = partition(A, p, r);
		quicksort(A, p, q-1);
		quicksort(A, q + 1, r);
	}
}

int getNumberOfCoin(int C[],int n,int m) {
	int T[50000];
	for (int j = 0;j <= n;j++) {
		T[j] = INF;
	}
	T[0] = 0;
	for (int i = 0;i < m;i++) {
		for (int j = 0;j <= n;j++) {
			if (j - C[i] >=0  ) {
				T[j] = min(T[j], T[j - C[i]] + 1);
			}
		}
	}
	return T[n];
}
int knapsack(int w, int n,int* v,int* ww) {
	int W[101][10001];
		for (int j = 0;j <= w;j++) {
			W[0][j] = 0;
		}
	for (int i = 0;i < n;i++) {
		for (int k = 0;k < n;k++){
			for (int j = 0;j <= w;j++) {
				if (j - ww[k] < 0) {
					W[i + 1][j] = W[i][j];
				}
				if (j - ww[i] >= 0) {
					W[i + 1][j] = max(W[i][j], W[i][j - ww[k]] + v[k]);

				}
			}
		}
	}
	return W[n][w];
}
int fib(int n) {
	vector<int> v(n+1);
	v[0] = 1;
	v[1] = 1;
	for (int i = 2;i < n+1;i++) {
		v[i] = v[i - 2] + v[i - 1];
	}
	return v[n];
}
int main(){
	map<string, vector<int> > dic;
	string voc;
	int page;
	while (cin >> voc >> page) {
		dic[voc].push_back(page);
	}
	map<string, vector<int> >::iterator it = dic.begin();
	while(it!=dic.end()) {
		cout << (*it).first<<endl;
		sort((*it).second.begin(), (*it).second.end());
		for (int i = 0;i < (*it).second.size();i++) {
			if (i = (*it).second.size() - 1) {
				cout << (*it).second[i];
			}
			else cout << (*it).second[i] << " ";
		}
		cout << endl;
		it++;
	}
	
}

int isPrime(int x) {
	if (x <= 1) return false;
	if (x>2 && x % 2 == 0) return false;
	for (int i = 2;i <= sqrt(x);i++) {
		if (x%i == 0) {
			return false;
		}
	}
	if (x == 2) return true;
	return true;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <map>
using namespace std;
typedef long long LL;
static const double EPS = 1e-9;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)

int main(void){
  map<string, set<int> > sakuin;
  string word;
  int page;
  map<string, set<int> >::iterator it;
  set<int>::iterator it2;
  while(cin>>word){
    cin>>page;
    if((it=sakuin.find(word))!=sakuin.end()){
      (*it).second.insert(page);
    }else{
      set<int> tmp;
      tmp.insert(page);
      sakuin[word]=tmp;
    }
  }
  for(it=sakuin.begin();it!=sakuin.end();it++){
    cout<<(*it).first<<endl;
    for(it2=(*it).second.begin();it2!=(*it).second.end();it2++){
      printf("%d ",(*it2));
    }
    cout<<endl;
  }
  return 0;
}

C++: //Name: Book Index
//Level: 2
//Category: やるだけ
//Note:

/**
 * mapを使って語句とページ番号を管理すると、語句は勝手に昇順に並ぶ。
 * ページ番号は出力時にソートする。
 *
 * オーダーは O(N|S| log N)。
 * ただしNは語句の個数、|S|は語句の最大長。
 */
#include <iostream>
#include <string>
#include <vector>
#include <map>

using namespace std;

bool solve() {
    string word;
    int page;
    map<string,vector<int>> dict;
    while(cin >> word >> page) {
        dict[word].push_back(page);
    }
    for(auto &e : dict) {
        cout << e.first << endl;
        sort(begin(e.second), end(e.second));
        for(int i = 0; i < (int)e.second.size(); ++i) {
            if(i > 0) cout << ' ';
            cout << e.second[i];
        }
        cout << endl;
    }
    return false;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);

    while(solve()) ;
    return 0;
}

C++: #include<iostream>
#include<string>

using namespace std;

int main()
{
	string str[128];
	string ans[128];
	int page[128];
	int i,j,k;
	char aa[64];
	
	cin >> str[0] ;
	for(i=0;cin >> page[i] ;i++)
	{
		cin >> str[i+1] ;
	}
	for(j=0;j<i;j++)
	{
		for(k=j+1;k<i;k++)
		{
			if(str[j]>str[k])
			{
				string a;
				int p;
				a=str[j];	str[j]=str[k];	str[k]=a;
				p=page[j];	page[j]=page[k];	page[k]=p;
			}
		}
	}
	for(j=0;j<i;j++)
	{
		for(k=j+1;k<i;k++)
		{
			if(str[j]==str[k])
			{
				if(page[j]>page[k])
				{
					int a;
					a=page[j];
					page[j]=page[k];
					page[k]=a;
//					cout << "page replaced" << endl;
//					cout << "word:" << str[j] << " page1:" << page[j] << " page2:" << page[k] << " j:" << j << " k:" << k << endl;
				}
			}
		}
	}
	
	for(j=0;j<i;j++)
	{
		if(str[j]!="zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz")
		{
			sprintf(aa,"%d",page[j]);
			ans[j]+=aa;
			for(k=j+1;k<i;k++)
			{
				if(str[j]==str[k])
				{
					str[k]="zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz";
					sprintf(aa,"%d",page[k]);
					ans[j]+=" ";
					ans[j]+=aa;
				}
			}
		}
	}
	
	for(j=0;j<i;j++)
	{
		if(str[j]!="zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz")
		{
			cout << str[j] << endl;
			cout << ans[j] << endl;
		}
	}
}

C++: #include <iostream>
#include <map>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main (int argc, char *argv[]) {
	map<string,vector<int> > dict;
	string word;
	int page_num;
	while (cin >> word >> page_num) {
		// New
		if (dict.count(word) == 0) {
			dict.insert(make_pair(word,vector<int>(1,page_num)));
		}
		// Add
		else {
			dict[word].push_back(page_num);
		}
	}
	map<string,vector<int> >::iterator it = dict.begin();
	while (it != dict.end()) {
		cout << it->first << endl;
		vector<int> sorted = it->second;
		sort(sorted.begin(), sorted.end());
		for(int i = 0; i < sorted.size(); ++i) {
			cout << sorted[i] << " ";
		}
		cout << endl;
		it++;
	}
	return 0;
}