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

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

a = A("a")
b = B("b")
h = A("h")
f = B("f")

a.q = b
b.y = f
h.q = f
f.y = b
assert f.y.y.y.txt == "
class A:
    def __init__(self, txt: str):
        self.q: B = None
        self.txt = txt

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

a = A("a")
b = B("b")
h = A("h")
f = B("f")

a.q = b
b.y = f
h.q = f
f.y = b
assert f.y.y.y.txt == "b"