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

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

class C:
    def __init__(self, txt: str):
        self.w: C = None
        self.s: C = None
        self.p: A = None
        self.txt = txt

l = A("l")
g = B("g")
m = C("m")
d = C("d")
f = B("f")

l.p = m
l.w = f
g.r = l
g.q = l
m.w = d
m.s = d
m.p = l
d.w = m
d.s = m
d.p = l
f.r = l
f.q = l
assert d.w.p.w.r.w.txt == "
class A:
    def __init__(self, txt: str):
        self.p: C = None
        self.w: B = None
        self.txt = txt

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

class C:
    def __init__(self, txt: str):
        self.w: C = None
        self.s: C = None
        self.p: A = None
        self.txt = txt

l = A("l")
g = B("g")
m = C("m")
d = C("d")
f = B("f")

l.p = m
l.w = f
g.r = l
g.q = l
m.w = d
m.s = d
m.p = l
d.w = m
d.s = m
d.p = l
f.r = l
f.q = l
assert d.w.p.w.r.w.txt == "f"