def succ(B, T):
    """Return the successor of the tuple T over the base represented by B.
    Returns None if T is the last tuple in the ordering.
    See the library description for how base representations work."""

    Tnew = T[:]
    for i in range(len(B)):
        # Increase the entry in position len(B)-i-1 if possible (i.e. we work
        # right-to-left).
        idx = len(B)-i-1

        if type(B[idx]) == int:
            Tnew[idx] = (Tnew[idx] + 1) % B[idx]

            # If we are not 0, then the increment was successful and we
            # can stop. Otherwise, we reached the end of the base set and
            # had to loop back to the beginning, so we must continue.
            if Tnew[idx] > 0:
                return Tnew

        else:
            # Move to the next position in the list.
            basis, lookup = B[idx]
            Tnew[idx] = basis[(lookup[Tnew[idx]] + 1) % len(basis)]

            # If we re not the first element in the base set, the increment
            # was successful and we can stop. otherwise, we reached the end
            # of the base set and had to loop back to the beginning, so we
            # must continue.
            if Tnew[idx] != basis[0]:
                return Tnew

    # We could not increment anything, and thus were at the end of the list.
    return None