collections.OrderedDict

An OrderedDict is a dictionary that remembers the order of the keys that were inserted first. If a new entry overwrites an existing entry, the original insertion position is left unchanged. 

Example

Code

>>> from collections import OrderedDict
>>> 
>>> ordinary_dictionary = {}
>>> ordinary_dictionary['a'] = 1
>>> ordinary_dictionary['b'] = 2
>>> ordinary_dictionary['c'] = 3
>>> ordinary_dictionary['d'] = 4
>>> ordinary_dictionary['e'] = 5
>>> 
>>> print ordinary_dictionary
{'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4}
>>> 
>>> ordered_dictionary = OrderedDict()
>>> ordered_dictionary['a'] = 1
>>> ordered_dictionary['b'] = 2
>>> ordered_dictionary['c'] = 3
>>> ordered_dictionary['d'] = 4
>>> ordered_dictionary['e'] = 5
>>> 
>>> print ordered_dictionary
OrderedDict([('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)])

Task  

You are the manager of a supermarket. 

You have a list of $N$ items together with their prices that consumers bought on a particular day. 

Your task is to print each item_name and net_price in order of its first occurrence.  

item_name = Name of the item. 

net_price = Quantity of the item sold multiplied by the price of each item.

Input Format

The first line contains the number of items, $N$. 

The next $N$ lines contains the item's name and price, separated by a space.

Constraints

$0<N\leq100$

Output Format

Print the item_name and net_price in order of its first occurrence.

Sample Input
9
BANANA FRIES 12
POTATO CHIPS 30
APPLE JUICE 10
CANDY 5
APPLE JUICE 10
CANDY 5
CANDY 5
CANDY 5
POTATO CHIPS 30

Sample Output
BANANA FRIES 12
POTATO CHIPS 60
APPLE JUICE 20
CANDY 20

Explanation

BANANA FRIES: Quantity bought: $\mbox{I}$, Price: $12$ 

Net Price: $12$ 

POTATO CHIPS: Quantity bought: $2$, Price: $30$

Net Price: $\mbox{60}$ 

APPLE JUICE: Quantity bought: $2$, Price: $10$ 

Net Price: $\textbf{20}$ 

CANDY: Quantity bought: $\begin{array}{c}A\end{array}$, Price: $5$ 

Net Price: $\textbf{20}$

Source: hackerrank | Difficulty: EASY


Solutions:
from collections import OrderedDict, Counter
report = type('OC', (OrderedDict, Counter), {})()
for line in range(int(input())):
	(article, price) = input().rsplit(' ', 1)
	report[article] += int(price)
for item in report.items():
	print(*item)


from collections import OrderedDict
d = OrderedDict()
for _ in range(int(input())):
	a = input().split()
	k = ' '.join(a[:-1])
	d[k] = d.get(k, 0) + int(a[-1])
print(*[' '.join(map(str, x)) for x in d.items()], sep='\n')


from collections import OrderedDict
sales = OrderedDict()
for k in range(0, int(input())):
	(name, price) = input().rstrip().rsplit(' ', 1)
	if name in sales:
		sales[name] += int(price)
	else:
		sales[name] = int(price)
while len(sales) > 0:
	print(' '.join([str(x) for x in sales.popitem(False)]))


import re
from collections import *
od = OrderedDict()
for i in range(int(input())):
	ip = input()
	tmp = re.search('\\d+', ip)
	z = tmp.start()
	try:
		od[ip[:z]] += int(ip[z:])
	except:
		od[ip[:z]] = int(ip[z:])
for x in od:
	print(x + str(od[x]))


from collections import OrderedDict
ordered_dictionary = OrderedDict()
numItems = int(input())
for _ in range(numItems):
	baseName = []
	inputList = input().split()
	for i in range(len(inputList)):
		if inputList[i].isdigit():
			itemValue = int(inputList[i])
			break
		baseName.append(inputList[i])
		itemName = ' '.join(baseName)
	if itemName in ordered_dictionary:
		ordered_dictionary[itemName] += itemValue
	else:
		ordered_dictionary[itemName] = itemValue
for i in ordered_dictionary:
	print(i + ' ' + str(ordered_dictionary[i]))


from collections import OrderedDict
a = OrderedDict()
for i in range(int(input())):
	ar = input().split()
	s = ' '.join(ar[0:len(ar) - 1])
	if s in a:
		a[s] += int(ar[-1])
	else:
		a[s] = int(ar[-1])
for i in a.keys():
	print(i + ' ' + str(a[i]))


from collections import Counter
(foods, prices) = ([], [])
for _ in range(int(input())):
	k = list(input().split())
	food = ' '.join(k[0:-1])
	amount = k[-1]
	foods.append(food)
	prices.append(amount)
k = Counter(foods)
for i in sorted(k):
	print(i, int(k[i]) * int(prices[foods.index(i)]))


from collections import OrderedDict
prices = OrderedDict()
n = int(input())
for _ in range(n):
	s = input().split()
	(item_name, price) = (' '.join(s[:-1]), int(s[-1]))
	if item_name not in prices:
		prices[item_name] = price
	else:
		prices[item_name] += price
for (item, price) in prices.items():
	print('%s %s' % (item, price))


from collections import OrderedDict
N = int(input())
d = OrderedDict()
for i in range(N):
	(item, sep, cost) = input().strip().rpartition(' ')
	if item in d:
		d[item] += int(cost)
	else:
		d[item] = int(cost)
for i in d.items():
	print('%s %d' % (i[0], i[1]))


from collections import OrderedDict
a = OrderedDict()
for i in range(int(input())):
	(item, space, quantity) = input().rpartition(' ')
	a[item] = a.get(item, 0) + int(quantity)
for (item, quantity) in a.items():
	print(item, quantity)


from collections import OrderedDict
import re
order = OrderedDict()
for i in range(int(input())):
	m = input().strip()
	item = re.search('^([a-zA-Z ]+)', m)
	price = re.search('(\\d+)$', m)
	i = item.group()
	p = price.group()
	order.setdefault(i, 0)
	order[i] += int(p)
for (k, v) in order.items():
	print(k + str(v))


from collections import OrderedDict
f = OrderedDict()
n = int(input())
for _ in range(n):
	s = input().split()
	a = ' '.join(s[:-1])
	b = int(s[-1])
	if a not in f:
		f[a] = 0
	f[a] += b
for a in f:
	print(a, f[a])


from collections import OrderedDict
items = list()
for i in range(int(input())):
	entry = input().strip().split()
	name = ' '.join(entry[0:len(entry) - 1])
	price = int(entry[len(entry) - 1])
	items.append((name, price))
d = OrderedDict()
for item in sorted(items, key=lambda i: i[0]):
	try:
		d[item[0]] += item[1]
	except KeyError as e:
		d[item[0]] = item[1]
for item in d.items():
	print(item[0], item[1])


from collections import OrderedDict
ordered_dictionary = OrderedDict()
N = int(input())
for _ in range(N):
	s = [x for x in input().split()]
	if ' '.join(s[:-1]) in ordered_dictionary:
		ordered_dictionary[' '.join(s[:-1])] += int(s[-1])
	else:
		ordered_dictionary[' '.join(s[:-1])] = int(s[-1])
for key in ordered_dictionary:
	print(key, ordered_dictionary[key])


from collections import OrderedDict
d = {}
N = int(input())
for n in range(N):
	cmd = input().strip().split(' ')
	key = ' '.join(cmd[:-1])
	if key in d:
		d[key] += int(cmd[-1])
	else:
		d[key] = int(cmd[-1])
d = OrderedDict(sorted(d.items(), key=lambda t: t[0]))
for i in d.items():
	print(i[0], i[1])


import collections

def read_name_and_price():
	line = input()
	last_space_index = len(line) - 1 - line[::-1].index(' ')
	return (line[:last_space_index], int(line[last_space_index + 1:]))

def main():
	N = int(input())
	ordered_name2price = collections.OrderedDict()
	for _ in range(N):
		(name, price) = read_name_and_price()
		if name not in ordered_name2price:
			ordered_name2price[name] = 0
		ordered_name2price[name] += price
	for name in ordered_name2price:
		print(name, ordered_name2price[name])
main()


from collections import OrderedDict
di = OrderedDict()
for _ in range(int(input())):
	info = input().split()
	product = ' '.join(info[:-1])
	price = int(info[-1])
	di[product] = di.setdefault(product, 0) + price
for cash in di:
	print(cash, di[cash])


from collections import OrderedDict
getTotalCount = int(input())
o_dict = OrderedDict()
for i in range(0, getTotalCount):
	getInput = input().split()
	tempValue = ''
	for i in range(0, len(getInput)):
		if getInput[i].isdigit():
			o_dict.update({tempValue.strip(): o_dict.get(tempValue.strip(), 0) + int(getInput[i])})
		else:
			tempValue += getInput[i] + ' '
for (key, value) in o_dict.items():
	print(key, value)


from collections import OrderedDict
import re
od = OrderedDict()
regex = re.compile('(?P<nombre>[a-zA-Z]+(?:\\s[a-zA-Z]+)?)\\s(?P<precio>\\d+)')
for _ in range(int(input())):
	m = regex.match(input())
	try:
		od[m.group('nombre')] += int(m.group('precio'))
	except:
		od[m.group('nombre')] = int(m.group('precio'))
for (k, v) in od.items():
	print(k, v)


from collections import OrderedDict
d = OrderedDict()
for i in range(int(input())):
	temp = input().split(' ')
	try:
		d[' '.join(temp[:-1])] += int(temp[-1])
	except:
		d[' '.join(temp[:-1])] = int(temp[-1])
for (k, v) in d.items():
	print(k, v)


from collections import OrderedDict
N = int(input())
data = OrderedDict()
for _ in range(N):
	raw = input().rsplit(maxsplit=1)
	try:
		data[raw[0]] += int(raw[1])
	except KeyError:
		data[raw[0]] = int(raw[1])
for item in data:
	print(str(item) + ' ' + str(data[item]))


from collections import OrderedDict
N = int(input())
goods = OrderedDict()
for _ in range(N):
	line = input().split()
	price = int(line[len(line) - 1])
	item = ' '.join(line[:len(line) - 1])
	if item in goods:
		goods[item] += price
	else:
		goods[item] = price
[print('%s %d' % (x, goods[x])) for x in goods]


from collections import OrderedDict
N = int(input())
sales = OrderedDict()
for _ in range(N):
	nextTrans = input().split()
	nextKey = ' '.join(nextTrans[:-1])
	if nextKey in sales.keys():
		sales[nextKey] += int(nextTrans[-1])
	else:
		sales[nextKey] = int(nextTrans[-1])
for k in sales:
	print(k + ' ' + str(sales[k]))


from collections import OrderedDict
d = OrderedDict()
for _ in range(int(input())):
	(item, space, quantity) = input().rpartition(' ')
	d[item] = d.get(item, 0) + int(quantity)
for (item, quantity) in d.items():
	print(item, quantity)


from collections import OrderedDict, namedtuple
Purchase = namedtuple('Purchase', 'name price')
N = int(input())
purchases = []
for _ in range(N):
	(*name, price) = input().split()
	name = ' '.join(name)
	purchases.append(Purchase(name, int(price)))
purchases.sort(key=lambda x: x.name)
mem = OrderedDict()
for item in purchases:
	mem[item.name] = mem.setdefault(item.name, 0) + item.price
for item in mem.items():
	print(*item, sep=' ')


from collections import OrderedDict
N = int(input())
D = OrderedDict()
for _ in range(N):
	S = input()
	(name, price) = (S[:S.rfind(' ')], int(S[S.rfind(' ') + 1:]))
	D[name] = D.get(name, 0) + price
for val in D.items():
	print(*val)


from collections import OrderedDict
N = int(input())
od = OrderedDict()
for i in range(N):
	iLine = input()
	num = iLine.split()[-1]
	itemName = iLine[:-len(num) - 1].strip()
	itemNum = int(num)
	if itemName not in od:
		od[itemName] = itemNum
	else:
		od[itemName] += itemNum
for o in od:
	print(o, od[o])


from collections import OrderedDict
itemList = OrderedDict()
for i in range(int(input())):
	ip = input().split(' ')
	item = ' '.join(ip[:-1])
	rate = int(''.join(ip[-1:]))
	if item in itemList:
		itemList[item] = itemList[item] + rate
	else:
		itemList[item] = rate
for (key, value) in itemList.items():
	print('{} {}'.format(key, value))


import collections, re
n = int(input())
item_od = collections.OrderedDict()
for i in range(n):
	record_list = re.split('(\\d+)$', input().strip())
	item_name = record_list[0]
	item_price = int(record_list[1])
	if item_name not in item_od:
		item_od[item_name] = item_price
	else:
		item_od[item_name] = item_od[item_name] + item_price
for i in item_od:
	print(i + str(item_od[i]))


import collections
N = int(input())
items = collections.OrderedDict()
for n in range(N):
	line = input().split()
	(name, price) = (' '.join(line[:-1]), int(line[-1]))
	if name in items:
		items[name] += price
	else:
		items[name] = price
for item in items:
	print(item, items[item])


from collections import OrderedDict
sales = OrderedDict()
for _ in range(int(input())):
	line = input().split()
	item = ' '.join(line[:-1])
	if item in sales:
		sales[item] += int(line[-1])
	else:
		sales[item] = int(line[-1])
for k in sales.keys():
	print(k, sales[k])


from collections import OrderedDict
d = OrderedDict()
for _ in range(int(input())):
	(name, space, qty) = input().rpartition(' ')
	d[name] = d.get(name, 0) + int(qty)
for (name, qty) in d.items():
	print(name, qty)


from collections import OrderedDict
d = OrderedDict()
x = input()
for i in range(int(x)):
	y = input().split()
	s = ' '.join(y[:len(y) - 1])
	if s not in d:
		d[s] = int(y[len(y) - 1])
	else:
		d[s] += int(y[len(y) - 1])
for (k, v) in d.items():
	print(k, v)


from collections import OrderedDict

def __starting_point():
	num = int(input().strip())
	history = OrderedDict()
	for _ in range(num):
		get_log = input().strip().split()
		good = ' '.join(get_log[:-1])
		sold = int(get_log[-1])
		if good not in list(history.keys()):
			history[good] = sold
		else:
			history[good] += sold
	for key in list(history.keys()):
		print('{} {}'.format(key, history[key]))
__starting_point()


from collections import OrderedDict
d = OrderedDict()
n = int(input())
for i in range(n):
	data = input().split()
	item = ''
	price = int(data[-1])
	for idx in range(len(data) - 1):
		item += data[idx] + ' '
	if item in d:
		d[item] += price
	else:
		d[item] = price
for (k, v) in d.items():
	print(k, v, sep='')


from collections import OrderedDict
od = OrderedDict()
n = int(input())
for i in range(n):
	s = input().split()
	name = ' '.join(s[0:-1])
	price = int(s[-1])
	v = 0
	if name in od:
		v = od[name]
	od[name] = v + price
for (k, v) in od.items():
	print(k + ' ' + str(v))


from collections import OrderedDict
N = int(input())
ordered_dictionary = OrderedDict()
for i in range(N):
	items = input().rpartition(' ')
	(item_name, sep, item_price) = items
	ordered_dictionary[item_name] = ordered_dictionary.get(item_name, 0) + int(item_price)
for (items, price) in ordered_dictionary.items():
	print(items, price)


from collections import OrderedDict
n = int(input())
d = OrderedDict()
for _ in range(n):
	(i, _, p) = input().rpartition(' ')
	d[i] = d.get(i, 0) + int(p)
for (k, v) in d.items():
	print(k, v)


from collections import OrderedDict
ordinary_dictionary = OrderedDict()
for i in range(int(input())):
	inputs = input().strip().split(' ')
	key = ' '.join(inputs[:-1])
	if ordinary_dictionary.get(key):
		ordinary_dictionary[key] = int(inputs[-1:][0]) + ordinary_dictionary[' '.join(inputs[:-1])]
	else:
		ordinary_dictionary[key] = int(inputs[-1:][0])
for i in ordinary_dictionary:
	print(i + ' ', end='')
	print(ordinary_dictionary[i])


from collections import OrderedDict
d = OrderedDict()
n = int(input())
for _ in range(n):
	(item, space, quantity) = input().rpartition(' ')
	d[item] = d.get(item, 0) + int(quantity)
for (item, quantity) in d.items():
	print(item, quantity)


from collections import OrderedDict
items = OrderedDict()
N = int(input())
for _ in range(N):
	data = input().split(' ')
	(item, price) = (' '.join(data[:-1]), int(data[-1]))
	items[item] = items.get(item, 0) + price
for item in items:
	print('%s %d' % (item, items[item]))


from collections import OrderedDict
(n, d) = (int(input()), OrderedDict())
for _ in range(n):
	line = input().rstrip().split()
	(item_name, net_price) = (' '.join(line[:-1]), int(line[-1]))
	if item_name in d:
		d[item_name] += net_price
	else:
		d[item_name] = net_price
for item in sorted(sorted(d, key=lambda t: t[0])):
	print(item, d[item])


from collections import OrderedDict
dic = OrderedDict()
for _ in range(0, int(input())):
	st = input().split()
	if ' '.join(st[:-1]) in dic:
		val = dic[' '.join(st[:-1])]
		dic[' '.join(st[:-1])] = val + int(st[-1])
	else:
		dic[' '.join(st[:-1])] = int(st[-1])
for k in sorted(dic):
	print(k, dic[k])


from collections import OrderedDict
n = int(input())
inputs = []
d = OrderedDict()
for i in range(0, n):
	inp = input().rsplit(' ', 1)
	item = inp[0]
	price = int(inp[1])
	if item in d:
		d[item] += price
	else:
		d[item] = price
d = OrderedDict(sorted(d.items()))
for (k, v) in d.items():
	print(k, v)


from collections import OrderedDict
d = OrderedDict()
for _ in range(int(input())):
	inp = input().split()
	(item, quantity) = (' '.join(inp[:-1]), int(inp[-1]))
	d[item] = d.get(item, 0) + quantity
for (item, quantity) in d.items():
	print(item, quantity)


from collections import OrderedDict, defaultdict
ds = defaultdict(int)
ls = []
for _ in range(int(input())):
	vs = ''.join(list(reversed(input().strip())))
	(price, name) = vs.split(' ', 1)
	price = int(''.join(list(reversed(price))))
	name = ''.join(list(reversed(name.strip())))
	ds[name] += price
d = OrderedDict(sorted(ds.items()))
for xs in d.items():
	print(*xs)


from collections import OrderedDict
N = int(input())
d = OrderedDict()
for each in range(N):
	inputs = input()
	netprice = int(inputs.split()[-1])
	itemname = ' '.join(inputs.split()[:-1])
	if itemname in d:
		d[itemname] = int(d[itemname]) + int(netprice)
	else:
		d[itemname] = int(netprice)
d = OrderedDict(sorted(d.items()))
keys = d.keys()
for each in keys:
	print(each + ' ' + str(d[each]))


import collections
d = collections.OrderedDict()
n = int(input())
for i in range(n):
	k = input().strip().split()
	n = k[len(k) - 1]
	k.remove(n)
	k = ' '.join(k)
	d[k] = d.get(k, 0) + int(n)
for i in d.keys():
	print(i, d[i])


from collections import OrderedDict, defaultdict
A = OrderedDict()
b = defaultdict(int)
for i in range(0, int(input())):
	a = input().split()
	try:
		val = int(a[1])
		s = a[0]
	except:
		val = int(a[2])
		s = a[0] + ' ' + a[1]
	b[s] = b[s] + val
	A[s] = i
for i in A:
	print(i, b[i])


import collections
import sys
N = int(sys.stdin.readline())
A = collections.OrderedDict()
for _ in range(N):
	x = sys.stdin.readline().split()
	net_price = int(x.pop())
	item_name = ' '.join(x)
	if item_name not in A:
		A[item_name] = 0
	A[item_name] += net_price
for (k, v) in A.items():
	print(k, v)


from collections import OrderedDict
items = {}
for i in range(int(input())):
	line = input().split()
	net_price = int(line.pop())
	item_name = ' '.join(line)
	if item_name in items:
		items[item_name] += net_price
	else:
		items[item_name] = net_price
for i in sorted(items.items(), key=lambda t: t[0]):
	print(i[0], i[1])


from collections import OrderedDict
items = OrderedDict()
for i in range(int(input())):
	(*item_name, net_price) = input().split()
	item_name = ' '.join(item_name)
	net_price = int(net_price)
	if item_name not in items:
		items[item_name] = net_price
	else:
		items[item_name] += net_price
for item in items:
	print(item, items[item])


from collections import OrderedDict
net_prices = OrderedDict()
N = int(input())
for _ in range(N):
	(*names, price) = input().split()
	name = ' '.join(names)
	if name in net_prices:
		net_prices[name] += int(price)
	else:
		net_prices[name] = int(price)
for (k, v) in net_prices.items():
	print(k, v)


from collections import OrderedDict
d = OrderedDict()
n = int(input())
for _ in range(n):
	args = input().split()
	(name, price) = (' '.join(args[:-1]), int(args[-1]))
	d[name] = d.get(name, 0) + price
for (item, price) in d.items():
	print(item, price)


from collections import OrderedDict
d = OrderedDict()
n = int(input())
for _ in range(n):
	(item, space, price) = input().rpartition(' ')
	d[item] = d.get(item, 0) + int(price)
for (item, price) in d.items():
	print(item, price)


from collections import OrderedDict
n = int(input())
sales = OrderedDict()
for _ in range(n):
	(item_name, _, net_price) = input().strip().rpartition(' ')
	if item_name not in sales:
		sales[item_name] = int(net_price)
	else:
		sales[item_name] += int(net_price)
for (item_name, total_net_price) in sales.items():
	print(item_name, total_net_price)


from collections import OrderedDict
N = int(input())
Groceries = OrderedDict()
Price = 0
for i in range(N):
	Purchase = [x for x in input().split()]
	Price = int(Purchase[len(Purchase) - 1])
	del Purchase[len(Purchase) - 1]
	Purchase = ' '.join(Purchase)
	try:
		Price += Groceries[Purchase]
		Groceries[Purchase] = Price
	except KeyError:
		Groceries[Purchase] = Price
for item in Groceries:
	print(item, Groceries[item])


from collections import OrderedDict
from collections import Counter
market = OrderedDict()
temp = []
for i in range(int(input())):
	s = input()
	for j in range(len(s) - 1, 0, -1):
		if s[j] == ' ':
			market[s[:j]] = s[j:]
			temp.append(s[:j])
			break
temp = Counter(temp)
for i in market:
	print(i, int(market[i]) * temp[i])


from collections import OrderedDict

def solve():
	c = OrderedDict()
	t = int(input())
	a = []
	for i in range(0, t):
		s = input().strip()
		if s in c:
			c[s] += 1
		else:
			c[s] = 1
	for (k, s) in c.items():
		a = k.split()
		j = int(a[-1]) * s
		a.pop()
		print(' '.join(a), end=' ')
		print(j)
solve()


from collections import OrderedDict
n = int(input())
items = OrderedDict()
for _ in range(n):
	(item, s, price) = input().rpartition(' ')
	items[item] = items.get(item, 0) + int(price)
[print(x, y) for (x, y) in items.items()]


from collections import OrderedDict
n = int(input())
d = OrderedDict()
for i in range(n):
	x = input().split()
	s = ' '.join(x[0:-1])
	if s in d:
		d[s] += int(x[-1])
	else:
		d[s] = int(x[-1])
for (i, k) in d.items():
	print(i, k)


d = {}
for _ in range(0, int(input())):
	a = input().strip().split()
	if ' '.join(a[:-1]) in d:
		d[' '.join(a[:-1])] += int(a[-1])
	else:
		d[' '.join(a[:-1])] = int(a[-1])
for x in sorted(d.keys()):
	print('{} {}'.format(x, d[x]))


from itertools import takewhile
from collections import OrderedDict
product_count = int(input())
product_data = OrderedDict()
for x in range(0, product_count):
	this_product = input().split()
	product_name = ' '.join(list(takewhile(lambda x: not x.isnumeric(), this_product)))
	product_price = int(this_product[-1])
	if product_name in product_data:
		product_data[product_name] += product_price
	else:
		product_data[product_name] = product_price
for item in product_data.items():
	print(*item)


from collections import OrderedDict
items = OrderedDict()
for i in range(int(input())):
	(key, val) = input().rsplit(maxsplit=1)
	if key in items:
		items[key] += int(val)
	else:
		items[key] = int(val)
for i in items.items():
	print(*i)


from collections import OrderedDict
N = int(input())
ordered_dictionary = OrderedDict()
for _ in range(N):
	tls = input().split()
	item = ' '.join(tls[:-1])
	cost = int(tls[-1])
	if item in ordered_dictionary:
		ordered_dictionary[item] += cost
	else:
		ordered_dictionary[item] = cost
for key in ordered_dictionary.keys():
	print(key, ordered_dictionary[key])


from collections import OrderedDict
d = OrderedDict()
n = int(input())
for _ in range(n):
	line = input().split()
	(item_name, net_price) = (' '.join(line[:-1]), int(line[-1]))
	if item_name in d:
		d[item_name] += net_price
	else:
		d[item_name] = net_price
for (item_name, net_price) in d.items():
	print(item_name, net_price)


from collections import namedtuple
from collections import OrderedDict

def main():
	n = int(input())
	d = OrderedDict()
	for _ in range(n):
		s = input().split()
		now = ' '.join(s[:-1])
		if now in d:
			d[now] += int(s[-1])
		else:
			d[now] = int(s[-1])
	for i in d:
		print(i, d[i])
main()


from collections import OrderedDict
n = int(input())
cart = OrderedDict()
for i in range(n):
	test = input().strip().split()
	(item, price) = (' '.join(test[:len(test) - 1]), int(''.join(test[len(test) - 1:])))
	if item in cart:
		cart[item] += price
	else:
		cart[item] = price
for x in cart:
	print(x, cart[x])


from collections import OrderedDict

def main():
	n = int(input())
	d = OrderedDict()
	for _ in range(n):
		(item, price) = input().rsplit(' ', 1)
		if item in d:
			d[item] += int(price)
		else:
			d[item] = int(price)
	for (k, v) in d.items():
		print(k, v)
main()


from collections import OrderedDict, Counter
dict = OrderedDict()
for i in range(int(input())):
	a = input().split()
	b = int(a[-1])
	a = ' '.join((a[x] for x in range(0, len(a) - 1)))
	if a in dict:
		dict[a] += b
	else:
		dict[a] = b
dict = OrderedDict(sorted(dict.items(), key=lambda t: t[0]))
for a in dict:
	print(a, dict[a])


from collections import OrderedDict
n = int(input())
order_d = OrderedDict()
for i in range(n):
	name = ''
	value = ''
	inp = input().split(' ')
	value = inp[len(inp) - 1]
	for j in range(len(inp) - 1):
		name += inp[j] + ' '
	name.strip()
	if name in order_d:
		order_d[name] += int(value)
	else:
		order_d[name] = int(value)
for (key, value) in order_d.items():
	print(key + '' + str(value))


from collections import OrderedDict
d = OrderedDict()
for i in range(int(input())):
	temp = input().split()
	name = ' '.join(temp[:-1])
	num = int(temp[-1])
	if name in d.keys():
		d[name] += num
	else:
		d[name] = num
for (name, num) in d.items():
	print(name, num)


from collections import OrderedDict
n = int(input())
d = OrderedDict()
for _ in range(n):
	(position, count) = input().rsplit(' ', 1)
	count = int(count)
	d[position] = d.get(position, 0) + count
for (position, count) in d.items():
	print(position, count)


from collections import OrderedDict
n = int(input())
items = OrderedDict()
for _ in range(n):
	line = input().split()
	item = ' '.join(line[:-1])
	price = int(line[-1])
	if item not in items:
		items[item] = 0
	items[item] += price
for (key, value) in items.items():
	print(key, value)


from collections import OrderedDict
N = int(input())
sales = OrderedDict()
for _ in range(N):
	sale = input()
	name = ''.join((x for x in sale if not x.isdigit())).rstrip()
	price = int(''.join((x for x in sale if x.isdigit())))
	sales[name] = sales.get(name, 0) + price
for (key, value) in sorted(sales.items()):
	print(key, value)


from collections import *
N = int(input())
D = OrderedDict()
for i in range(N):
	L = input().split()
	Name = ' '.join(L[0:-1])
	Price = int(L[-1])
	if Name in D:
		D[Name] += Price
	else:
		D[Name] = Price
for i in D:
	print(i + ' ' + str(D[i]))


d = dict()
for i in range(int(input())):
	inp = input().split()
	name = ' '.join(inp[:-1])
	quant = int(inp[-1])
	if name in d:
		d[name] += quant
	else:
		d[name] = quant
l = [(a, b) for (a, b) in d.items()]
l.sort()
for el in l:
	print(' '.join(map(str, el)))


from collections import OrderedDict
items = OrderedDict()
for i in range(int(input())):
	inp = input().strip().split()
	(key, val) = (' '.join(inp[:-1]), int(inp[-1]))
	items[key] = items.get(key, 0) + val
for i in items:
	print(i, items[i])


from collections import OrderedDict
od = OrderedDict()
for _ in range(int(input())):
	(item, price) = input().rsplit(maxsplit=1)
	od[item] = od.setdefault(item, 0) + int(price)
for item in od:
	print(item, od[item])


from collections import OrderedDict
length = int(input())
priceList = OrderedDict()
for i in range(0, length):
	theInput = input().split()
	item = ' '.join(theInput[:-1])
	price = int(theInput[-1])
	if item in priceList.keys():
		priceList[item] = priceList[item] + price
	else:
		priceList[item] = price
for key in priceList.keys():
	print(key + ' ' + str(priceList[key]))


from collections import OrderedDict
d = OrderedDict()
for _ in range(int(input())):
	line = input().split()
	name = ' '.join(line[:-1])
	price = int(line[-1])
	if name not in d:
		d[name] = 0
	d[name] += int(price)
for (name, price) in d.items():
	print(name, price)


from collections import OrderedDict
items = OrderedDict()
for _ in range(int(input())):
	line = input().split()
	(item_name, item_price) = (' '.join(line[:-1]), int(line[-1]))
	if item_name in items:
		items[item_name] += int(item_price)
	else:
		items[item_name] = int(item_price)
for (k, v) in items.items():
	print('{} {}'.format(k, v))


d = {}
for _ in range(int(input())):
	line = input().strip().split()
	name = ' '.join(line[:-1])
	count = int(line[-1])
	if name in d:
		d[name] += count
	else:
		d[name] = count
for (a, b) in sorted(d.items()):
	print(a, b)


from collections import OrderedDict
sales_report = OrderedDict()
n = int(input())
for _ in range(n):
	raw_data = input().strip().split()
	(item, price) = (' '.join(raw_data[:-1]), int(raw_data[-1]))
	if item in sales_report:
		sales_report[item] += price
	else:
		sales_report[item] = price
for item in sales_report:
	print(item + ' ' + str(sales_report[item]))


from collections import OrderedDict
ord_dic = OrderedDict()
nips = int(input())
for _ in range(0, nips):
	ip = list(map(str, input().split()))
	key = ''
	val = 0
	for i in ip:
		if i.isdigit():
			val = int(i)
		else:
			key = key + i + ' '
	key = key[:len(key) - 1]
	if key in ord_dic.keys():
		ord_dic[key] = ord_dic[key] + val
	else:
		ord_dic[key] = val
for val in ord_dic.keys():
	print(val, ord_dic[val], sep=' ')


from collections import OrderedDict
n = int(input())
item_dict = OrderedDict()
for _ in range(n):
	(*key, price) = input().split()
	price = int(price)
	key = ' '.join(key)
	total_price = item_dict[key] + price if key in item_dict.keys() else price
	item_dict[key] = total_price
for key in item_dict.keys():
	print('%s %s' % (key, item_dict[key]))


from collections import OrderedDict
n = int(input())
inventory = OrderedDict()
for i in range(0, n):
	data = input().strip().split()
	price = int(data[-1])
	product = ' '.join(data[:-1])
	if product in inventory:
		inventory[product] += price
	else:
		inventory[product] = price
for (k, v) in inventory.items():
	print(k, v)


from collections import OrderedDict
t = int(input())
items = OrderedDict()
for _ in range(t):
	s = input().split()
	if ' '.join(s[0:len(s) - 1]) in items:
		items[' '.join(s[0:len(s) - 1])] += int(s[-1])
	else:
		items[' '.join(s[0:len(s) - 1])] = int(s[-1])
for item in items:
	print(item, items[item])


from collections import OrderedDict
import re
orders = OrderedDict()
prices = {}
for _ in range(int(input())):
	match = re.search('([A-Za-z\\s]+)(\\d+)', input())
	item = match.group(1).strip()
	price = int(match.group(2))
	try:
		orders[item] += 1
	except KeyError:
		orders[item] = 1
		prices[item] = price
for (item, quantity) in orders.items():
	print(item, quantity * prices[item])


from collections import OrderedDict
N = int(input())
food = OrderedDict()
for n in range(N):
	line = input().split()
	item = ' '.join(line[:len(line) - 1:1])
	cost = int(line[len(line) - 1])
	if item in food:
		food[item] += cost
	else:
		food[item] = cost
for x in food:
	print(x, food[x])


from collections import OrderedDict
d = OrderedDict()
for i in range(int(input())):
	q = input().split()
	n = int(q.pop())
	if ' '.join(q) in d:
		d[' '.join(q)] += n
	else:
		d[' '.join(q)] = n
for i in list(d):
	print(i, d[i])


from collections import OrderedDict
d = OrderedDict()
n = int(input().strip())
for i in range(n):
	s = input().split()
	(name, price) = ('', 0)
	for w in s:
		if w.isnumeric():
			price = int(w)
		else:
			name = name + ' ' + w
	name = name.strip()
	if name not in d:
		d[name] = price
	else:
		d[name] += price
for k in d:
	print(k + ' ' + str(d[k]))


from collections import OrderedDict
D = OrderedDict()
for _ in range(int(input())):
	l = input().split()
	(i, p) = (' '.join(l[:-1]), int(l.pop()))
	D[i] = D.get(i, 0) + p
for i in D:
	print(i, D[i])


from collections import OrderedDict
N = int(input())
d = OrderedDict()
for _ in range(N):
	line = input().split()
	(key, value) = (' '.join(line[:-1]), int(line[-1]))
	d[key] = d[key] + value if key in d else value
for (k, v) in d.items():
	print(k, v)


from collections import OrderedDict
d = OrderedDict()
for _ in range(int(input())):
	s = input().split()
	k = ' '.join(s[:-1])
	price = int(s[-1])
	if k in d:
		d[k] += price
	else:
		d[k] = price
print('\n'.join(('{0} {1}'.format(p[0], p[1]) for p in d.items())))


from collections import OrderedDict
d = OrderedDict()
for _ in range(int(input())):
	(*name, qty) = input().split()
	if ' '.join(name) in d:
		d[' '.join(name)] += int(qty)
	else:
		d[' '.join(name)] = int(qty)
[print(key + ' ' + str(value)) for (key, value) in d.items()]


from collections import OrderedDict
sells = OrderedDict()
n = int(input())
for i in range(n):
	items = input().split()
	name = ' '.join(items[:-1])
	price = int(items[-1])
	if name in sells:
		sells[name] += price
	else:
		sells[name] = price
for e in sells.items():
	print(e[0], e[1])