#include<bits/stdc++.h>
#define pii pair<int,int>
#define N 1000005
using namespace std;
int n,q,f[N];
inline int find(int x){return x==f[x]?x:f[x]=find(f[x]);}
map<int,int>mp[N];
int main()
{
	scanf("%d%d",&n,&q);
	for(int i=1,c;i<=n;++i)
	{
		scanf("%d",&c);
		mp[i][c]=1;
		f[i]=i;
	}
	while(q--)
	{
		int t,x,y;
		scanf("%d%d%d",&t,&x,&y);
		if(t==1)
		{
			x=find(x);y=find(y);
			if(x!=y)
			{
				if(mp[x].size()<mp[y].size())swap(x,y);
				for(pii p:mp[y])mp[x][p.first]+=p.second;
				f[y]=x;
			}
		}
		else printf("%d\n",mp[find(x)][y]);
	}
}
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int n, m, t[200200], p[200200];
ll a[200200], x[200200], ans[200200];

int find(int a){
	if(a == p[a]) return a;
	return p[a] = find(p[a]);
}

void merge(int a, int b){
	a = find(a), b = find(b);
	p[a] = b;
}

int main(){
	cin.tie(0)->sync_with_stdio(0);
	cin >> n;
	for(int i=1;i<=n;i++) cin >> a[i] >> t[i];
	cin >> m;
	for(int i=1;i<=m;i++) cin >> x[i], p[i] = i;

	set<pair<ll, int>> s;
	for(int i=1;i<=m;i++) s.insert({x[i], i});

	ll pl = 0;
	for(int i=1;i<=n;i++){
		vector<set<pair<ll, int>>::iterator> v;
		if(t[i] == 1){
			pl += a[i];
		}if(t[i] == 2){
			auto it = s.upper_bound({a[i]-pl, n+1});
			if(it != s.begin()){
				it = prev(it);

				for(auto u = s.begin(); u != it; u++){
					merge((*u).second, (*next(u)).second);
					v.push_back(u);
				}

				auto [x, y] = *it;
				s.erase(it), s.insert({a[i]-pl, y});
			}
		}if(t[i] == 3){
			auto it = s.lower_bound({a[i]-pl, 0});
			if(it != s.end()){
				for(auto u = prev(s.end()); u != it; u--){
					merge((*u).second, (*prev(u)).second);
					v.push_back(u);
				}
				auto [x, y] = *it;
				s.erase(it), s.insert({a[i]-pl, y});
			}
		}

		for(auto x : v) s.erase(x);
	}

	for(auto [x, y] : s) ans[y] = x;
	for(int i=1;i<=m;i++) ans[i] = ans[find(i)];
	for(int i=1;i<=m;i++) cout << ans[i]+pl << "\n";
}