#include <bits/stdc++.h>
using namespace std;
#define int long long
int n,q,w,h;
struct A{
int x, y, r;
};
A tre[2010];
struct B{
int u,v,w;
bool operator < (const B&o) const {
return w < o.w;
}
};
vector<B> ed;
struct C{
int r,e,i;
bool operator < (const C&o) const {
return r < o.r;
}
};
vector<C> que;
string ans[100010];
int f[2010];
int fr(int x){ return ((f[x] == x)? x : f[x] = fr(f[x])); }
void uni(int x, int y)
{
int fx = fr(x), fy = fr(y);
if (fx == fy) return;
f[fx] = fy;
}
int wdi(int x, int y, int wa)
{
if (wa == 1) return x;
else if (wa == 2) return y;
else if (wa == 3) return w-x;
else return h-y;
}
int dis(pair<int,int> a, pair<int,int> b)
{
return (int)sqrt((a.first-b.first)*(a.first-b.first) + (a.second-b.second)*(a.second-b.second));
}
signed main()
{
cin.tie(0)->sync_with_stdio(0);
cin >> n >> q >> w >> h;
for (int i = 1; i <= n; i++){
int x,y,r; cin >> x >> y >> r;
tre[i] = {x,y,r};
f[i] = i;
for (int j = 1; j <= 4; j++){
ed.push_back({i, n+j, wdi(x,y,j) - r});
}
}
for (int i = 1; i <= 4; i++) f[n+i] = n+i;
for (int i = 1; i <= n; i++){
for (int j = i+1; j <= n; j++){
ed.push_back({i,j, dis({tre[i].x,tre[i].y}, {tre[j].x, tre[j].y}) - tre[i].r - tre[j].r});
}
}
for (int i = 1; i <= q; i++){
int r,e; cin >> r >> e;
que.push_back({r,e,i});
}
sort(ed.begin(), ed.end());
sort(que.begin(), que.end());
int idx = 0;
for (auto now : que){
while (idx < ed.size() && ed[idx].w < 2*now.r){
uni(ed[idx].u, ed[idx].v);
idx++;
}
int f1 = fr(n+1), f2 = fr(n+2), f3 = fr(n+3), f4 = fr(n+4);
//cout << now.r << " : ";
//cout << f1 << " " << f2 << " " << f3 << " " << f4 << "\n";
if (now.e == 1){
ans[now.i] += "1";
if (f2 != f1 && f2 != f4 && f2 != f3) ans[now.i] += "2";
if (f4 != f2 && f4 != f3 && f2 != f1) ans[now.i] += "3";
if (f1 != f4 && f1 != f3 && f1 != f2) ans[now.i] += "4";
}
else if (now.e == 2){
if (f2 != f3 && f2 != f4 && f2 != f1) ans[now.i] += "1";
ans[now.i] += "2";
if (f3 != f4 && f3 != f1 && f3 != f2) ans[now.i] += "3";
if (f1 != f4 && f1 != f3 && f2 != f3) ans[now.i] += "4";
}
else if (now.e == 3){
if (f4 != f3 && f4 != f2 && f1 != f2) ans[now.i] += "1";
if (f3 != f4 && f3 != f1 && f3 != f2) ans[now.i] += "2";
ans[now.i] += "3";
if (f4 != f1 && f4 != f2 && f4 != f3) ans[now.i] += "4";
}
else {
if (f1 != f4 && f1 != f3 && f1 != f2) ans[now.i] += "1";
if (f2 != f3 && f2 != f4 && f1 != f4) ans[now.i] += "2";
if (f4 != f1 && f4 != f2 && f4 != f3) ans[now.i] += "3";
ans[now.i] += "4";
}
}
for (int i = 1; i <= q; i++){
cout << ans[i] << "\n";
}
return 0;
}