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

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

d = A("d")
b = B("b")
a = A("a")
f = A("f")
g = B("g")

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

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

d = A("d")
b = B("b")
a = A("a")
f = A("f")
g = B("g")

d.q = g
b.r = a
a.q = b
f.q = b
g.r = a
assert a.q.r.q.r.txt == "a"