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

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

g = A("g")
a = B("a")
k = B("k")
c = B("c")

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

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

g = A("g")
a = B("a")
k = B("k")
c = B("c")

g.z = k
a.q = g
k.q = g
c.q = g
assert g.z.q.z.q.txt == "g"