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

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

a = A("a")
i = B("i")
d = A("d")
h = A("h")
k = A("k")
g = B("g")

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

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

a = A("a")
i = B("i")
d = A("d")
h = A("h")
k = A("k")
g = B("g")

a.z = g
a.r = h
i.w = g
i.q = k
d.z = i
d.r = h
h.z = g
h.r = k
k.z = i
k.r = h
g.w = i
g.q = a
assert d.r.r.z.w.w.w.txt == "g"