#include <iostream>
using namespace std;

int main() {
    int a0,a1,p,q,k;
    while(cin>>a0>>a1>>p>>q>>k){
        int dp[k+1];
        dp[0]=a0;
        dp[1]=a1;
        for(int i=2;i<=k;i++){
            dp[i]=(p*dp[i-1]%10000+q*dp[i-2]%10000)%10000;
        }
        cout<<dp[k]<<endl;
    }

}