#include<bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
int v,q;
string s="0123456789";
int f(string x)
{
int ans=0,k=0;
for(int i=0;i<x.size();i++)
ans+=(x[i]-'0')*pow(3,k++);
return ans;
}
string change(int x)
{
string a="";
while(x)
{
a+=s[x%3];
x/=3;
}
return a;
}
signed main()
{
scanf("%lld%lld",&v,&q);
string s=change(v);
int n=(int)s.size();
while(q--)
{
int op,i;
scanf("%lld%lld",&op,&i);
if(op==1)
{
if(s[i]=='0')
s[i]='1';
else if(s[i]=='1')
s[i]='2';
else s[i]='0';
}
else if(op==2)
{
if(s[i]=='0')
s[i]='2';
else if(s[i]=='1')
s[i]='0';
else s[i]='1';
}
else
{
if(s[i]=='1')
s[i]='2';
else if(s[i]=='2')
s[i]='1';
else continue;
}
cout<<f(s)<<endl;
}
return 0;
}