#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const int N=60;
const ll inf=2e18;
ll x,m,a[N];
vector<ll>v;
void san(ll x)
{
while(x)
{
v.push_back(x%3);
x/=3;
}
}
void print()
{
ll res=0;
for(ll i=0;i<=50;i++)
{
res+=pow(3,i)*a[i];
}
cout<<res<<'\n';
}
int main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>x>>m;
san(x);
for(int i=0;i<v.size();i++)
a[i]=v[i];
while(m--)
{
ll k,p;cin>>k>>p;
if(k==1)
{
if(a[p]==0)a[p]=1;
else if(a[p]==1)a[p]=2;
else if(a[p]==2)a[p]=0;
}else if(k==2)
{
if(a[p]==0)a[p]=2;
else if(a[p]==1)a[p]=0;
else if(a[p]==2)a[p]=1;
}else
{
if(a[p]==0)a[p]=0;
else if(a[p]==1)a[p]=2;
else if(a[p]==2)a[p]=1;
}
print();
}
return 0;
}