func x  = foldl (\x y->[x!!0+y!!0,x!!1+y!!1]) [0,0] ((map (\y->if y!!0>y!!1 then [y!!0+y!!1,0] else if y!!0==y!!1 then [y!!0,y!!1] else [0,y!!0+y!!1]) x))

func' (x:xs) | x/=[0] = (func (fst y)) : (func' (snd y)) 
			 | x==[0] = []
			 	where y = splitAt (x!!0) xs

main = do
	getContents>>=mapM_ putStrLn.map(unwords.(map show)).func'.map(map(read::String->Int).words).lines