#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);++i)
#define ST first
#define ND second
#define PB push_back
#define PAIR(a,b) make_pair((a), (b))
struct data {
data(int sum, int last_hist_id) : sum(sum), last_hist_id(last_hist_id) {}
int sum, last_hist_id;
};
struct historic_data{
historic_data(int sum, int left_id, int right_id) : sum(sum), left_id(left_id), right_id(right_id) {}
int left_id, right_id;
int sum;
};
struct historic_header {
historic_header(int X, int hist_data_id) : X(X), hist_data_id(hist_data_id) {}
int X;
int hist_data_id;
bool operator<(historic_header const & obj) const {
return X > obj.X; // hax
}
};
vector<data> curr_data;
vector<historic_data> hist_data;
set<historic_header> hist_sweep_line;
int transl, power;
int translate(vector<pair<int,int> > & points){
int mini = points[0].ST;
REP(i, points.size()){
mini = min(mini, min(points[i].ST, points[i].ND));
}
REP(i, points.size()){
points[i].ST -= mini;
points[i].ND -= mini;
}
return -mini;
}
void prepare(int elements){
--elements;
int res = 1;
while(elements){
res <<= 1;
elements >>= 1;
}
power = res;
vector<data> empty(2*power, data(0, -1));
curr_data.swap(empty);
}
void addAndCopy(int y){
bool tmp = true;
y += power;
while(y){
++curr_data[y].sum;
curr_data[y].last_hist_id = hist_data.size();
hist_data.PB(historic_data(
curr_data[y].sum,
tmp?-1:curr_data[2*y].last_hist_id,
tmp?-1:curr_data[2*y+1].last_hist_id));
y /= 2;
tmp = false;
}
}
void save(int x){
historic_header header(x, curr_data[1].last_hist_id);
hist_sweep_line.insert(header);
}
int get_max_y(vector<pair<int,int> > & points){
int max_y = points[0].ND;
REP(i, points.size()) max_y = max(max_y, points[i].ND);
return max_y;
}
void build(vector<pair<int,int> > points){
transl = translate(points);
sort(points.begin(), points.end());
int max_y = get_max_y(points);
prepare(max_y + 1);
REP(i, points.size()){
if(i!=0 && points[i-1].ST != points[i].ST){
save(points[i-1].ST);
}
addAndCopy(points[i].ND);
}
save(points[points.size()-1].ST);
}
int subquery(int root_id, int const& ym, int const& yM, int a, int b){
//printf("subQuery: root_id: %d, ym: %d, yM: %d, a: %d, b: %d hist_data[root_id].sum: %d\n", root_id, ym, yM, a, b, hist_data[root_id].sum);
if(a==b) return hist_data[root_id].sum;
int c = (a + b) / 2;
int result = 0;
int left = hist_data[root_id].left_id;
int right = hist_data[root_id].right_id;
if(left != -1 && yM >= a && c >= ym){
//printf("left: sum: %d\n", hist_data[left].sum);
result += (ym<=a && c<=yM ? hist_data[left].sum : subquery(left, ym, yM, a, c));
}
if(right != -1 && yM >= c+1 && b >= ym){
//printf("right: sum: %d\n", hist_data[right].sum);
result += (ym<=c+1 && b<=yM ? hist_data[right].sum : subquery(right, ym, yM, c+1, b));
}
return result;
}
int query(int xm, int xM, int ym, int yM){
if (xm > xM || ym > yM) return 0;
xm += transl;
xM += transl;
ym += transl;
yM += transl;
set<historic_header>::iterator lower = hist_sweep_line.lower_bound(historic_header(xM, -1));
set<historic_header>::iterator upper = hist_sweep_line.upper_bound(historic_header(xm, -1));
int low_root_id = -1;
//int tmp1 = subquery(root_id, ym, yM, 0, power-1), tmp2 = subquery(low_root_id, ym, yM, 0, power-1);
//printf("%s %d %d\n", low_root_id == -1?"low_root_id == -1":"low_root_id != -1", tmp1, tmp2);
return (lower == hist_sweep_line.end() ? 0 :
(upper == hist_sweep_line.end() ?
subquery(lower->hist_data_id, ym, yM, 0, power-1) :
subquery(lower->hist_data_id, ym, yM, 0, power-1) - subquery(upper->hist_data_id, ym, yM, 0, power-1)));
}
void cleanup(){
curr_data.clear();
hist_data.clear();
hist_sweep_line.clear();
}
int main()
{
int z,n,k,x,y,xm,xM,ym,yM; scanf("%d", &z); while(z--){
vector<pair<int,int> > points;
scanf("%d", &n);
REP(i,n) { scanf("%d%d", &x, &y); points.PB(PAIR(x,y)); }
build(points);
//printf("curr_data: "); REP(i, curr_data.size()) { printf("(sum: %d last_hist_id: %d) ", curr_data[i].sum, curr_data[i].last_hist_id); } printf("\n");
//printf("hist_data: "); REP(i, hist_data.size()) { printf("(sum: %d left_id: %d right_id: %d)\n", hist_data[i].sum, hist_data[i].left_id, hist_data[i].right_id); } printf("\n");
scanf("%d", &k); while(k--){
scanf("%d%d%d%d", &xm, &xM, &ym, &yM);
//printf("decoded: %d %d %d %d\n", xm, xM, ym, yM);
printf("%d\n", query(xm, xM, ym, yM));
}
cleanup();
}
return 0;
}