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.y: A = None
        self.x: A = None
        self.txt = txt

a = A("a")
h = B("h")
f = B("f")
m = A("m")
g = A("g")
l = B("l")

a.z = h
a.q = g
h.y = g
h.x = g
f.y = m
f.x = m
m.z = l
m.q = g
g.z = l
g.q = m
l.y = a
l.x = a
assert l.x.q.z.x.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.y: A = None
        self.x: A = None
        self.txt = txt

a = A("a")
h = B("h")
f = B("f")
m = A("m")
g = A("g")
l = B("l")

a.z = h
a.q = g
h.y = g
h.x = g
f.y = m
f.x = m
m.z = l
m.q = g
g.z = l
g.q = m
l.y = a
l.x = a
assert l.x.q.z.x.txt == "a"