class A:
    def __init__(self, txt: str):
        self.z: A = None
        self.txt = txt

class B:
    def __init__(self, txt: str):
        self.y: A = None
        self.txt = txt

n = A("n")
d = B("d")
h = A("h")

n.z = h
d.y = h
h.z = n
assert d.y.z.z.txt == "
class A:
    def __init__(self, txt: str):
        self.z: A = None
        self.txt = txt

class B:
    def __init__(self, txt: str):
        self.y: A = None
        self.txt = txt

n = A("n")
d = B("d")
h = A("h")

n.z = h
d.y = h
h.z = n
assert d.y.z.z.txt == "h"