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

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

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

class D:
    def __init__(self, txt: str):
        self.t: A = None
        self.o: A = None
        self.v: B = None
        self.txt = txt

b = A("b")
g = B("g")
m = C("m")
n = D("n")

b.p = g
g.p = n
m.w = g
m.v = b
m.y = b
n.t = b
n.o = b
n.v = g
assert g.p.o.p.txt == "
class A:
    def __init__(self, txt: str):
        self.p: B = None
        self.txt = txt

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

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

class D:
    def __init__(self, txt: str):
        self.t: A = None
        self.o: A = None
        self.v: B = None
        self.txt = txt

b = A("b")
g = B("g")
m = C("m")
n = D("n")

b.p = g
g.p = n
m.w = g
m.v = b
m.y = b
n.t = b
n.o = b
n.v = g
assert g.p.o.p.txt == "g"