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

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

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

d.x = a
d.w = f
f.z = b
f.q = b
b.z = f
b.q = f
a.x = d
a.w = f
assert f.q.q.z.txt == "
class A:
    def __init__(self, txt: str):
        self.x: A = None
        self.w: B = None
        self.txt = txt

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

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

d.x = a
d.w = f
f.z = b
f.q = b
b.z = f
b.q = f
a.x = d
a.w = f
assert f.q.q.z.txt == "b"