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

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

f = A("f")
g = B("g")
a = A("a")

f.r = a
f.p = a
g.s = f
a.r = f
a.p = f
assert a.p.p.r.txt == "
class A:
    def __init__(self, txt: str):
        self.r: A = None
        self.p: A = None
        self.txt = txt

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

f = A("f")
g = B("g")
a = A("a")

f.r = a
f.p = a
g.s = f
a.r = f
a.p = f
assert a.p.p.r.txt == "f"