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

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

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

j.r = i
j.q = i
d.u = m
i.r = j
i.q = j
m.u = d
assert m.u.u.u.txt == "
class A:
    def __init__(self, txt: str):
        self.r: A = None
        self.q: A = None
        self.txt = txt

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

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

j.r = i
j.q = i
d.u = m
i.r = j
i.q = j
m.u = d
assert m.u.u.u.txt == "d"