Letting $quo(x,y)$ be$ x/y$, $lh(s)$ defined to be the length of the sequence,
$ent(s,i)$ the ith member of the sequence coded for by S
$\pi(n)$ the nth prime counting 2 as the $0th$
$$
sub(s,c,d,0):=
\begin{cases}
s & \quad ,fst(s)\neq c\\
\\
qou\left(s,3^c\right)3^d & \quad ,fst(s) = c \\
\end{cases}
$$
$ \\ $
$$
sub(s,c,d,i')=
\begin{cases}
sub(s,c,d,i) & \quad ent(s,i)\neq c \\
\\
quo\bigg(sub(s,c,d,i),\pi(i+1)^c\bigg)\pi(i+1)^d & \quad ent(s,i) = c
\end{cases}
$$
Then $$sub(s,c,d):=sub(s,c,d,lh(s))$$