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

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

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

a.q = i
a.x = i
i.z = g
i.p = g
g.q = i
g.x = i
assert a.x.p.q.txt == "
class A:
    def __init__(self, txt: str):
        self.q: B = None
        self.x: B = None
        self.txt = txt

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

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

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