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

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

g = A("g")
a = B("a")
e = A("e")
f = A("f")
c = B("c")
m = A("m")

g.q = m
g.v = f
a.r = f
e.q = f
e.v = m
f.q = e
f.v = g
c.r = g
m.q = g
m.v = e
assert a.r.v.v.v.v.txt == "
class A:
    def __init__(self, txt: str):
        self.q: A = None
        self.v: A = None
        self.txt = txt

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

g = A("g")
a = B("a")
e = A("e")
f = A("f")
c = B("c")
m = A("m")

g.q = m
g.v = f
a.r = f
e.q = f
e.v = m
f.q = e
f.v = g
c.r = g
m.q = g
m.v = e
assert a.r.v.v.v.v.txt == "f"