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

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

j = A("j")
g = B("g")
m = B("m")
a = A("a")

j.q = a
g.u = m
g.p = a
m.u = g
m.p = a
a.q = j
assert a.q.q.q.q.txt == "
class A:
    def __init__(self, txt: str):
        self.q: A = None
        self.txt = txt

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

j = A("j")
g = B("g")
m = B("m")
a = A("a")

j.q = a
g.u = m
g.p = a
m.u = g
m.p = a
a.q = j
assert a.q.q.q.q.txt == "a"