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

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

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

d.w = g
d.x = f
a.w = g
f.w = d
f.x = d
g.w = f
g.x = d
assert f.x.w.w.x.txt == "
class A:
    def __init__(self, txt: str):
        self.w: A = None
        self.x: A = None
        self.txt = txt

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

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

d.w = g
d.x = f
a.w = g
f.w = d
f.x = d
g.w = f
g.x = d
assert f.x.w.w.x.txt == "d"