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

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

g = A("g")
m = B("m")
d = A("d")
a = B("a")

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

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

g = A("g")
m = B("m")
d = A("d")
a = B("a")

g.z = m
m.q = d
m.w = g
d.z = a
a.q = g
a.w = g
assert d.z.q.z.w.txt == "g"