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

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

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

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

j = A("j")
b = B("b")
a = C("a")
c = D("c")
e = D("e")
g = C("g")

j.p = g
b.y = e
b.z = g
b.r = c
a.p = g
a.r = c
c.w = j
e.w = j
g.p = a
g.r = e
assert g.r.w.p.p.r.txt == "
class A:
    def __init__(self, txt: str):
        self.p: C = None
        self.txt = txt

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

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

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

j = A("j")
b = B("b")
a = C("a")
c = D("c")
e = D("e")
g = C("g")

j.p = g
b.y = e
b.z = g
b.r = c
a.p = g
a.r = c
c.w = j
e.w = j
g.p = a
g.r = e
assert g.r.w.p.p.r.txt == "c"