m@main=getLine>>=print.f.words>>m f[a,b,c]=foldl(\x y->x^10*r a^r[y]`mod`r c)1b r=read