Added a check in merge_sets(first, second) which ensures that if first == second then the element first should exist in the map