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

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

g = A("g")
d = B("d")
f = B("f")

g.t = f
d.w = f
d.q = f
f.w = d
f.q = d
assert d.w.q.q.txt == "
class A:
    def __init__(self, txt: str):
        self.t: B = None
        self.txt = txt

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

g = A("g")
d = B("d")
f = B("f")

g.t = f
d.w = f
d.q = f
f.w = d
f.q = d
assert d.w.q.q.txt == "f"