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

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

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

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

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

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

c.r = f
c.p = f
f.q = i
j.r = f
j.p = f
m.r = f
m.p = f
i.q = f
d.r = i
d.p = i
assert f.q.q.q.q.q.q.txt == "f"