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.s: B = None
        self.y: B = None
        self.txt = txt

f = A("f")
m = B("m")
e = A("e")
d = B("d")
a = B("a")

f.r = e
f.q = e
m.s = d
m.y = a
e.r = f
e.q = f
d.s = a
d.y = m
a.s = d
a.y = m
assert d.s.y.s.y.s.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.s: B = None
        self.y: B = None
        self.txt = txt

f = A("f")
m = B("m")
e = A("e")
d = B("d")
a = B("a")

f.r = e
f.q = e
m.s = d
m.y = a
e.r = f
e.q = f
d.s = a
d.y = m
a.s = d
a.y = m
assert d.s.y.s.y.s.txt == "d"