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

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

m = A("m")
h = B("h")
f = A("f")
j = A("j")
b = B("b")
d = B("d")
l = B("l")
c = A("c")

m.s = f
h.q = d
f.s = m
j.s = f
b.q = d
d.q = b
l.q = b
c.s = m
assert d.q.q.q.q.txt == "
class A:
    def __init__(self, txt: str):
        self.s: A = None
        self.txt = txt

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

m = A("m")
h = B("h")
f = A("f")
j = A("j")
b = B("b")
d = B("d")
l = B("l")
c = A("c")

m.s = f
h.q = d
f.s = m
j.s = f
b.q = d
d.q = b
l.q = b
c.s = m
assert d.q.q.q.q.txt == "d"