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

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

class C:
    def __init__(self, txt: str):
        self.x: B = None
        self.u: A = None
        self.txt = txt

n = A("n")
g = B("g")
d = C("d")
k = B("k")
h = C("h")
j = A("j")

n.q = d
g.u = k
d.x = k
d.u = j
k.u = g
h.x = g
h.u = n
j.q = h
assert n.q.u.q.u.q.txt == "
class A:
    def __init__(self, txt: str):
        self.q: C = None
        self.txt = txt

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

class C:
    def __init__(self, txt: str):
        self.x: B = None
        self.u: A = None
        self.txt = txt

n = A("n")
g = B("g")
d = C("d")
k = B("k")
h = C("h")
j = A("j")

n.q = d
g.u = k
d.x = k
d.u = j
k.u = g
h.x = g
h.u = n
j.q = h
assert n.q.u.q.u.q.txt == "d"