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

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

d = A("d")
m = B("m")
i = B("i")
h = A("h")

d.s = h
d.x = h
m.q = i
i.q = m
h.s = d
h.x = d
assert m.q.q.q.q.txt == "
class A:
    def __init__(self, txt: str):
        self.s: A = None
        self.x: A = None
        self.txt = txt

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

d = A("d")
m = B("m")
i = B("i")
h = A("h")

d.s = h
d.x = h
m.q = i
i.q = m
h.s = d
h.x = d
assert m.q.q.q.q.txt == "m"