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

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

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

n = A("n")
g = B("g")
d = C("d")
h = A("h")
b = B("b")
f = C("f")

n.r = d
n.z = h
n.y = g
g.p = n
d.q = n
h.r = d
h.z = n
h.y = g
b.p = h
f.q = n
assert b.p.r.q.r.txt == "
class A:
    def __init__(self, txt: str):
        self.r: C = None
        self.z: A = None
        self.y: B = None
        self.txt = txt

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

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

n = A("n")
g = B("g")
d = C("d")
h = A("h")
b = B("b")
f = C("f")

n.r = d
n.z = h
n.y = g
g.p = n
d.q = n
h.r = d
h.z = n
h.y = g
b.p = h
f.q = n
assert b.p.r.q.r.txt == "d"