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

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

a = A("a")
b = B("b")
d = A("d")
k = A("k")
h = A("h")
f = A("f")
c = A("c")
l = A("l")
e = B("e")
i = B("i")

a.z = h
a.q = l
b.r = i
d.z = l
d.q = f
k.z = c
k.q = a
h.z = d
h.q = k
f.z = k
f.q = k
c.z = f
c.q = a
l.z = f
l.q = f
e.r = b
i.r = e
assert i.r.r.r.txt == "
class A:
    def __init__(self, txt: str):
        self.z: A = None
        self.q: A = None
        self.txt = txt

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

a = A("a")
b = B("b")
d = A("d")
k = A("k")
h = A("h")
f = A("f")
c = A("c")
l = A("l")
e = B("e")
i = B("i")

a.z = h
a.q = l
b.r = i
d.z = l
d.q = f
k.z = c
k.q = a
h.z = d
h.q = k
f.z = k
f.q = k
c.z = f
c.q = a
l.z = f
l.q = f
e.r = b
i.r = e
assert i.r.r.r.txt == "i"