rt,
// Problem: T448165 进制
// LuoguUID: 514700
// LuoguUserName: MicroSun
//
// Powered by CP Editor (https://cpeditor.org)
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define pb push_back
#define fst first
#define scd second
#define rep(i,s,e) for(int i=s;i<=e;i++)
#define dep(i,s,e) for(int i=s;i>=e;i--)
using ll=long long;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
const int maxn=2e5+10;
ll a[500],cnt,quan[40];
void solve(){
ll v,q;
cin>>v>>q;
while(v){
a[cnt++]=v%3;
v/=3;
}
cnt=39;
quan[0]=1;
rep(i,1,39) quan[i]=quan[i-1]*3;
while(q--){
int op,x;
cin>>op>>x;
a[x]=(a[x]+op)%3;
if(op==3){
if(a[x]==1) ++a[x];
if(a[x]==2) --a[x];
}
//rep(i,0,cnt) cerr<<a[i]<<' ';
//cerr<<endl;
ll ans=0;
rep(i,0,cnt) ans+=a[i]*quan[i];
cout<<ans<<endl;
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t=1;
//cin>>t;
while(t--) solve();
return 0;
}