#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#define int long long
using namespace std;
inline int read()
{
int x=0,w=1;
char ch=0;
while(ch<'0'||ch>'9')
{
if(ch=='-') w=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=(x<<1)+(x<<3)+(ch^48);
ch=getchar();
}
return x*w;
}
inline void write(int x)
{
if(x<0)
{
x=-x;
putchar('-');
}
if(x>=10) write(x/10);
putchar((x%10)^48);
}
inline char change(int op,char x)
{
if(op==1)
{
if(x=='0') return '1';
if(x=='1') return '2';
if(x=='2') return '0';
}
if(op==2)
{
if(x=='0') return '2';
if(x=='1') return '0';
if(x=='2') return '1';
}
if(op==3)
{
if(x=='0') return '0';
if(x=='1') return '2';
if(x=='2') return '1';
}
return x;
}
signed main()
{
int v=read(),q=read(),tmp=v;
string x;
while(tmp!=0)
{
x+=(tmp%3)+48;
tmp/=3;
}
const int xsize=x.size();
reverse(x.begin(),x.end());
x.resize(114,'0');
for(int i=1;i<=q;i++)
{
int op=read(),y=read(),ans=0;
x[y]=change(op,x[y]);
for(int j=40;j>=0;j--) ans=(ans<<1)+ans+(x[j]-48);
write(ans);
putchar('\n');
}
return 0;
}