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

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

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

a = A("a")
h = B("h")
c = C("c")
l = A("l")

a.q = l
a.x = l
a.p = c
h.q = c
h.r = c
c.q = h
l.q = a
l.x = a
l.p = c
assert h.q.q.r.txt == "
class A:
    def __init__(self, txt: str):
        self.q: A = None
        self.x: A = None
        self.p: C = None
        self.txt = txt

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

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

a = A("a")
h = B("h")
c = C("c")
l = A("l")

a.q = l
a.x = l
a.p = c
h.q = c
h.r = c
c.q = h
l.q = a
l.x = a
l.p = c
assert h.q.q.r.txt == "c"