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

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

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

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

d.q = n
g.q = k
k.x = d
k.r = d
n.q = k
assert d.q.q.r.txt == "
class A:
    def __init__(self, txt: str):
        self.q: B = None
        self.txt = txt

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

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

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

d.q = n
g.q = k
k.x = d
k.r = d
n.q = k
assert d.q.q.r.txt == "d"