#include <stdio.h>
#include <malloc.h>
int cal(int a,int b,long n);/*计算f(n)的值*/
main(){
/*初始化数值,建立链表*/
struct result{
int result;
struct result *next;
};
int a=1;
int b=1;
int c=0;
long n=0;
int i=0;
struct result *p1,*p2,*p;
p=p1=p2=(struct result*)malloc(sizeof(struct result));
scanf("%d%d%ld",&a,&b,&n);
if(a<1||a>1000||b<1||b>1000||n<1||n>100000000)
exit(0);
if(a==0&&b==0&&n==0)
exit(0);
else
p1->result=cal(a,b,n);
/*用链表存放每行数据得出的结果*/
while(a!=0||b!=0||n!=0){
c++;
if(c==1) p=p1;
else p2->next=p1;
p2=p1;
p1=(struct result*)malloc(sizeof(struct result));
scanf("%d%d%ld",&a,&b,&n);
if(a<1||a>1000||b<1||b>1000||n<1||n>100000000)
exit(0);
p1->result=cal(a,b,n);
}
/*输出链表*/
for(i=0;i<c;i++){
printf("%d\n",p->result);
p=p->next;
}
}
/*计算f(n)的值*/
int cal(int a,int b,long n){
int f;
if(n==1)
f=1;
if(n==2)
f=1;
if(n>=3)
f=(a*cal(a,b,n-1)+b*cal(a,b,n-2))%7;
return f;
}