#include <iostream>

using namespace std;

const int N = 1e5 + 10;
int a[N];

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