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

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

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

l.x = h
l.q = a
g.r = a
g.s = a
m.x = g
m.q = a
d.r = f
d.s = f
h.r = m
h.s = m
i.r = l
i.s = f
f.x = g
f.q = m
a.x = h
a.q = l
assert d.s.x.s.q.txt == "
class A:
    def __init__(self, txt: str):
        self.x: B = None
        self.q: A = None
        self.txt = txt

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

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

l.x = h
l.q = a
g.r = a
g.s = a
m.x = g
m.q = a
d.r = f
d.s = f
h.r = m
h.s = m
i.r = l
i.s = f
f.x = g
f.q = m
a.x = h
a.q = l
assert d.s.x.s.q.txt == "l"