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

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

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

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

a = A("a")
k = B("k")
c = C("c")
j = D("j")
h = A("h")

a.q = h
k.o = c
k.r = c
k.v = j
c.y = k
c.p = k
c.u = k
j.p = k
h.q = a
assert j.p.v.p.v.p.txt == "
class A:
    def __init__(self, txt: str):
        self.q: A = None
        self.txt = txt

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

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

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

a = A("a")
k = B("k")
c = C("c")
j = D("j")
h = A("h")

a.q = h
k.o = c
k.r = c
k.v = j
c.y = k
c.p = k
c.u = k
j.p = k
h.q = a
assert j.p.v.p.v.p.txt == "k"