#include<stdio.h>
main(){
int a,b,n,s[1000],i,r,start,k,counted;
while(scanf("%d %d %d",&a,&b,&n)){
s[1]=1;
s[2]=1;
if(a+b+n==0) break;
r=0;
for(i=3;i<1000;i++){
s[i]=(a*s[i-1]+b*s[i-2])%7;
for(k=1;k<i-1;k++)
if(s[i-1]==s[k]&&s[i]==s[k+1]) {r=i-1-k;start=k;counted=i;i=1000;break;}
}
if(n<=counted) printf("%d\n",s[n]);
else{
n=n-start+1;
n=n%r;
s[start-1]=s[start+r-1];
printf("%d\n",s[start+n-1]);
}
}
}