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.z: B = None
        self.txt = txt

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

g.p = a
g.q = a
m.z = i
i.z = m
a.p = g
a.q = g
assert a.q.q.q.p.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.z: B = None
        self.txt = txt

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

g.p = a
g.q = a
m.z = i
i.z = m
a.p = g
a.q = g
assert a.q.q.q.p.txt == "a"