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

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

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

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

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

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

j.q = g
j.y = f
a.r = f
i.q = f
i.y = f
f.r = g
g.r = f
assert j.q.r.r.r.txt == "f"