```
#include <iostream>
using namespace std;
int tokens[205][25],machines[205][25],arr[8000];
bool yn[205][25];
int main()
{
ios_base::sync_with_stdio(false);
long long int N;
int M,T,S;
cin>>M>>T>>S>>N;
for(int i=1; i<=M; i++)
{
for(int j=1; j<=T; j++)
{
cin>>tokens[i][j]>>machines[i][j];
yn[i][j]=false;
}
}
int a=S,b=1,counter=1;
while(yn[a][b]!=true)
{
arr[counter]=a;
yn[a][b]=true;
counter++;
int temp=b;
b=tokens[a][b];
a=machines[a][temp];
}
counter--;
for(int i=1; i<=M; i++)
{
for(int j=1; j<=T; j++)
{
yn[i][j]=false;
}
}
int m=a,n=b,counter2=0;
a=S;
b=1;
while(yn[a][b]!=true)
{
if(a==m && b==n)
{
break;
}
else
{
yn[a][b]=true;
counter2++;
int temp=b;
b=tokens[a][b];
a=machines[a][temp];
}
}
if(N<=counter2)
{
cout<<arr[N];
}
else
{
if((N-counter2)%(counter-counter2)==0)
{
cout<<arr[counter];
}
else
{
cout<<arr[((N-counter2)%(counter-counter2))+counter2];
}
}
}
```