Using unordered set for visitedPairs instead of set