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

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

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

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

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

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

l.p = a
l.q = a
m.q = l
a.p = l
a.q = g
g.p = a
g.q = a
assert m.q.p.p.q.txt == "a"