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

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

n = A("n")
i = B("i")
g = A("g")
d = A("d")
k = B("k")

n.x = i
n.q = g
i.o = n
g.x = k
g.q = d
d.x = i
d.q = n
k.o = d
assert i.o.x.o.x.txt == "
class A:
    def __init__(self, txt: str):
        self.x: B = None
        self.q: A = None
        self.txt = txt

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

n = A("n")
i = B("i")
g = A("g")
d = A("d")
k = B("k")

n.x = i
n.q = g
i.o = n
g.x = k
g.q = d
d.x = i
d.q = n
k.o = d
assert i.o.x.o.x.txt == "i"