pairs matrix implementation