#include <algorithm>
#include <codecvt>
#include <fstream>
#include <iostream>
#include <locale>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>
 
using namespace std;
 
int main(int argc, const char* argv[]) try {
	locale ru(locale(argc>3 ? argv[3] : ""), new std::codecvt_utf8<wchar_t>);
	ios_base::sync_with_stdio(false);
 
	wifstream inf;
	if (argc>1) inf.open(argv[1]), wcin.rdbuf(inf.rdbuf());
	wcin.imbue(ru);
 
	wofstream outf;
	if (argc>2) outf.open(argv[2]), wcout.rdbuf(outf.rdbuf());
	wcout.imbue(ru);
 
	unordered_map<wstring, int> dict;
	for(wstring s; wcin >> s;) {
		transform(begin(s),end(s), begin(s),
			[&ru](auto c) { return tolower(c, ru);});
		s.erase(remove_if(begin(s),end(s),
			[&ru](auto c) { return !isalpha(c, ru);}),
			s.end());
		if (!s.empty()) --dict[s];
	}
 
	vector<pair<wstring,int>> result(begin(dict),end(dict));
	sort(begin(result),end(result),
		[](auto& a, auto& b) {
			return tie(a.second,a.first) < tie(b.second,b.first);
		});
 
	for(auto& i : result) wcout << -i.second << " " << i.first << "\n";
	return 0;
} catch(exception& e) { cout << e.what() << endl; }
