Use union-by-rank with a name dictionary.