Let $w(a,b)$ be a word in two letter alphabet. Let $$A=\left(\begin{array}{lll}x_1 & x_2 & x_3\\\ x_4 &x_5 & x_6\\\ x_7 & x_8 & x_9\end{array}\right), B=\left(\begin{array}{lll}y_1 & y_2 & y_3\\\ y_4 &y_5 & y_6\\\ y_7 & y_8 & y_9\end{array}\right)$$ where $x_i,y_i$ are commuting variables. Let $f_w=\mathrm{trace}(w(A,B))$, a polynomial in 18 variables.
Question. Is it possible to reconstruct $w$ up to a cyclic shift from $f_w$?
Note that there exists a polynomial in one variable that encodes $w$: $x^{p_1}+...+x^{p_s}+x^{|w|}$ where $p_1,...,p_s$ are the places where $a$ occurs in $w$. Also note that for 2 by 2 matrices the answer is "no". For example if $w=abbaba$ and $w'=ababba$, then $f_w=f_{w'}$ for 2 by 2 matrices. The question is related to the study of the moduli space of representations (of degree 3) of the free group.
Update I think that as George suggested below, one can assume that $A=\mathrm{diag}(a,b,c)$ is a diagonal matrix (otherwise consider a conjugate of the pair $A,B$ over some algebraically complete field). After that the problem reduces to the following problem which seems longer but is in fact easier because we reduce the number of variables to from 18 to 3:
Pick a natural number $n\gg 1$. For every cyclic sequence $p$ (i.e. $p_{n+1}=p_1$) of $\{1,2,3\}$ of length $n$ consider a 9-vector $\phi(p)=($number of occurrences of 11, number of occurrences of 12, ..., number of occurrences of 33$)\in \mathbb{N}^9$. The sum of coordinates of $\phi(p)$ is $n$, so we get a partition of $n$, and the number of different $\phi(p)$ is at most the number of partitions of $n$ into 9 parts, so less than $n^9$. Thus the map $\phi$ has a non-trivial kernel $\mathrm{Ker}(\phi)$ (i.e. the equivalence relation $p\equiv q$ iff $\phi(p)=\phi(q)$ ). Let $S$ be a preimage of a point in $\mathbb{N}^9$ under $\phi$. Let $v=(v_1,...,v_n)$ be a cyclic vector of natural numbers (including 0). For every $p\in S$ consider the monomial $m_p=a^sb^tc^u$ in 3 variables where $s$ is the sum of $v_i$ such that $p_i=1$, $t$ is the sum of $v_i$ such that $p_i=2$, $u$ is the sum of $v_i$ such that $p_i=3$. The sum of all the monomials $m_p$, $p\in S$, is a polynomial $f_S(v)$ in $a,b,c$.
Question Is the sequence $v$ determined by the sequence of polynomials $f_S(v)$ where $S$ runs over the equivalence classes of the partition $\mathrm{Ker}(\phi)$.