Merge similar content according to precedence on output