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

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

m = A("m")
j = B("j")
d = B("d")
g = B("g")
a = A("a")

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

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

m = A("m")
j = B("j")
d = B("d")
g = B("g")
a = A("a")

m.w = a
m.r = a
j.q = g
d.q = j
g.q = d
a.w = m
a.r = m
assert m.r.r.w.w.txt == "m"