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

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

g = A("g")
f = B("f")
a = B("a")
n = A("n")

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

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

g = A("g")
f = B("f")
a = B("a")
n = A("n")

g.w = f
g.z = n
f.q = a
a.q = f
n.w = f
n.z = g
assert g.z.w.q.q.txt == "f"