class A:
    def __init__(self, txt: str):
        self.x: C = None
        self.txt = txt

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

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

n = A("n")
j = B("j")
f = C("f")
d = B("d")
h = C("h")

n.x = h
j.r = n
j.p = d
j.w = h
f.x = n
f.s = h
d.r = n
d.p = j
d.w = h
h.x = n
h.s = f
assert h.x.x.s.txt == "
class A:
    def __init__(self, txt: str):
        self.x: C = None
        self.txt = txt

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

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

n = A("n")
j = B("j")
f = C("f")
d = B("d")
h = C("h")

n.x = h
j.r = n
j.p = d
j.w = h
f.x = n
f.s = h
d.r = n
d.p = j
d.w = h
h.x = n
h.s = f
assert h.x.x.s.txt == "f"