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

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

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

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

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

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

i.w = b
g.p = b
g.q = i
m.w = g
c.p = g
c.q = d
b.p = c
b.q = i
d.w = c
assert d.w.q.w.q.w.p.txt == "g"