#include <iostream>
using namespace std;
int main() {
    int a0,a1,p,q,k;
    cin>>a0>>a1>>p>>q>>k;
    int arr[k+1];
    arr[0] = a0;
    arr[1] = a1;
    for(int i =2;i<=k;i++){
        arr[i] = (p*arr[i-1]+q*arr[i-2])%10000;
    }
    cout<<arr[k]<<endl;
}
// 64 位输出请用 printf("%lld")