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

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

class C:
    def __init__(self, txt: str):
        self.v: D = None
        self.txt = txt

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

g = A("g")
b = B("b")
k = C("k")
h = D("h")
f = A("f")

g.z = f
b.u = k
b.q = k
b.r = h
b.x = g
k.v = h
h.u = g
h.x = k
h.o = b
f.z = g
assert g.z.z.z.z.z.txt == "
class A:
    def __init__(self, txt: str):
        self.z: A = None
        self.txt = txt

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

class C:
    def __init__(self, txt: str):
        self.v: D = None
        self.txt = txt

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

g = A("g")
b = B("b")
k = C("k")
h = D("h")
f = A("f")

g.z = f
b.u = k
b.q = k
b.r = h
b.x = g
k.v = h
h.u = g
h.x = k
h.o = b
f.z = g
assert g.z.z.z.z.z.txt == "f"