from collections import deque

def well_connections(wells):
    common = set.intersection(*({name for *_, name in well} for well in wells))
    wells = list(map(deque, wells))
    this = 0
    while all(wells):
        if all(well[0][2] in common for well in wells):
            yield tuple(well[0][1] for well in wells)
            if len(wells[this]) > 1 and wells[this][1][2] in common:
                this = 1 - this
            wells[this].popleft()
        while wells[this] and wells[this][0][2] not in common:
            yield tuple(well[0][1] for well in wells)
            wells[this].popleft()
        this = 1 - this
        wells[this].popleft()

wells = [
    [
        [0, 0.4, 'pop'],
        [0.4, 0.8, 'sup'],
        [0.8, 1.2, 'muka'],
        [1.2, 2, 'sand'],
        [2, 2.4, 'buz']
    ],
    [
        [0, 0.4, 'pop'],
        [0.4, 1.6, 'sand'],
        [1.6, 2, 'graviy'],
        [2, 3.2, 'galka'],
        [3.2, 3.6, 'buz']
    ]
]
print(list(well_connections(wells)))