solution for pairs