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

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

a = A("a")
g = B("g")
m = A("m")
f = B("f")
j = B("j")

a.y = f
g.y = j
g.q = m
m.y = g
f.y = g
f.q = a
j.y = f
j.q = m
assert f.y.q.y.y.txt == "
class A:
    def __init__(self, txt: str):
        self.y: B = None
        self.txt = txt

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

a = A("a")
g = B("g")
m = A("m")
f = B("f")
j = B("j")

a.y = f
g.y = j
g.q = m
m.y = g
f.y = g
f.q = a
j.y = f
j.q = m
assert f.y.q.y.y.txt == "j"