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

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

g = A("g")
n = B("n")
h = A("h")

g.z = h
n.q = g
h.z = g
assert n.q.z.z.txt == "
class A:
    def __init__(self, txt: str):
        self.z: A = None
        self.txt = txt

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

g = A("g")
n = B("n")
h = A("h")

g.z = h
n.q = g
h.z = g
assert n.q.z.z.txt == "g"