#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> pp;
template<typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
return os << p.first << " " << p.second;
}
#define all(a) (a).begin(), (a).end()
#define what_is(x) cerr << #x << " is " << x << '\n';
#define print(a) for (const auto &x : (a)) cout << x << ' '; cout << '\n';
const int N = 2e5 + 7;
const int MOD = 1e9 + 7;
const int INF = 1e15;
const int M = 1e6 + 2;
int pre[M] {};
vector<int> levels[N], K[N];
int ans[10003];
vector<pp> X[2002];
int suf[N] {};
bool mrk[M] {};
void solve() {
// Don't forget
//
// 1. If you are looking at editorial, remember that you are accepting defeat.
// the problem defeated you
// 2. [If stuck] Don't stick to one approach and attack with different approaches.
// Write everything down, analyse the G-spot and attack through it.
// 3. Fix l, r, or i, and if necessary give it a value.
int n, m;
cin >> n >> m;
for (int i = 0, l, r; i < n; ++i) {
cin >> l >> r;
pre[l]++;
pre[r + 1] --;
}
for (int i = 1; i <= m; ++i) {
pre[i] += pre[i - 1];
}
// 1- Identify each level
vector<int> douts(m);
iota(all(douts), 1ll);
// for (auto &x : douts) {
// cout << x << ' ';
// }
// cout << endl;
for (int i = 20; i > 10; --i) {
vector<int> have;
for (auto &x : douts) {
if ((1<<i) & x) have.emplace_back(x);
}
if (have.size()) douts = have;
}
vector<pp> l_v;
for (auto x : douts) {
mrk[x] = true;
l_v.emplace_back(pre[x], x);
}
sort(l_v.rbegin(), l_v.rend());
for (int i = 1; i <= m; ++i) {
if (mrk[i]) continue;
levels[pre[i]].push_back(i);
}
for (int i = n; i >= 1; --i) {
suf[i] = suf[i + 1] + levels[i].size();
}
// 2- reading the queries and sort them based on k
int q; cin >> q;
for (int i = 0, x, k; i < q; ++i) {
cin >> x >> k;
X[x].emplace_back(k, i);
}
// 3- Loop for each x and determine it independenlty
int limit = min(2000ll, m);
for (int x = 1; x <= limit; ++x) {
if (!X[x].size()) continue;
int sum = 0;
int xl = pre[x];
map<int, int> mp;
for (auto [level, num] : l_v) {
int y = num ^ x;
if (1 <= y && y <= m) {
if (mp.count(level)) mp[level]++;
else {
auto it = mp.upper_bound(level);
if (it != mp.end()) mp[level] = it->second;
mp[level]++;
}
}
}
for (auto [k, i] : X[x]) {
auto it = mp.lower_bound(k);
int s1 = (it != mp.end() ? it->second : 0);
int s2 = suf[k];
if (!mrk[x] && pre[x] >= k) s2--;
ans[i] = s1 + s2;
}
}
for (int i = 0; i < q; ++i) {
cout << ans[i] << '\n';
}
// 3. Don't look at standings during the contest.
// 4. Don't try to code fast and code concetrately instead or bugs will annoy you
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int tt = 1;
// cin >> tt;
while (tt--) {
solve();
}
return 0;
}