A rare article in the Internet is posted without a possibility to comment it. On a Polycarp's website each article has comments feed.

Each comment on Polycarp's website is a non-empty string consisting of uppercase and lowercase letters of English alphabet. Comments have tree-like structure, that means each comment except root comments (comments of the highest level) has exactly one parent comment.

When Polycarp wants to save comments to his hard drive he uses the following format. Each comment he writes in the following format:   at first, the text of the comment is written;  after that the number of comments is written, for which this comment is a parent comment (i. e. the number of the replies to this comments);  after that the comments for which this comment is a parent comment are written (the writing of these comments uses the same algorithm).  All elements in this format are separated by single comma. Similarly, the comments of the first level are separated by comma.

For example, if the comments look like: [Image] 

then the first comment is written as "hello,2,ok,0,bye,0", the second is written as "test,0", the third comment is written as "one,1,two,2,a,0,b,0". The whole comments feed is written as: "hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0". For a given comments feed in the format specified above print the comments in a different format:   at first, print a integer d — the maximum depth of nesting comments;  after that print d lines, the i-th of them corresponds to nesting level i;  for the i-th row print comments of nesting level i in the order of their appearance in the Policarp's comments feed, separated by space. 


-----Input-----

The first line contains non-empty comments feed in the described format. It consists of uppercase and lowercase letters of English alphabet, digits and commas. 

It is guaranteed that each comment is a non-empty string consisting of uppercase and lowercase English characters. Each of the number of comments is integer (consisting of at least one digit), and either equals 0 or does not contain leading zeros.

The length of the whole string does not exceed 10^6. It is guaranteed that given structure of comments is valid. 


-----Output-----

Print comments in a format that is given in the statement. For each level of nesting, comments should be printed in the order they are given in the input.


-----Examples-----
Input
hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0

Output
3
hello test one 
ok bye two 
a b 



Input
a,5,A,0,a,0,A,0,a,0,A,0

Output
2
a 
A a A a A 



Input
A,3,B,2,C,0,D,1,E,0,F,1,G,0,H,1,I,1,J,0,K,1,L,0,M,2,N,0,O,1,P,0

Output
4
A K M 
B F H L N O 
C D G I P 
E J 




-----Note-----

The first example is explained in the statements.

Source: codeforces | Difficulty: MEDIUM_HARD
Tags: String algorithms

Graph traversal

Implementation


Solutions:
import sys
from typing import List
slist = input().split(',')
stack = [1 << 30]
res = [[]]
for s in slist:
	if s.isnumeric():
		stack.append(int(s))
		if len(stack) > len(res):
			res.append([])
	else:
		while stack[-1] == 0:
			stack.pop()
		stack[-1] -= 1
		res[len(stack) - 1].append(s)
while res[-1] == []:
	res.pop()
print(len(res))
for l in res:
	if l:
		print(*l)


import collections

def solve():
	comments = input().split(',')
	comments_by_depth = collections.defaultdict(list)
	current_depth = 0
	current_depth_limit = [10000000]
	cur_cmt = [0] * 2
	for (i, item) in enumerate(comments):
		cur_cmt[i % 2] = item
		if i % 2 == 1:
			while current_depth_limit[-1] == 0:
				current_depth_limit.pop()
				current_depth -= 1
			text = cur_cmt[0]
			child_count = int(cur_cmt[1])
			comments_by_depth[current_depth].append(text)
			current_depth_limit[-1] -= 1
			if child_count > 0:
				current_depth += 1
				current_depth_limit.append(child_count)
	depths = list(sorted(comments_by_depth.keys()))
	if depths:
		print(depths[-1] + 1)
	else:
		print(0)
	for d in depths:
		print(' '.join(comments_by_depth[d]))
solve()


tokens = input().split(',')
depths = []
stk = [len(tokens)]
for i in range(len(tokens) // 2):
	tok = tokens[2 * i]
	children = int(tokens[2 * i + 1])
	y = stk.pop()
	y -= 1
	stk.append(y)
	while len(depths) <= len(stk):
		depths.append([])
	depths[len(stk)].append(tok)
	stk.append(children)
	while stk and stk[-1] == 0:
		stk.pop()
print(len(depths) - 1)
for xs in depths[1:]:
	print(' '.join(xs))


t = input().split(',')
(k, m) = (0, len(t))
(p, d) = ([m] * m, [[] for i in range(m)])
for j in range(0, m, 2):
	while p[k] < 1:
		k -= 1
	p[k] -= 1
	d[k].append(t[j])
	k += 1
	p[k] = int(t[j + 1])
while d[k]:
	k += 1
print(k)
for q in d[:k]:
	print(*q)


v = input().strip().split(',')
v = [(v[i * 2], int(v[i * 2 + 1])) for i in range(len(v) // 2)]
n = len(v)
f = {}
s = [1000000]
for i in range(n):
	(x, m) = v[i]
	d = len(s)
	if d not in f:
		f[d] = []
	f[d].append(x)
	s.append(m)
	while s[-1] <= 0:
		s.pop()
		s[-1] -= 1
r = sorted([(k, v) for (k, v) in list(f.items())])
print(len(r))
for i in range(len(r)):
	print(' '.join(r[i][1]))


from collections import defaultdict

def ri():
	return map(int, input().split())
temp = input().split(',')
node = temp[0::2]
count = temp[1::2]
count = list(map(int, count))
level = defaultdict(list)
stack = []
l = 0
for j in range(len(node)):
	level[l].append(j)
	if count[j]:
		count[j] -= 1
		stack.append(j)
		l += 1
	else:
		while len(stack):
			if count[stack[-1]] == 0:
				stack.pop()
				l -= 1
			else:
				count[stack[-1]] -= 1
				break
print(len(level))
for l in level:
	print(' '.join([node[i] for i in level[l]]))


info = input().split(',')
info = [(word, int(num)) for (word, num) in zip(info[::2], info[1::2])]
out = []
stack = []
for (index, item) in enumerate(info):
	level = len(stack)
	while level > 0 and stack[-1] == 0:
		stack.pop()
		level -= 1
	if len(out) <= level:
		out.append([item[0]])
	else:
		out[level].append(item[0])
	if item[1] > 0:
		stack.append(item[1])
	if level > 0:
		stack[level - 1] -= 1
print(len(out))
print('\n'.join((' '.join(line) for line in out)))


comments = input().split(',')
n = len(comments) // 2
stack = [0]
lvl = 1
res = []
for i in range(n):
	(comm, child) = (comments[2 * i], int(comments[2 * i + 1]))
	while lvl > 1 and stack[lvl - 1] == 0:
		lvl -= 1
	stack[lvl - 1] -= 1
	if len(res) == lvl - 1:
		res.append([comm])
	else:
		res[lvl - 1].append(comm)
	if child > 0:
		if lvl == len(stack):
			stack.append(child)
		else:
			stack[lvl] = child
		lvl += 1
print(len(res))
for l in res:
	print(' '.join(l))


import sys
s = sys.stdin.readline().strip().split(',')
n = len(s) // 2
levels = [[]]
stack = []
for i in range(n):
	st = s[i * 2]
	if len(stack) + 1 > len(levels):
		levels.append([])
	levels[len(stack)].append(st)
	if len(stack):
		stack[-1] -= 1
	nm = int(s[i * 2 + 1])
	stack.append(nm)
	while len(stack) and stack[-1] == 0:
		stack.pop()
sys.stdout.write('{}\n'.format(len(levels)) + '\n'.join([' '.join(x) for x in levels]) + '\n')
sys.stdout.flush()


from collections import defaultdict
s = [i for i in input().split(',')]
arr = []
n = len(s)
lvl = 0
levels = []
for i in range(1, n, 2):
	if not arr:
		arr.append(s[i - 1])
		arr.append(int(s[i]))
		levels.append([s[i - 1], lvl])
		lvl += 1
	else:
		while arr and arr[-1] == 0:
			arr.pop()
			arr.pop()
			lvl -= 1
		if arr:
			arr[-1] -= 1
		arr.append(s[i - 1])
		arr.append(int(s[i]))
		levels.append([s[i - 1], lvl])
		lvl += 1
d = defaultdict(list)
ans = 0
for (st, val) in levels:
	ans = max(ans, val)
	d[val].append(st)
print(ans + 1)
for i in range(ans + 1):
	print(*d[i])


comments = input().split(',')
n = len(comments)
p = [n] * n
ans = [[] for i in range(n)]
layer = 0
for i in range(0, n, 2):
	com = comments[i]
	sub_count = int(comments[i + 1])
	while p[layer] == 0:
		layer -= 1
	p[layer] -= 1
	ans[layer].append(com)
	layer += 1
	p[layer] = sub_count
while ans[layer]:
	layer += 1
print(layer)
for sub_ans in ans[:layer]:
	print(*sub_ans)


t = input().split(',')
w = t[::2]
c = list(map(lambda x: int(x), t[1::2]))
max_d = 0
cur_d = 0
cnt = [10 ** 6]
ans = [[] for i in range(10 ** 6)]
i = 0
while i < len(w):
	ans[cur_d].append(w[i])
	cnt[cur_d] -= 1
	if c[i] > 0:
		cnt.append(c[i])
		cur_d += 1
		max_d = max(max_d, cur_d)
	else:
		while cnt[cur_d] == 0:
			cnt.pop()
			cur_d -= 1
	i += 1
print(max_d + 1)
for i in range(max_d + 1):
	print(' '.join(ans[i]))


a = input().split(',')
o = 0
for i in range(1, len(a), 2):
	a[i] = int(a[i])
b = [[] for i in range(300000)]
c = []
for i in range(0, len(a), 2):
	b[len(c)] += [a[i]]
	if a[i + 1] == 0:
		if len(c) != 0:
			c[len(c) - 1] -= 1
	else:
		if len(c) != 0:
			c[len(c) - 1] -= 1
		c += [a[i + 1]]
	while len(c) > 0 and c[len(c) - 1] == 0:
		c.pop()
	o = max(o, len(c))
print(o + 1)
for i in range(300000):
	if b[i] == []:
		break
	else:
		print(*b[i])


import sys
input = sys.stdin.readline
stuff = input().split(',')
n = len(stuff)
comm = [[] for _ in range(1000001)]
st = [1000001]
for i in range(0, n, 2):
	while st[-1] == 0:
		st.pop(-1)
	comm[len(st)].append(stuff[i])
	st[-1] -= 1
	st.append(int(stuff[i + 1]))
maxd = 0
for i in range(1000000, 0, -1):
	if len(comm[i]):
		maxd = i
		break
print(maxd)
for i in range(1, maxd + 1):
	print(' '.join(comm[i]))


a = input().split(',')
n = len(a)
comm = [[] for _ in range(1000001)]
st = [1000001]
for i in range(0, n, 2):
	while st[-1] == 0:
		st.pop(-1)
	comm[len(st)].append(a[i])
	st[-1] -= 1
	st.append(int(a[i + 1]))
maxd = 0
for i in range(1000000, 0, -1):
	if len(comm[i]):
		maxd = i
		break
print(maxd)
for i in range(1, maxd + 1):
	print(' '.join(comm[i]))


levels = []
left = [-1]
inp = input().split(',')
i = 0
while i < len(inp):
	if len(levels) <= len(left) - 1:
		levels.append([])
	t = inp[i]
	n = int(inp[i + 1])
	levels[len(left) - 1].append(t)
	if n > 0:
		left.append(n)
	while left[-1] == 0:
		del left[-1]
	left[-1] -= 1
	i += 2
print(len(levels))
for i in levels:
	print(' '.join(i))


ls = [[] for _ in range(10 ** 6 // 4)]
maxd = 0
s = input().split(',')
i = 0
stack = [1000000]
for i in range(0, len(s), 2):
	(w, c) = (s[i], int(s[i + 1]))
	ls[len(stack) - 1].append(w)
	stack[-1] -= 1
	stack.append(c)
	while stack[-1] == 0:
		stack.pop()
	maxd = max(maxd, len(stack))
print(maxd)
for i in range(maxd):
	print(*ls[i])


from queue import Queue

def parse(s):
	A = s.split(',')
	n = len(A) // 2
	B = [(A[2 * i], int(A[2 * i + 1])) for i in range(n)]
	stack = []
	E = {i: [] for i in range(n + 1)}
	comment = {}
	depth = {i: 0 for i in range(n + 1)}
	for (i, b) in enumerate(reversed(B)):
		comment[i + 1] = b[0]
		for j in range(b[1]):
			k = stack.pop()
			E[i + 1].append(k)
			depth[i + 1] = max(depth[i + 1], depth[k])
		depth[i + 1] += 1
		stack.append(i + 1)
	E[0].extend(reversed(stack))
	depth[0] = max((depth[i] for i in stack)) + 1
	comment[0] = None
	Q = Queue()
	Q.put(0)
	res = [[] for i in range(depth[0])]
	while not Q.empty():
		v = Q.get()
		d = depth[v] - 1
		res[d].append(comment[v])
		for u in E[v]:
			depth[u] = d
			Q.put(u)
	res.pop()
	return res
s = input().rstrip()
res = parse(s)
print(len(res))
for layer in reversed(res):
	print(' '.join(layer))


text = input()
depth = 0
h_text = []
cnt = [1000000]
for s in text.split(','):
	try:
		k = int(s)
	except:
		while cnt[depth] == 0:
			depth -= 1
		cnt[depth] -= 1
		if depth == len(h_text):
			h_text.append([])
		h_text[depth].append(s)
		depth += 1
	else:
		if depth == len(cnt):
			cnt.append(0)
		cnt[depth] = k
print(len(h_text))
for row in h_text:
	print(*row)


line = input()
line = line.split(',')
A = 'abcdefghijklmnopqrstuvwxyz'
depth = 1
comments = [[]]
(cur_cnt, cur_depth) = ([0], 1)
for token in line:
	if token[0] in A or token[0] in A.upper():
		comments[cur_depth - 1].append(token)
		cur_cnt[cur_depth - 1] -= 1
	else:
		cnt = int(token)
		if cnt > 0:
			cur_depth += 1
			if cur_depth > len(cur_cnt):
				cur_cnt.append(0)
			cur_cnt[cur_depth - 1] = cnt
			if cur_depth > depth:
				comments.append([])
				depth = max(cur_depth, depth)
		else:
			while cur_depth > 1 and cur_cnt[cur_depth - 1] == 0:
				cur_depth -= 1
print(len(comments))
for line in comments:
	print(' '.join(line))


t = input().split(',')
(k, m) = (0, len(t))
(p, d) = ([m] * m, [[] for i in range(m)])
for (s, n) in zip(t[::2], t[1::2]):
	while not p[k]:
		k -= 1
	p[k] -= 1
	d[k].append(s)
	k += 1
	p[k] = int(n)
while d[k]:
	k += 1
print(k)
for q in d[:k]:
	print(*q)