A newly opened multinational brand has decided to base their company logo on the three most common characters in the company name. They are now trying out various combinations of company names and logos based on this condition. Given a string $\boldsymbol{\mathrm{~S~}}$, which is the company name in lowercase letters, your task is to find the top three most common characters in the string.

Print the three most common characters along with their occurrence count.
Sort in descending order of occurrence count.  
If the occurrence count is the same, sort the characters in alphabetical order.   

For example, according to the conditions described above, 

$\textbf{G00GLE}$ would have it's logo with the letters $\textbf{G},\textbf{C},\textbf{E}$. 

Input Format

A single line of input containing the string $\mbox{S}$.  

Constraints

$3<len(S)\leq10^4$   
$\mbox{S}$ has at least $3$ distinct characters   

Output Format

Print the three most common characters along with their occurrence count each on a separate line. 

Sort output in descending order of occurrence count. 

If the occurrence count is the same, sort the characters in alphabetical order.  

Sample Input 0
aabbbccde

Sample Output 0
b 3
a 2
c 2

Explanation 0

$\textsf{aabbccde}$

Here, b occurs $3$ times. It is printed first.

Both a and c occur $2$ times. So, a is printed in the second line and c in the third line because a comes before c in the alphabet.

Note: The string $\mbox{S}$ has at least $3$ distinct characters.

Source: hackerrank | Difficulty: MEDIUM


Solutions:
from collections import Counter

def main():
	S = input()
	counts = Counter(S).most_common()
	counts = [(t[1], t[0]) for t in counts]
	counts.sort(key=lambda x: (x[0], -ord(x[1])), reverse=True)
	for i in range(3):
		print(counts[i][1], counts[i][0])
main()


import itertools
import collections

def sortCounter(top):
	if len(top) < 2:
		return top
	(currKey, currVal) = top[0]
	currList = list(itertools.takewhile(lambda x: x[1] == currVal, top))
	currList = sorted(currList, key=lambda x: x[0])
	restOfList = sortCounter(list(itertools.dropwhile(lambda x: x[1] == currVal, top)))
	currList.extend(restOfList)
	return currList
top = sortCounter(collections.Counter(input()).most_common())
for i in range(3):
	(key, val) = top[i]
	print(key, val)


s = input()
alphabet = [0] * 26
for c in s:
	alphabet[ord(c) - ord('a')] += 1
alphabet.append(-1)
first = 26
second = 26
third = 26
for i in range(0, len(alphabet)):
	amount = alphabet[i]
	if amount > alphabet[first]:
		third = second
		second = first
		first = i
	elif amount > alphabet[second]:
		third = second
		second = i
	elif amount > alphabet[third]:
		third = i
print('{:s} {:d}'.format(chr(ord('a') + first), alphabet[first]))
print('{:s} {:d}'.format(chr(ord('a') + second), alphabet[second]))
print('{:s} {:d}'.format(chr(ord('a') + third), alphabet[third]))


inp = input()
from collections import defaultdict
d = defaultdict(int)
for i in inp:
	d[i] += 1
for i in sorted(d.items(), key=lambda x: (x[1] * -1, x[0]))[:3]:
	print('{} {}'.format(i[0], i[1]))


import sys
from collections import Counter
from operator import itemgetter
line = sys.stdin.readline()
data = dict(Counter(line))
newdata = list()
for elem in data:
	newdata.append((elem, data[elem]))
newdata = sorted(newdata, key=itemgetter(0))
newdata = sorted(newdata, key=itemgetter(1), reverse=True)
for i in range(3):
	print('{0} {1}'.format(newdata[i][0], newdata[i][1]))


inStr = input()
d = {}
for i in range(0, len(inStr)):
	c = inStr[i]
	if c in d:
		d[c] += 1
	else:
		d[c] = 1
vals = list(d.items())

def s(x):
	return x[1]

def l(x):
	return x[0]
vals.sort(key=l)
vals.sort(key=s, reverse=True)
for i in range(3):
	print(vals[i][0], vals[i][1])


S = input()
lst = [0 for x in range(26)]
for letter in S:
	lst[ord(letter) - 97] += 1
maxes = [[0, 0], [0, 0], [0, 0]]
for (index, count) in enumerate(lst):
	if count > maxes[0][1]:
		(maxes[2], maxes[1]) = (maxes[1], maxes[0])
		maxes[0] = [index, count]
	elif count > maxes[1][1]:
		maxes[2] = maxes[1]
		maxes[1] = [index, count]
	elif count > maxes[2][1]:
		maxes[2] = [index, count]
for l in maxes:
	print(chr(l[0] + 97) + ' ' + str(l[1]))


s = input()
s_freq = [0] * 26
for e in s:
	index = ord(e) - 97
	s_freq[index] += 1
for i in range(3):
	elem = max(s_freq)
	ind = s_freq.index(elem)
	print('%s %d' % (chr(ind + 97), elem))
	s_freq[ind] = 0


import operator
(s, map) = (input(), {})
for c in s:
	if c in map:
		map[c] += 1
	else:
		map[c] = 1
for _ in range(3):
	max_key = max(sorted(map.items()), key=operator.itemgetter(1))[0]
	print(max_key + ' ' + str(map[max_key]))
	del map[max_key]


s = input()
from collections import Counter
x = Counter(s)
p = sorted(x, key=lambda k: (-x[k], k))
l = 1
for i in p:
	if l > 3:
		break
	print(str(i) + ' ' + str(x[i]))
	l += 1


S = input()
cnt = [[10000, chr(ord('a') + i)] for i in range(ord('z') - ord('a') + 1)]
for s in S:
	cnt[ord(s) - ord('a')][0] -= 1
cnt.sort()
for i in range(3):
	print(cnt[i][1], 10000 - cnt[i][0])


def func():
	st = input()
	hashs = {}
	for ch in st:
		if ch in hashs:
			hashs[ch] = hashs[ch] + 1
		else:
			hashs[ch] = 1
	hashs = sorted(hashs.items(), key=lambda k: (-k[1], k[0]))
	for i in range(3):
		print('%s %d' % (hashs[i][0], hashs[i][1]))
func()


import sys
import math
import time
import random
import functools
import fractions

def keySort(array):
	return array[1] * 1000 - ord(array[0])
s = input()
array = [[chr(ord('a') + i), 0] for i in range(26)]
for i in range(len(s)):
	array[ord(s[i]) - ord('a')][1] += 1
array.sort(key=keySort)
array.reverse()
print(array[0][0], array[0][1])
print(array[1][0], array[1][1])
print(array[2][0], array[2][1])


S1 = str(input())
no = [0] * 27
no_match = 0
for i in range(0, 26):
	no[i] = S1.count(chr(i + 97))
for x in range(3):
	print(chr(no.index(max(no)) + 97), end=' ')
	print(max(no), end='\n')
	no[no.index(max(no))] = 0


arr = []
s = input()
lenn = len(s)
s1 = sorted(s)
arr[:27] = [0] * 27
for i in s1:
	arr[ord(i) - 97] += 1
maxx = -1
pos = -1
for j in range(3):
	for i in range(27):
		if maxx < arr[i]:
			maxx = arr[i]
			pos = i
	print(chr(pos + 97), maxx)
	maxx = -1
	arr[pos] = -2
	pos = -1


from collections import defaultdict
s = input().strip()
letters = defaultdict(int)
for c in s:
	letters[c] += 1
res = []
for (letter, cnt) in letters.items():
	res.append((cnt, letter))
res.sort(key=lambda x: (-x[0], x[1]))
for i in range(3):
	print(res[i][1], res[i][0])


str1 = input()
list1 = {}
for x in str1:
	if x in list1:
		list1[x] += 1
	else:
		list1[x] = 1
n = 3
while n > 0:
	max = 0
	name = ''
	for x in list1:
		if list1[x] > max:
			name = x
			max = list1[x]
		elif list1[x] == max:
			if name > x:
				name = x
	print(name + ' ' + str(max))
	del list1[name]
	n -= 1


from collections import defaultdict
N = 10 ** 6
d = defaultdict(int)
for c in input():
	d[c] += 1
L = []
for x in d:
	L.append([d[x], x])
L.sort(key=lambda x: (N - x[0], x[1]))
for i in range(3):
	print(L[i][1], L[i][0])


from collections import Counter
dic = {}
s = list(input())
s.sort()
c = Counter(s)
for (x, y) in c.most_common(len(s)):
	if dic.get(y):
		dic[y].append(x)
	else:
		dic[y] = [x]
c = 0
for i in sorted(dic.keys(), reverse=True):
	for j in sorted(dic[i]):
		if c < 3:
			print(j, i)
			c += 1


from collections import Counter
from operator import itemgetter
a = []
s = input()
c = Counter(s)
for x in c:
	a.append([c[x], x])
a = sorted(sorted(a, key=itemgetter(1)), key=itemgetter(0), reverse=True)
for i in range(3):
	print(str(a[i][1]) + ' ' + str(a[i][0]))


a = input()
b = {}
for i in set(a):
	b[i] = a.count(i)
c = sorted(set(a))
c = sorted(c, key=lambda x: b[x], reverse=True)
for i in range(3):
	print('{} {}'.format(c[i], b[c[i]]))


from string import ascii_lowercase
s = input().strip()
counts = sorted(list(((-s.count(c), c) for c in ascii_lowercase)))
for i in range(3):
	print(counts[i][1], -counts[i][0])


s = input()
a = {}
b = {}
for i in s:
	if not a.__contains__(i):
		a[i] = 1
	else:
		a[i] = a[i] + 1
for i in a:
	if not b.__contains__(a[i]):
		b[a[i]] = []
		b[a[i]].append(i)
	else:
		b[a[i]].append(i)
c = list(b)
c.sort(reverse=True)
k = 0
for i in range(3):
	a = b[c[i]]
	a.sort()
	for j in a:
		k = k + 1
		if k <= 3:
			print(j, c[i])


phrase = input()
chars = set(phrase)
chars = sorted(list(chars))
chars = sorted(chars, key=lambda x: phrase.count(x), reverse=True)[:3]
for char in chars:
	print(char, phrase.count(char))


from collections import Counter
s = input()
cnt = Counter(s).most_common(26)
s = sorted(cnt, key=lambda x: (-x[1], x[0]))
for (a, b) in s[:3]:
	print(a, b)


from collections import Counter
teststr = input().strip()
ary = [(-y, x) for (x, y) in Counter(teststr).items()]
ary.sort()
for (x, y) in ary[:3]:
	print(y, -x)


from string import ascii_lowercase
stdin = input()
d = {}
arr = []
for letter in ascii_lowercase:
	d[letter] = 0
for letter in stdin:
	d[letter] += 1
for letter in ascii_lowercase:
	arr.append((letter, d[letter]))
arr.sort(reverse=True, key=lambda tup: tup[1])
for i in range(3):
	print(arr[i][0], arr[i][1])


from collections import defaultdict
from operator import itemgetter
d = defaultdict(int)
for c in list(input()):
	d[c] += 1
for k in sorted(d.items(), key=lambda x: (-x[1], ord(x[0])))[:3]:
	print(k[0], k[1])


from collections import Counter
c = sorted(Counter(input()).items(), key=lambda x: x[1] * 1000 - ord(x[0]), reverse=True)[:3]
for x in c:
	print(x[0], x[1])


from collections import Counter
count = 0
for (x, y) in sorted(Counter(input()).most_common(100), key=lambda x: (-x[1], x[0])):
	if count == 3:
		break
	count = count + 1
	print(str(x) + ' ' + str(y))


import sys
d = {}
for c in input():
	if c < 'a' or c > 'z':
		continue
	d[c] = d.get(c, 0) + 1
for (k, v) in sorted(d.items(), key=lambda x: (-x[1], x[0]))[:3]:
	print(k, v)


import operator
text = input()
d = dict()
for c in text:
	try:
		d[c] += 1
	except KeyError:
		d[c] = 1
sorted_d = sorted(d.items(), key=lambda x: x[1], reverse=True)
sorted_d.sort(key=lambda l: (-l[1], l[0]))
l = []
for i in range(3):
	print(sorted_d[i][0], sorted_d[i][1])


from string import ascii_lowercase
s = input()
d = {}
for l in ascii_lowercase:
	d[l] = 0
for c in s:
	d[c] += 1
sorted_list = sorted(sorted(d.items(), key=lambda x: x[0]), key=lambda x: x[1], reverse=True)
for i in range(3):
	print(' '.join(map(str, sorted_list[i])))


a = input().lower()
n = ' '.join((str(ord(c)) for c in a)).split(' ')
s = []
for i in range(len(n)):
	s.append(int(n[i]))
s.sort()
f = [i for i in set(s)]
g = {i: s.count(i) for i in set(s)}
for i in range(3):
	print(chr(max(g, key=g.get)), g[max(g, key=g.get)])
	del g[max(g, key=g.get)]


string = input()
d = {}
li = []
for ch in string.strip():
	if ch in d:
		d[ch] += 1
	else:
		d[ch] = 1
for (k, v) in d.items():
	li.append([k, v])
li.sort(key=lambda k: (k[1], k[0]))
values = [x[1] for x in li]
for x in values:
	if values.count(x) > 1:
		values.remove(x)
newlist = [[[y[0], y[1]] for y in li if y[1] == x] for x in values]
counter = 0
for i in reversed(newlist):
	for j in i:
		if counter < 3:
			print(j[0], j[1])
			counter += 1
		else:
			break


s = input()
d = {}
for i in range(len(s)):
	if s[i] in d:
		d[s[i]] = d[s[i]] + 1
	else:
		d[s[i]] = 1
count = 0
t = []
for x in sorted(d, key=d.get, reverse=True):
	t.append((d[x], x))
t = sorted(t, key=lambda k: (-k[0], k[1]))
for i in range(3):
	print(t[i][1], t[i][0])


from collections import Counter
import operator
chars = Counter(list(input()))
count = 0
for _ in range(3):
	max_key = max(sorted(chars.items()), key=operator.itemgetter(1))[0]
	print(max_key, chars[max_key])
	del chars[max_key]


from collections import Counter
ctr = Counter(input())
ans = sorted(((-x, c) for (c, x) in ctr.items()))[:3]
for (nx, c) in ans:
	print(c, -nx)


from collections import Counter
S = input()
x = Counter(S)
res = []
for i in x:
	res.append([x[i], i])
res = sorted(res, key=lambda cnt: cnt[1])
res = sorted(res, key=lambda cnt: cnt[0], reverse=True)
print(res[0][1], res[0][0])
print(res[1][1], res[1][0])
print(res[2][1], res[2][0])


from collections import Counter
c = Counter(input())
for (i, v) in enumerate(sorted(c, key=lambda k: (c[k], -ord(k)), reverse=True)):
	print(v, c[v])
	if i == 2:
		break


import sys
from collections import defaultdict
line = sys.stdin.readline().strip()
counts = defaultdict(lambda : 0)
for c in line:
	counts[c] += 1
for (k, v) in sorted(counts.items(), key=lambda x: (-x[1], x[0]))[:3]:
	print(k, v)


from collections import Counter
from collections import OrderedDict
s = input().strip()
lis = sorted([x for x in s])
d = OrderedDict()
for i in lis:
	if i in d:
		d[i] += 1
	else:
		d[i] = 1
c = OrderedDict(sorted(d.items(), key=lambda t: t[0]))
i = 0
res = []
tes = []
t = {}
for (key, value) in OrderedDict(sorted(d.items(), key=lambda t: t[1], reverse=True)).items():
	if i <= 2:
		t[key] = value
		res.append(key)
		tes.append(value)
		i += 1
	else:
		break
print(res[0], tes[0])
print(res[1], tes[1])
print(res[2], tes[2])


from operator import itemgetter
S = input()
times = {}
for c in S:
	if c in times:
		times[c] += 1
	else:
		times[c] = 1
keys = sorted(sorted(times.items()), key=itemgetter(1), reverse=True)
for i in range(3):
	print('%s %d' % (keys[i][0], keys[i][1]))


def groupBy(f, lst):
	c = []
	out = []
	if lst == []:
		return []
	for i in range(len(lst)):
		v = lst[i]
		if i != 0 and f(prev) != f(v):
			out.append(c)
			c = []
		c.append(v)
		prev = v
	out.append(c)
	return out

def head(lst):
	return lst[0]

def print_pair(pair):
	(count, char) = pair
	print(char + ' ' + str(count))
	return
s0 = input()
g = groupBy(lambda x: x, sorted(s0))
pairs = sorted(zip(map(len, g), map(head, g)), key=lambda x: [-x[0], x[1]])[0:3]
for p in pairs:
	print_pair(p)


l = list(input())
lett = [0] * 26
for i in l:
	lett[ord(i) - 97] += 1
print(chr(lett.index(max(lett)) + 97) + ' ' + str(lett[lett.index(max(lett))]))
lett[lett.index(max(lett))] = 0
print(chr(lett.index(max(lett)) + 97) + ' ' + str(lett[lett.index(max(lett))]))
lett[lett.index(max(lett))] = 0
print(chr(lett.index(max(lett)) + 97) + ' ' + str(lett[lett.index(max(lett))]))


from collections import Counter
from operator import itemgetter
frequencies = Counter()
for char in input():
	frequencies[char] += 1
mc = frequencies.most_common()
mc = sorted(mc, key=itemgetter(0))
mc = list(sorted(mc, key=itemgetter(1), reverse=True))
for (char, freq) in mc[:3]:
	print(char, freq)


(s, d) = (input(), dict())
for l in s:
	if l not in d:
		d[l] = 1
	else:
		d[l] += 1
top3 = sorted(list(d.items()), key=lambda x: (x[1], 122 - ord(x[0])), reverse=True)[:3]
for (key, value) in top3:
	print(key + ' ' + str(value))


import sys
from collections import defaultdict
str = sys.stdin.readline()
chars = list(str)
ledict = defaultdict(int)
for c in chars:
	ledict[c] += 1
lelist = []
for (key, val) in ledict.items():
	lelist.append((val, key))
lelist.sort(key=lambda x: x[1])
lelist.sort(key=lambda x: x[0], reverse=True)
del lelist[3:]
for x in lelist:
	print(x[1], x[0])


from collections import Counter

def srtby(x):
	(a, b) = x
	return (-b, a)

def main():
	s = input()
	c = Counter(s)
	counts = sorted(c.items(), key=srtby)[:3]
	for (k, v) in counts:
		print('%s %s' % (k, v))
main()


freq = []
for y in range(26):
	freq.append(0)
s = input()
for x in s:
	t = ord(x) - ord('a')
	freq[t] = freq[t] + 1
for x in range(3):
	common = 0
	for y in range(len(freq)):
		if freq[y] > freq[common]:
			common = y
	print(chr(common + ord('a')) + ' ' + str(freq[common]))
	freq[common] = 0


import string as hello
string = input()
characters = {c: 0 for c in hello.ascii_lowercase}
for c in string:
	characters[c] += 1
frequency = {}
for c in characters:
	if characters[c] in frequency:
		frequency[characters[c]] += [c]
	else:
		frequency[characters[c]] = [c]
for c in frequency:
	frequency[c].sort()
a = sorted(list(frequency.keys()))[-1:-4:-1]
i = 0
for f in a:
	for c in frequency[f]:
		i += 1
		print(c, f)
		if i == 3:
			quit()


s = input()
cnt = dict()
for c in s:
	cnt[c] = cnt.get(c, 0) + 1
for p in sorted(cnt.items(), key=lambda x: (-x[1], x[0]))[:3]:
	print(*p)


from collections import defaultdict
from operator import itemgetter

def most_common(s):
	d = defaultdict(int)
	for letter in s:
		d[letter] += 1
	l = sorted(list(sorted(d.items())), key=itemgetter(1), reverse=True)[:3]
	for (letter, count) in l:
		print(letter, count)
s = input()
most_common(s)


S = input()
d = {}
m = [0] * 3
val = [None] * 3
for i in range(ord('a'), ord('z') + 1):
	d[i] = 0
for c in S:
	d[ord(c)] += 1
for i in range(ord('a'), ord('z') + 1):
	if d[i] > m[0]:
		(m[2], val[2]) = (m[1], val[1])
		(m[1], val[1]) = (m[0], val[0])
		(m[0], val[0]) = (d[i], i)
		continue
	if d[i] > m[1]:
		(m[2], val[2]) = (m[1], val[1])
		(m[1], val[1]) = (d[i], i)
		continue
	if d[i] > m[2]:
		(m[2], val[2]) = (d[i], i)
		continue
for i in range(3):
	print('%c %d' % (val[i], m[i]))


from collections import Counter

def lexico(array, times, max):
	for i in range(times):
		mini = min(array)
		print(mini, max)
		array.remove(mini)
s = input()
max_1_array = []
max_2_array = []
max_3_array = []
c = Counter(s)
value_array = list(c.values())
max_1 = max(value_array)
value_array.remove(max_1)
max_2 = max(value_array)
value_array.remove(max_2)
max_3 = max(value_array)
for i in range(len(c)):
	curr_value = list(c.items())[i]
	if curr_value[1] == max_1:
		max_1_array.append(curr_value[0])
	if curr_value[1] == max_2:
		max_2_array.append(curr_value[0])
	if curr_value[1] == max_3:
		max_3_array.append(curr_value[0])
if max_1 == max_2 == max_3:
	lexico(max_1_array, 3, max_1)
elif max_1 == max_2:
	lexico(max_1_array, 2, max_1)
	print(min(max_3_array), max_3)
elif max_2 == max_3:
	print(min(max_1_array), max_1)
	lexico(max_2_array, 2, max_2)
else:
	print(min(max_1_array), max_1)
	print(min(max_2_array), max_2)
	print(min(max_3_array), max_3)


s = input()
l = []
for i in range(26):
	l.append(0)
for i in s:
	l[ord(i) - ord('a')] += 1
m = 0
for i in range(len(l)):
	if l[i] > l[m]:
		m = i
print(chr(m + ord('a')) + ' ' + str(l[m]))
l[m] = 0
m = 0
for i in range(len(l)):
	if l[i] > l[m]:
		m = i
print(chr(m + ord('a')) + ' ' + str(l[m]))
l[m] = 0
m = 0
for i in range(len(l)):
	if l[i] > l[m]:
		m = i
print(chr(m + ord('a')) + ' ' + str(l[m]))
l[m] = 0


s = input()
d = {}
for c in s:
	if c in d:
		d[c] += 1
	else:
		d[c] = 1
vals = list(set(d.values()))
vals.sort(reverse=True)
i = 0
for v in vals:
	letters = [(key, value) for (key, value) in d.items() if value == v]
	letters.sort()
	for item in letters:
		if i >= 3:
			break
		print(item[0], item[1])
		i += 1


s = input()
array = []
has = []
for i in range(0, 26):
	array.append(chr(i + 97))
	has.append(0)
for i in range(0, len(s)):
	for j in range(0, 26):
		if array[j] == s[i]:
			has[j] = has[j] + 1
m = 0
for i in range(0, 26):
	if m < has[i]:
		m = has[i]
		j = i
print('', array[j], ' ', has[j], sep='')
has[j] = 0
m = 0
for i in range(0, 26):
	if m < has[i]:
		m = has[i]
		j = i
print('', array[j], ' ', has[j], sep='')
has[j] = 0
m = 0
for i in range(0, 26):
	if m < has[i]:
		m = has[i]
		j = i
print('', array[j], ' ', has[j], sep='')


a = input()
num = {}
for i in a:
	if i in num:
		num[i] += 1
	else:
		num[i] = 1
m = [[i, num[i]] for i in num]
m.sort(key=lambda x: x[0])
m.sort(reverse=True, key=lambda x: x[1])
m = m[:3]
for i in m:
	print(i[0], i[1])


z = {}
for i in range(ord('a'), ord('z') + 1):
	z[chr(i)] = 0
s = input().strip()
for i in s:
	z[i] += 1
res = []
for i in z:
	res.append((z[i], i))
res.sort(reverse=True)
last = 0
sec = 0
while sec < len(res):
	fir = res[sec][0]
	if fir == 0:
		break
	while sec < len(res) and res[sec][0] == fir:
		sec += 1
	hh = list(reversed(res[last:sec]))
	res = res[:last] + hh + res[sec:]
	last = sec
for i in range(3):
	print(str(res[i][1]) + ' ' + str(res[i][0]))


def countLetters(word):
	letterdict = {}
	for letter in word:
		letterdict[letter] = 0
	for letter in word:
		letterdict[letter] += 1
	return letterdict
stri = input()
d = countLetters(stri)
i = 0
abc = sorted(d.items(), key=lambda x: (-x[1], x[0]))
for i in range(0, 3):
	print(abc[i][0], abc[i][1])


s = input()
d = {}
for i in s:
	d[i] = 0
for i in s:
	d[i] += 1
a = sorted(d.values())
for i in range(1, 4):
	w = a[len(a) - i]
	for e in sorted(d.keys()):
		if d[e] == w:
			print(e, d[e])
			d.pop(e)
			break


st = input()
li = [0] * 26
for char in st:
	li[ord(char) - ord('a')] += 1
mfreq = [0, 0, 0]
posi = [-1, -1, -1]
for (idx, freq) in enumerate(li):
	if freq <= mfreq[2]:
		continue
	elif freq <= mfreq[1]:
		(mfreq[2], posi[2]) = (freq, idx)
	elif freq <= mfreq[0]:
		(mfreq[2], posi[2]) = (mfreq[1], posi[1])
		(mfreq[1], posi[1]) = (freq, idx)
	else:
		(mfreq[2], posi[2]) = (mfreq[1], posi[1])
		(mfreq[1], posi[1]) = (mfreq[0], posi[0])
		(mfreq[0], posi[0]) = (freq, idx)
print(chr(posi[0] + 97) + ' ' + str(mfreq[0]))
print(chr(posi[1] + 97) + ' ' + str(mfreq[1]))
print(chr(posi[2] + 97) + ' ' + str(mfreq[2]))


from collections import Counter
counter = Counter(input())
li = []
li = sorted(counter.items(), key=lambda x: x[1], reverse=True)
distinct = set((x for (_, x) in li))
ans_li = dict()
for (a, b) in li:
	ans_li[b] = []
for (a, b) in li:
	ans_li[b].append(a)
ans = sorted(ans_li.items(), key=lambda x: x[0], reverse=True)[:3]
count = 0
for (x, y) in ans:
	for a in sorted(y):
		if count == 3:
			break
		print(a, x)
		count += 1


import operator
from collections import defaultdict
a = list(input())
a_set = sorted(set(a))
list1 = []
for i in a_set:
	count = 0
	for j in a:
		if j == i:
			count += 1
	list1.append({'char': i, 'freq': count})
newlist = sorted(list1, key=lambda k: k['freq'], reverse=True)
count = 0
for dict1 in newlist:
	if count < 3:
		print(dict1['char'], dict1['freq'])
	else:
		break
	count += 1


import operator
s = input().strip()
counts = {}
for char in s:
	if char not in counts.keys():
		counts.update({char: s.count(char)})
counts = sorted(counts.items(), key=operator.itemgetter(0))
counts.sort(key=lambda row: row[1], reverse=True)
c = 0
for (key, value) in counts:
	if c >= 3:
		break
	print(key, value)
	c += 1


from collections import defaultdict
s = input()
d = defaultdict(int)
for c in s:
	d[c] += 1
for (k, v) in sorted(d.items(), key=lambda x: (-x[1], x[0]))[:3]:
	print('{0} {1}'.format(k, v))


s = input()
i = 0
a = []
d = {}
while len(s) != 0:
	c = s.count(s[0])
	a += [c]
	try:
		d[c] += [s[0]]
	except:
		d[c] = [s[0]]
	s = s.replace(s[0], '')
a.sort()
a.reverse()
b = d[a[0]]
if len(b) >= 3:
	b.sort()
	print(b[0], a[0])
	print(b[1], a[1])
	print(b[2], a[2])
elif len(b) == 2:
	b.sort()
	print(b[0], a[0])
	print(b[1], a[1])
	e = d[a[2]]
	e.sort()
	print(e[0], a[2])
else:
	print(b[0], a[0])
	f = d[a[1]]
	if len(f) >= 2:
		f.sort()
		print(f[0], a[1])
		print(f[1], a[2])
	else:
		print(f[0], a[1])
		g = d[a[2]]
		g.sort()
		print(g[0], a[2])


__author__ = 'Shivani'
import heapq

def most_common(s):
	h = []
	sd = set()
	for a in s:
		if a not in sd:
			sd.add(a)
			x = s.count(a)
			h.append((-x, a))
	heapq.heapify(h)
	for i in range(3):
		x = heapq.heappop(h)
		print(x[1], -x[0])

def main():
	most_common(input())
main()


from operator import itemgetter
s = input()
p = list(s)
k = set(p)
k = list(k)
a = []
b = []
for i in range(len(k)):
	z = p.count(k[i])
	b.append([k[i], z])
b = sorted(b, key=itemgetter(0))
b = sorted(b, key=itemgetter(1), reverse=True)
for i in range(3):
	print(b[i][0], b[i][1], sep=' ')


from collections import Counter
import operator
S = list(input())
c = Counter(S)
C = sorted(c.items(), key=lambda x: (-x[1], ord(x[0])))
for l in C[:3]:
	print(' '.join(map(str, l)))


a = input()
a = list(a)
listn = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
for k in range(3):
	maxb = 0
	c = ''
	for i in listn:
		b = a.count(i)
		if b > maxb:
			maxb = b
			c = i
	print(c, maxb)
	listn.remove(c)


alpha = 'abcdefghijklmnopqrstuvwxyz'
letters = input()
letter_dict = {}
ansA = None
ansB = None
ansC = None
for l in letters:
	if l in letter_dict:
		letter_dict[l] += 1
	else:
		letter_dict[l] = 1
for a in alpha:
	if a in letter_dict:
		if ansA == None or letter_dict[a] > ansA[1]:
			if ansA != None:
				if ansB != None:
					ansC = ansB
			ansB = ansA
			ansA = (a, letter_dict[a])
		elif ansB == None or letter_dict[a] > ansB[1]:
			if ansB != None:
				ansC = ansB
			ansB = (a, letter_dict[a])
		elif ansC == None or letter_dict[a] > ansC[1]:
			ansC = (a, letter_dict[a])
print(ansA[0], ansA[1])
print(ansB[0], ansB[1])
print(ansC[0], ansC[1])


from collections import Counter
counts = Counter(list(input())).most_common()
counts.sort(key=lambda x: (-x[1], x[0]))
for i in counts[:3]:
	print(i[0] + ' ' + str(i[1]))


s = input()
dict = {}
for i in s:
	try:
		dict[i] += 1
	except:
		dict[i] = 1
max = -1
char = 'a'
for i in dict:
	if dict[i] > max:
		max = dict[i]
		char = i
	elif dict[i] == max and i < char:
		char = i
print(char, max)
dict[char] = 0
max = -1
max = -1
char = 'a'
for i in dict:
	if dict[i] > max:
		max = dict[i]
		char = i
	elif dict[i] == max and i < char:
		char = i
print(char, max)
dict[char] = 0
max = -1
max = -1
char = 'a'
for i in dict:
	if dict[i] > max:
		max = dict[i]
		char = i
	elif dict[i] == max and i < char:
		char = i
print(char, max)
dict[char] = 0
max = -1


S = input()
list(map(lambda x: print(x[0], x[1]), sorted(map(lambda x: (x, S.count(x)), sorted(set(S))), key=lambda x: x[1], reverse=True)[0:3]))


s = input()
res = []
for i in sorted(set(s)):
	res.append((i, s.count(i)))
res.sort(key=lambda x: x[1], reverse=True)
print(res[0][0], res[0][1])
print(res[1][0], res[1][1])
print(res[2][0], res[2][1])


string = input()
letters = sorted(set(string))
count = {}
max1 = ['', 0]
max2 = ['', 0]
max3 = ['', 0]
for l in letters:
	c = string.count(l)
	if c > max1[1]:
		max3 = max2
		max2 = max1
		max1 = [l, c]
	elif c > max2[1]:
		max3 = max2
		max2 = [l, c]
	elif c > max3[1]:
		max3 = [l, c]
print(max1[0], max1[1])
print(max2[0], max2[1])
print(max3[0], max3[1])


from collections import defaultdict
s = input()
a = []
d = defaultdict(int)
for x in s:
	d[x] += 1
for x in d:
	a.append((x, d[x]))
a.sort(key=lambda item: item[0])
a.sort(key=lambda item: item[1], reverse=True)
c = 0
for x in a:
	c += 1
	print(x[0] + ' ' + str(x[1]))
	if c == 3:
		break


from collections import Counter

def sortTuple(f, s):
	if f[1] == s[1] and f[0] > s[0]:
		return (s, f)
	return (f, s)
ls = list(Counter(list(input().strip())).most_common())
for i in range(len(ls) - 1):
	for i in range(len(ls) - 1):
		(ls[i], ls[i + 1]) = sortTuple(ls[i], ls[i + 1])
for (char, freq) in ls[:3]:
	print(char, freq)


from operator import itemgetter
st = input()
l = [[chr(i + 97), 0] for i in range(26)]
l.reverse()
for i in st:
	l[122 - ord(i)][1] += 1
l.sort(key=itemgetter(1))
l.reverse()
print(l[0][0] + ' ' + str(l[0][1]))
print(l[1][0] + ' ' + str(l[1][1]))
print(l[2][0] + ' ' + str(l[2][1]))


from collections import Counter
most_common = Counter(input()).most_common()
for (char, val) in sorted(most_common, key=lambda x: (-x[1], x[0]))[:3]:
	print('{} {}'.format(char, val))


import operator
from collections import defaultdict
letters = defaultdict(int)
for let in input():
	letters[let] += 1
sorted_letters = sorted(letters.items(), key=operator.itemgetter(0))
sorted_letters.sort(key=operator.itemgetter(1), reverse=True)
for i in range(3):
	print('{} {}'.format(sorted_letters[i][0], sorted_letters[i][1]))


from collections import Counter
S = list(input())
cnt = Counter()
for c in S:
	cnt[c] += 1
l = sorted(Counter(cnt).most_common(len(cnt)), key=lambda x: x[0])
l = sorted(l, key=lambda x: x[1], reverse=True)[:3]
print('\n'.join(map(lambda t: t[0] + ' ' + str(t[1]), l)))


import sys
from collections import Counter

def __starting_point():
	s = input().strip()
	best = Counter(s)
	sortit = sorted(list(best.items()), key=lambda x: (-x[1], x[0]))[:3]
	print('\n'.join((x[0] + ' ' + str(x[1]) for x in sortit)))
__starting_point()


from collections import Counter
from operator import itemgetter
s = input()
dic = {}
for i in s:
	if i in dic:
		dic[i] += 1
	else:
		dic[i] = 1
arr = []
for i in sorted(zip(dic.values(), dic.keys())):
	arr.append((i[1], i[0]))
arr = sorted(arr, key=itemgetter(0))
arr = sorted(arr, key=itemgetter(1), reverse=True)
count = 0
for i in arr:
	if count == 3:
		break
	print(i[0] + ' ' + str(i[1]))
	count += 1


s = [None] * 26
p = input()
for i in range(97, 123):
	s[i - 97] = [chr(i), 0]
for c in p:
	k = ord(c)
	s[k - 97][1] += 1

def get_key(obj):
	return obj[1]
p = sorted(s, key=get_key, reverse=True)
for i in range(3):
	print(p[i][0], p[i][1])


import pprint
S = input()
frequencyDict = {}
for letter in S:
	frequencyDict[letter] = frequencyDict.setdefault(letter, 0) + 1
topThree = sorted(sorted(frequencyDict), key=frequencyDict.get, reverse=True)[:3]
for each in topThree:
	print(each, frequencyDict[each])


a = []
for i in range(26):
	a.append([0, chr(97 + i)])
s = input().strip()
for c in s:
	a[ord(c) - 97][0] += 1
a = sorted(a, key=lambda x: x[0], reverse=True)
for i in range(3):
	print('%c %d' % (a[i][1], a[i][0]))


freq = {}
topFreq = {}
data = input()
for c in data:
	if c not in freq:
		freq[c] = 1
	else:
		freq[c] += 1
for i in range(3):
	largest = 0
	largestChar = None
	for (k, v) in freq.items():
		if v == largest:
			if largestChar == None or k < largestChar:
				largestChar = k
		elif v > largest:
			largest = v
			largestChar = k
	if largest not in topFreq:
		topFreq[largest] = []
	topFreq[largest].append(largestChar)
	del freq[largestChar]
for k in sorted(topFreq.keys(), reverse=True):
	topFreq[largest].sort()
	for i in topFreq[k]:
		print(i, k)


s = input()
n = len(s)
d = {}
for i in range(n):
	d[s[i]] = 0
for i in range(n):
	d[s[i]] += 1
result = sorted(d.items(), key=lambda x: (-x[1], x[0]))
for i in range(3):
	print(result[i][0], result[i][1])


__author__ = 'Sak'
from operator import itemgetter
from collections import defaultdict
import collections
S = str(input())
c = collections.Counter()
c.update(S.rstrip())
d = defaultdict(list)
for (letter, count) in c.most_common(len(S)):
	d[count].append(letter)
cnt = 0
for key in sorted(d, reverse=True):
	for a in sorted(d[key]):
		print(str(a) + ' ' + str(key))
		cnt += 1
		if cnt == 3:
			break
	if cnt == 3:
		break


def swap(a, b):
	temp = a
	a = b
	b = temp
memo = {}
letters = 'abcdefghijklmnopqrstuvwxyz'
for i in letters:
	memo[i] = 0
string = input()
for i in string:
	memo[i] += 1
lst = []
for i in letters:
	minlist = []
	if memo[i] != 0:
		minlist.append(memo[i])
		minlist.append(i)
		lst.append(minlist)
lst = sorted(lst)
rev = lst[::-1]
for j in range(0, len(lst)):
	for k in range(1, len(lst)):
		if rev[k][0] == rev[k - 1][0]:
			if letters.index(rev[k][1]) < letters.index(rev[k - 1][1]):
				temp = rev[k - 1]
				rev[k - 1] = rev[k]
				rev[k] = temp
output = []
count = 0
i = 0
for i in range(0, len(rev)):
	minlist = []
	minlist.append(rev[i][1])
	minlist.append(rev[i][0])
	if minlist not in output:
		output.append(minlist)
		count += 1
for m in range(0, 3):
	print(output[m][0], output[m][1])


import sys
line = next(sys.stdin)
chars = list(line)
counts = dict()
for c in chars:
	if c in counts:
		counts[c] += 1
	else:
		counts[c] = 1

def kf(a):
	(_, va) = a
	return va

def kf2(a):
	(ka, va) = a
	return (-va, ka)
s = sorted(counts.items(), key=kf2)[:3]
for (k, v) in s:
	print('{} {}'.format(k, v))


s = input()
i = 0
fst = ''
fst1 = 0
snd = ''
snd1 = 0
trd = ''
trd1 = 0
l = sorted(s)
s = ''
for x in l:
	s = s + x
while len(s) != 0:
	first = s[0]
	count = s.count(first)
	s = s.replace(first, '')
	i = i + 1
	if count > fst1:
		trd = snd
		trd1 = snd1
		snd = fst
		snd1 = fst1
		fst = first
		fst1 = count
	elif count > snd1:
		trd = snd
		trd1 = snd1
		snd = first
		snd1 = count
	elif count > trd1:
		trd = first
		trd1 = count
print(fst + ' ' + str(fst1))
print(snd + ' ' + str(snd1))
print(trd + ' ' + str(trd1))


str = input()
count = {}
for i in str:
	if i in count:
		count[i] += 1
	else:
		count[i] = 1
l = [[i, count[i]] for i in count]

def getkey(item):
	return item[1]

def getkey2(item):
	return item[0]
l.sort(key=getkey, reverse=True)
ll = len(l)
i = 1
run = 1
while i < ll:
	if l[i][1] == l[i - 1][1]:
		run += 1
	else:
		b = l[i - run:i]
		b.sort(key=getkey2)
		l[i - run:i] = b
		run = 1
	i += 1
b = l[i - run:i]
b.sort(key=getkey2)
l[i - run:i] = b
i = 0
while i < ll and i < 3:
	print(l[i][0], end=' ')
	print(l[i][1])
	i += 1