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

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

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

l = A("l")
g = B("g")
j = C("j")
k = A("k")
h = B("h")

l.p = k
g.v = h
g.x = l
j.r = l
j.z = h
k.p = l
h.v = g
h.x = k
assert j.z.x.p.txt == "
class A:
    def __init__(self, txt: str):
        self.p: A = None
        self.txt = txt

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

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

l = A("l")
g = B("g")
j = C("j")
k = A("k")
h = B("h")

l.p = k
g.v = h
g.x = l
j.r = l
j.z = h
k.p = l
h.v = g
h.x = k
assert j.z.x.p.txt == "l"