code to convert NxN matching into top.txt output