#include <iostream>
using namespace std;
int main() {
long long a0, a1, p, q, k,temp;
cin>>a0>>a1>>p>>q>>k;
for (int i = 0; i <= k-2; i++) {
temp = a1;
a1 = (p * a1 + q * a0)%10000;
a0 = temp;
}
cout<<a1%10000;
return 0;
}