//  Example 10.6, page 232
//  Schaum's Outline of Fundamental of Computing with C++
//  by John R. Hubbard
//  Copyright McGraw-Hill, 1998

#include <cassert>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <string>

using namespace std;

typedef list<string> SL;             // string list type
typedef list<string>::iterator SLI;  // string list iterator type

string extract_word(SLI& it) {
    string element = *it;
    int k = element.find('\t');
    return element.substr(0, k);
}

bool found(SL& x, SLI& it, string& new_word) {
    for (it=x.begin(); it != x.end(); ++it) {
        string word = extract_word(it);
        if (word == new_word) return true;
        if (word > new_word) return false;
    }
}

string literal(int n) {
    assert(n >= 0 && n <= 99);
    string s; //(2);
    s += char('0' + n/10);
    s += char('0' + n%10);
    return s;
}

void print(SL& x) {
    int i=1;
    for (SLI it=x.begin(); it != x.end(); ++it, ++i)
        cout << setw(4) << i << ". " << *it << endl;
}

// frequ returns the number of times word appeared in the input
int freq(string& word) {
    int count = 1;
    const char* cword = word.c_str();
    for(; *cword; ++cword)
        if(*cword==',') ++count;
    return count;
}

int main() {
    SL x;
    SLI it = x.begin();
    string new_word;
    int count=0;
    while (cin >> new_word) {
        ++count;
        if (found(x, it, new_word))
            *it += ", " + literal(count);
        else {
            new_word += "\t" + literal(count);
            x.insert(it, new_word); // + "\t" + literal(count));
        }
    }
    print(x);
    for(it = x.begin(); it != x.end(); ++it) {
        new_word = extract_word(it);
        cout << endl << new_word << " appears " << freq(*it) << " times.";
    }
}
