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

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

n = A("n")
b = B("b")
g = B("g")
i = A("i")
f = B("f")

n.r = b
b.s = i
b.q = n
g.s = n
g.q = i
i.r = g
f.s = i
f.q = i
assert n.r.q.r.s.txt == "
class A:
    def __init__(self, txt: str):
        self.r: B = None
        self.txt = txt

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

n = A("n")
b = B("b")
g = B("g")
i = A("i")
f = B("f")

n.r = b
b.s = i
b.q = n
g.s = n
g.q = i
i.r = g
f.s = i
f.q = i
assert n.r.q.r.s.txt == "i"