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

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

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

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

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

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

n.r = a
n.q = g
f.w = g
f.s = a
a.r = n
a.q = f
g.w = f
g.s = n
assert g.s.q.s.r.txt == "a"