Let $$C= \begin{pmatrix} A & 0 \\0 & B \\
\end{pmatrix} \,.$$
Then $C$ is invertible with inverse $C^{-1}= \begin{pmatrix} A^{-1} & 0 \\0 & B^{-1} \\
\end{pmatrix} \,.$
Let $$P(x)=x^{2n}+a_{2n-1}x^{2n-1}+\dots+a_0$$ be the characteristic polynomial of $C$. Then, since $C$ is invertible we have $a_0 \neq 0$.
Thus
$$I_{2n}=-\frac{1}{a_0}\left[ C^{2n}+a_{2n-1}C^{2n-1}+\dots+a_1C\right] \,.$$
Let $$Q(x)=-\frac{1}{a_0} \left[ x^{2n}+a_{2n-1}x^{2n-1}+\dots+a_1x\right] \,.$$
Since $Q(C)=I_{2n}$ from the definition of $C$ we get $Q(A)=Q(B)=I_n$.
From $A^kX=B^kY, \forall 1 \leq k \leq 2n$ we get $Q(A)X=Q(B)Y$, thus $X=Y$:
$$X=I_nX=Q(A)X=-\frac{1}{a_0} \left[ A^{2n}X+a_{2n-1}A^{2n-1}X+\dots+a_1AX\right]=-\frac{1}{a_0}\left[ B^{2n}Y+a_{2n-1}B^{2n-1}Y+\dots+a_1BY\right]=Q(B)Y=I_nY=Y \,.$$