#!/usr/bin/env python3
"""Hadoop Streaming - reducer zliczania słów (służy też jako combiner).

Wejście przychodzi posortowane po kluczu, więc wystarczy sumować kolejne wiersze
z tym samym słowem i wypisać wynik, gdy słowo się zmieni.
"""
import sys

sys.stdin.reconfigure(encoding='utf-8')
sys.stdout.reconfigure(encoding='utf-8')

current_word = None
current_sum = 0

for line in sys.stdin:
    word, count = line.rstrip('\n').split('\t')
    if word == current_word:
        current_sum += int(count)
    else:
        if current_word is not None:
            sys.stdout.write('%s\t%d\n' % (current_word, current_sum))
        current_word, current_sum = word, int(count)

if current_word is not None:
    sys.stdout.write('%s\t%d\n' % (current_word, current_sum))
