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

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

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

class D:
    def __init__(self, txt: str):
        self.z: D = None
        self.txt = txt

b = A("b")
a = B("a")
f = C("f")
j = D("j")
h = B("h")
d = D("d")

b.y = a
a.y = j
f.p = a
f.w = j
j.z = d
h.y = j
d.z = j
assert a.y.z.z.z.z.z.txt == "
class A:
    def __init__(self, txt: str):
        self.y: B = None
        self.txt = txt

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

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

class D:
    def __init__(self, txt: str):
        self.z: D = None
        self.txt = txt

b = A("b")
a = B("a")
f = C("f")
j = D("j")
h = B("h")
d = D("d")

b.y = a
a.y = j
f.p = a
f.w = j
j.z = d
h.y = j
d.z = j
assert a.y.z.z.z.z.z.txt == "d"