类似于p4357
不过因为距离相等的时候要优先选择序号小的,所以要重载一下运算符
//minamoto
#include<bits/stdc++.h>
#define R register
#define ll long long
#define inf 0x3f3f3f3f
#define fp(i,a,b) for(R int i=a,I=b+1;i<I;++i)
#define fd(i,a,b) for(R int i=a,I=b-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;}
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
using namespace std;
char buf[1<<21],*p1=buf,*p2=buf;
inline char getc(){return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;}
int read(){
R int res,f=1;R char ch;
while((ch=getc())>'9'||ch<'0')(ch=='-')&&(f=-1);
for(res=ch-'0';(ch=getc())>='0'&&ch<='9';res=res*10+ch-'0');
return res*f;
}
char sr[1<<21],z[20];int C=-1,Z=0;
inline void Ot(){fwrite(sr,1,C+1,stdout),C=-1;}
void print(R int x){
if(C>1<<20)Ot();if(x<0)sr[++C]='-',x=-x;
while(z[++Z]=x%10+48,x/=10);
while(sr[++C]=z[Z],--Z);sr[++C]='
';
}
const int N=1e5+5;
struct node{int v[2],mn[2],mx[2],l,r,id,idmn;}tr[N],S;
int n,m,K,rt,k;
inline bool operator <(const node &a,const node &b){return a.v[K]<b.v[K];}
struct qqq{
int id;ll dis;
qqq(){}
qqq(int id,ll dis):id(id),dis(dis){}
inline bool operator <(const qqq &b)const{return dis==b.dis?id<b.id:dis>b.dis;}
};priority_queue<qqq>q;
void upd(int p){
int l=tr[p].l,r=tr[p].r;tr[p].idmn=tr[p].id;
if(l)cmin(tr[p].idmn,tr[l].idmn);if(r)cmin(tr[p].idmn,tr[r].idmn);
fp(i,0,1){
tr[p].mn[i]=tr[p].mx[i]=tr[p].v[i];
if(l)cmin(tr[p].mn[i],tr[l].mn[i]),cmax(tr[p].mx[i],tr[l].mx[i]);
if(r)cmin(tr[p].mn[i],tr[r].mn[i]),cmax(tr[p].mx[i],tr[r].mx[i]);
}
}
int build(int l,int r,int k){
K=k;int mid=(l+r)>>1;nth_element(tr+l,tr+mid,tr+r+1);
if(l<mid)tr[mid].l=build(l,mid-1,k^1);
if(mid<r)tr[mid].r=build(mid+1,r,k^1);
upd(mid);return mid;
}
inline ll sqr(ll x){return x*x;}
inline ll ck(int p){
return max(sqr(tr[p].mn[0]-S.v[0]),sqr(tr[p].mx[0]-S.v[0]))+
max(sqr(tr[p].mn[1]-S.v[1]),sqr(tr[p].mx[1]-S.v[1]));
}
inline ll dis(int p){return sqr(tr[p].v[0]-S.v[0])+sqr(tr[p].v[1]-S.v[1]);}
void query(int p){
ll dl=-inf,dr=-inf,dd=dis(p);int id=tr[p].id;
if(dd>q.top().dis||(dd==q.top().dis&&id<q.top().id))q.pop(),q.push(qqq(id,dd));
if(tr[p].l)dl=ck(tr[p].l);if(tr[p].r)dr=ck(tr[p].r);
if(dl>dr){
if(dl>q.top().dis||(dl==q.top().dis&&tr[tr[p].l].idmn<q.top().id))query(tr[p].l);
if(dr>q.top().dis||(dr==q.top().dis&&tr[tr[p].r].idmn<q.top().id))query(tr[p].r);
}else{
if(dr>q.top().dis||(dr==q.top().dis&&tr[tr[p].r].idmn<q.top().id))query(tr[p].r);
if(dl>q.top().dis||(dl==q.top().dis&&tr[tr[p].l].idmn<q.top().id))query(tr[p].l);
}
}
int main(){
// freopen("testdata.in","r",stdin);
n=read();fp(i,1,n)tr[i].v[0]=read(),tr[i].v[1]=read(),tr[i].id=i;
rt=build(1,n,0),m=read();
while(m--){
while(!q.empty())q.pop();S.v[0]=read(),S.v[1]=read(),k=read();
fp(i,1,k)q.push(qqq(0,-inf));
query(rt);ll d=q.top().dis;print(q.top().id);
}return Ot(),0;
}