def compare_pair(p1, p2, cp1, cp2):
    """Given comparison functions for the first and second part of
    the pair, compare the two pairs using the two functions under
    lexicographic order.

    """
    res1 = cp1(p1[0], p2[0])
    if res1 != 0:
        return res1
    else:
        return cp2(p1[1], p2[1])