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

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

i = A("i")
d = B("d")
g = A("g")
b = B("b")

i.w = b
d.p = g
g.w = d
b.p = i
assert b.p.w.p.w.txt == "
class A:
    def __init__(self, txt: str):
        self.w: B = None
        self.txt = txt

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

i = A("i")
d = B("d")
g = A("g")
b = B("b")

i.w = b
d.p = g
g.w = d
b.p = i
assert b.p.w.p.w.txt == "b"