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

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

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

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

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

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

d.q = c
g.w = a
c.q = d
b.w = f
a.w = f
f.w = g
assert g.w.w.w.w.txt == "a"