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

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

m = A("m")
k = B("k")
i = A("i")
d = A("d")
a = B("a")

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

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

m = A("m")
k = B("k")
i = A("i")
d = A("d")
a = B("a")

m.z = k
m.q = d
k.p = d
i.z = a
i.q = d
d.z = a
d.q = m
a.p = i
assert k.p.q.q.txt == "d"