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

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

h = A("h")
g = B("g")
k = A("k")
b = B("b")

h.s = k
h.w = b
g.r = k
g.s = h
k.s = h
k.w = g
b.r = k
b.s = k
assert g.r.w.s.txt == "
class A:
    def __init__(self, txt: str):
        self.s: A = None
        self.w: B = None
        self.txt = txt

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

h = A("h")
g = B("g")
k = A("k")
b = B("b")

h.s = k
h.w = b
g.r = k
g.s = h
k.s = h
k.w = g
b.r = k
b.s = k
assert g.r.w.s.txt == "h"