#include <bits/stdc++.h>
#define MAX 100
using namespace std;

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

}