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

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

f = A("f")
c = B("c")
m = A("m")
a = A("a")

f.x = a
f.q = m
c.r = a
c.o = f
m.x = a
m.q = f
a.x = f
a.q = f
assert f.x.q.q.txt == "
class A:
    def __init__(self, txt: str):
        self.x: A = None
        self.q: A = None
        self.txt = txt

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

f = A("f")
c = B("c")
m = A("m")
a = A("a")

f.x = a
f.q = m
c.r = a
c.o = f
m.x = a
m.q = f
a.x = f
a.q = f
assert f.x.q.q.txt == "m"