  This paper is a continuation of math.QA/9907181 and math.QA/9908115. We
consider traces of intertwiners between certain representations of the
quantized enveloping algebra associated to a semisimple complex Lie algebra g,
which are twisted by a ``generalized Belavin-Drinfeld triple'', i.e a triple
consisting of two subdiagrams of the Dynkin diagram of g together with an
isomorphism between them. The generating functions F(lambda,mu) for such traces
depend on two weights lambda and mu. We show that F(lambda,mu) satisfy two sets
of difference equations in the variable lambda: the Macdonald-Ruijsenaars (MR)
equations and the quantum Knizhnik-Zamolodchikov
  (qKZB) equations. These equations involve as a main ingredient the quantum
dynamical R-matrices constructed in math.QA/9912009. When the generalized
Belavin-Drinfeld triple is an automorphism, we show that F(lambda,mu) satisfy
another two sets of difference equations with respect to the weight mu. These
dual MR and dual qKZB equations involve the usual Felder's dynamical R-matrix.
These results were first obtained by the first author and A. Varchenko in the
special case of the trivial Belavin-Drinfeld triple. However, the symmetry
between lambda and mu which exists in that case is destroyed in the twisted
setting. At the end, we brielfly treat the (simialr) case of Kac-Moody algebras
g and derive the classical limits of all the previous results.
