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

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

b = A("b")
c = B("c")
g = A("g")
a = A("a")
k = A("k")
i = B("i")
d = A("d")

b.r = a
c.q = g
c.p = i
g.r = a
a.r = g
k.r = b
i.q = b
i.p = c
d.r = g
assert a.r.r.r.r.txt == "
class A:
    def __init__(self, txt: str):
        self.r: A = None
        self.txt = txt

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

b = A("b")
c = B("c")
g = A("g")
a = A("a")
k = A("k")
i = B("i")
d = A("d")

b.r = a
c.q = g
c.p = i
g.r = a
a.r = g
k.r = b
i.q = b
i.p = c
d.r = g
assert a.r.r.r.r.txt == "a"