#include using namespace std; #define int long long int #define forn(i, a, n) for (int i = a; i < n; i++) #define fornr(i, a, n) for (int i = n - 1; i >= a; i--) #define print(a) cout << a << "\n"; #define printarr(a) forn(i, 0, a.size()) cout << a[i] << " "; cout << endl; #define file_read(filepath) freopen(filepath, "r", stdin); #define file_write(filepath) freopen(filepath, "w", stdout); #define f first #define s second #define pb push_back #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define sorted(a) is_sorted(all(a)) #define vi vector #define vvi vector> #define vc vector #define vs vector #define pii pair #define pis pair #define psi pair #define vpii vector> #define MOD1 1000000007 #define mii map #define sz(a) (int)a.size() #define ld long double template struct BIT { int n; vector b, a; BIT(int n) : n(n), b(n+1), a(n) {} BIT(vector& v) : BIT(v.size()) { for (int i = 0; i < n; ++i) add(i, v[i]); } void add(int i, T v) { a[i] += v; for (++i; i <= n; i += i & -i) b[i] += v; } void set(int i, T v) { add(i, v - a[i]); } T sum(int i) { T r = 0; for (++i; i; i -= i & -i) r += b[i]; return r; } T sum(int l, int r) { return sum(r) - (l ? sum(l - 1) : 0); } int lower_bound(T x) { int i = 0; for (int k = 1 << __lg(n); k; k >>= 1) if (i + k <= n && b[i + k] < x) x -= b[i += k]; return i; } }; void solve(){ int n; cin >> n; vvi chal(100005); vi compr; forn(i,0,n){ int x, y; cin >> x >> y; chal[y].pb(x); compr.pb(x); } int m; cin >> m; vector> p(m); vvi who(100005); for(int i = 0; i < m; i++){ for(int j = 0; j < 2; j++){ cin >> p[i][j]; } who[p[i][1]].pb(i); p[i][2] = i; compr.pb(p[i][0]); } sort(all(compr)); set st(all(compr)); compr = vi(); compr.pb(0); for(auto it : st){ compr.pb(it); } for(int i = 1; i <= 100000; i++){ for(int j = 0; j < sz(chal[i]); j++){ chal[i][j] = lower_bound(all(compr), chal[i][j]) - compr.begin(); } } //maintain fenwick. BIT fenw(500000); for(int i = 1; i <= 100000; i++){ for(int j = 0; j < sz(chal[i]); j++){ fenw.add(chal[i][j], 1); } } vi ans(m); for(int i = 1; i <= 100000; i++){ //initially remove of this type from fenwick. for(int j = 0; j < sz(chal[i]); j++){ fenw.add(chal[i][j], -1); } for(auto k : who[i]){ auto iska = lower_bound(all(compr), p[k][0]) - compr.begin(); int cur = fenw.sum(0, iska); ans[k] = cur; } for(int j = 0; j < sz(chal[i]); j++){ fenw.add(chal[i][j], +1); } } for(auto it : ans){ cout << it << endl; } } signed main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; //cin >> t; while(t--){ solve(); } }