//Let's join Kaede Takagaki Fan Club !!
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
#define int long long
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define a first
#define b second
#define fi first
#define sc second
#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
#define all(x) x.begin(),x.end()
#define si(x) int(x.size())

#ifdef LOCAL
#define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl
#else
#define dmp(x) void(0)
#endif

template<class t,class u> bool chmax(t&a,u b){if(a<b){a=b;return true;}else return false;}
template<class t,class u> bool chmin(t&a,u b){if(b<a){a=b;return true;}else return false;}

template<class t> using vc=vector<t>;

template<class t,class u>
ostream& operator<<(ostream& os,const pair<t,u>& p){
	return os<<"{"<<p.fi<<","<<p.sc<<"}";
}

template<class t> ostream& operator<<(ostream& os,const vc<t>& v){
	os<<"{";
	for(auto e:v)os<<e<<",";
	return os<<"}";
}

template<class T>
void g(T &a){
	cin >> a;
}
template<class T>
void o(const T &a,bool space=false){
	cout << a << (space?' ':'\n');
}
//ios::sync_with_stdio(false);
const ll mod = 1000000007;//998244353
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
template<class T>
void add(T&a,T b){
	a+=b;
	if(a >= mod) a-=mod;
}
ll modpow(ll x,ll n){
	ll res=1;
	while(n>0){
		if(n&1) res=res*x%mod;
		x=x*x%mod;
		n>>=1;
	}
	return res;
}
int n, m, q;
int zan[3][3];
int x[200005], y[200005];
int ty[200005], val[200005];
int ans[2][200005];
vc<P>Q[200005];

void solve(){
	rep(i, 3) zan[i][i] = 1;
	cin >> n;
	repn(i, n) cin >> x[i] >> y[i];
	cin>>m;
	repn(i, m){
		cin >> ty[i];
		if(ty[i] >= 3) cin >> val[i];
	}
	cin>>q;
	rep(i, q){
		int a, b; cin >> a >> b;
		Q[a].eb(b, i);
	}
	repn(i, m+1){
		for(auto at:Q[i-1]){
			int v = at.a, id = at.b;
			ans[0][id] = x[v] * zan[0][0] + y[v] * zan[0][1] + zan[0][2];
			ans[1][id] = x[v] * zan[1][0] + y[v] * zan[1][1] + zan[1][2];
		}
		if(i == m+1) break;
		int nxt[3][3] = {};
		int v[3][3] = {};
		if(ty[i] == 1){
			v[0][1] = 1;
			v[1][0] = -1;
			v[2][2] = 1;
		}
		else if(ty[i] == 2){
			v[0][1] = -1;
			v[1][0] = 1;
			v[2][2] = 1;
		}
		else if(ty[i] == 3){
			v[0][0] = -1; v[0][2] = 2LL * val[i];
			v[1][1] = v[2][2] = 1;
		}
		else{
			v[0][0] = 1;
			v[1][1] = -1; v[1][2] = 2LL*val[i];
			v[2][2] = 1;
		}
		rep(i, 3) rep(j, 3) rep(k, 3){
			nxt[i][k] += v[i][j] * zan[j][k];
		}
		rep(i, 3) rep(j, 3) zan[i][j] = nxt[i][j];
	}
	rep(i, q) cout<<ans[0][i]<<" "<<ans[1][i]<<'\n';
}
signed main(){
	cin.tie(0);
	ios::sync_with_stdio(0);
	cout<<fixed<<setprecision(20);
	int t; t = 1; //cin >> t;
	while(t--) solve();
}

//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid (tr[u].l+tr[u].r>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;

//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;

const int N=200010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;

int n,k;
int a[N];
char s[N];
int f[N][20];

int dfs(int pos,int state,int flag,int lead)
{
    int cnt=__builtin_popcount(state);
    if(cnt>k) return 0;
    if(pos==n+1) return cnt==k&&lead;
    if(flag&&lead&&f[pos][cnt]!=-1) return f[pos][cnt];
    int x=flag? 15:a[pos];
    int ans=0;
    for(int i=0;i<=x;i++)
        (ans+=dfs(pos+1,(!lead&&i==0)? state:state|(1<<i),flag||i<x,lead||i!=0))%=mod;
    if(flag&&lead) f[pos][cnt]=ans;
    return ans;
}

int main()
{
//	ios::sync_with_stdio(false);
//	cin.tie(0);

    scanf("%s%d",s+1,&k);
    n=strlen(s+1);
    for(int i=1;i<=n;i++)
        if(s[i]>='A'&&s[i]<='Z') a[i]=s[i]-'A'+10;
        else a[i]=s[i]-'0';
    memset(f,-1,sizeof(f));
    printf("%d",dfs(1,0,0,0));






	return 0;
}
/*

*/
