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

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

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

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

g = A("g")
e = B("e")
d = C("d")
b = D("b")
i = A("i")
c = C("c")
n = B("n")

g.p = i
e.q = g
d.q = i
d.x = c
d.r = b
b.u = i
i.p = g
c.q = g
c.x = d
c.r = b
n.q = g
assert g.p.p.p.txt == "
class A:
    def __init__(self, txt: str):
        self.p: A = None
        self.txt = txt

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

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

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

g = A("g")
e = B("e")
d = C("d")
b = D("b")
i = A("i")
c = C("c")
n = B("n")

g.p = i
e.q = g
d.q = i
d.x = c
d.r = b
b.u = i
i.p = g
c.q = g
c.x = d
c.r = b
n.q = g
assert g.p.p.p.txt == "i"