#!/usr/bin/python

import sys

table = dict()
header = list()
offset = 0
for i in sys.argv[1:]:
	tsv = open(i)
	header += tsv.readline().rstrip().split("\t")[1:]
	for line in tsv:
		fields = line.rstrip().split("\t")
		rowid  = fields[0]
		if rowid not in table:
			table[rowid] = ["0"] * len(header)
		else:
			table[rowid] += ["0"] * (len(fields) - 1)

		for cell in range(1,len(fields)):
			if fields[cell] == "":
				fields[cell] = "0"
			table[rowid][cell + offset - 1] = fields[cell]

	tsv.close()
	offset += len(fields) - 1

	for i in table:
		if len(table[i]) < len(header):
			table[i] += ["0"] * (len(header) - len(table[i]))

print "\t".join(header)
for i in table:
	print i + "\t" + "\t".join(table[i])
