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

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

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

a.x = j
a.w = b
g.y = b
n.x = j
n.w = g
j.x = n
j.w = b
b.y = f
f.y = b
assert a.w.y.y.y.txt == "
class A:
    def __init__(self, txt: str):
        self.x: A = None
        self.w: B = None
        self.txt = txt

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

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

a.x = j
a.w = b
g.y = b
n.x = j
n.w = g
j.x = n
j.w = b
b.y = f
f.y = b
assert a.w.y.y.y.txt == "f"