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

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

f = A("f")
e = B("e")
a = A("a")

f.x = a
f.y = a
e.q = a
e.v = f
a.x = f
a.y = f
assert a.y.y.x.txt == "
class A:
    def __init__(self, txt: str):
        self.x: A = None
        self.y: A = None
        self.txt = txt

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

f = A("f")
e = B("e")
a = A("a")

f.x = a
f.y = a
e.q = a
e.v = f
a.x = f
a.y = f
assert a.y.y.x.txt == "f"