#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <random>
#include <chrono>
#include <bitset>

using namespace std;

typedef long long ll;
typedef long double ld;
#define fastInp cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);

const ll SIZE = 1e5 + 100, INF = 1e9 * 1e4 + 10, MOD = 1e9 + 7;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

ll n, a, b, c, d, dp[SIZE][30], lf, q;
vector<ll> vec;
map<ll, ll> ch;

ll check(int val, int pos) {
    if (val == 0) return pos;

    ll lft = val, u = pos;
    for (ll i = 29; i >= 0; i--) {
        if (lft - (1ll << i) >= 0) {
            u = dp[u][i];
            lft -= (1ll << i);
        }
    }

    return u;
}
void sol() {
    cin >> n;
    vec.resize(n);
    for (int i = 0; i < n; i++) {
        cin >> vec[i];
    }

    cin >> lf;

    cin >> q;

    for (int i = 29; i >= 0; i--) {
        dp[n - 1][i] = n - 1;
    }
    for (int i = vec.size() - 2; i >= 0; i--) {
        int l = i, r = vec.size();

        while (r - l > 1) {
            int mid = (l + r) / 2;
            if (vec[i] + lf < vec[mid]) {
                r = mid;
            }
            else {
                l = mid;
            }
        }

        dp[i][0] = l;
        for (int j = 1; j < 30; j++) {
            dp[i][j] = dp[dp[i][j - 1]][j - 1];
        }
    }
    
    while (q--) {
        ll lq, rq;
        cin >> lq >> rq;
        if (lq > rq) swap(lq, rq);

        if (lq == rq) {
            cout << "0\n";
            continue;
        }

        lq--;
        rq--;
        ll dist = 0, cur = lq, curR = rq;

        int l = 0, r = vec.size() + 1;
        while (r - l > 1) {
            int mid = (l + r) / 2;
            if (check(mid, cur) >= curR) {
                r = mid;
            }
            else {
                l = mid;
            }
        }
        cout << r << "\n";
    }
}

int main()
{
    fastInp;

    ll t = 1;

    while (t--) {
        sol();
    }

    return 0;
}
#include<bits/stdc++.h>
#pragma GCC optimize("Ofast")

using namespace std;
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
typedef long long ll;
#define rep(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define REP(i,num,n) for(int i=num, i##_len=(n); i<i##_len; ++i)
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
#define print(x) (cout << (x) << endl)
#define pb push_back
#define mp make_pair
#define sz(x) int(x.size())
template<class T> inline void Yes(T condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; }
#define fs first
#define sc second
// 多倍長
#include <boost/rational.hpp>
#include <boost/multiprecision/cpp_int.hpp>
using boost::rational;
using boost::multiprecision::cpp_int;

const int INF = 1e9;
const ll LLINF = 1e16;
const ll MOD = 1e9+7;
template<class T> inline void add(T &a, T b){a = ((a+b) % MOD + MOD) % MOD;};

template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}
 
template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
  return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}
 

// 指定した文字cが文字列に何文字入ってるか
ll stringcount(string s, char c) {
    return count(s.cbegin(), s.cend(), c);
}


// 割り算の切り上げ処理
template<typename T>
T rp(T a, T b) {
  return (a + b - 1) / b;
}

// 桁和
template<typename T>
T digsum(T n) {
    T res = 0;
    while(n > 0) {
        res += n%10;
        n /= 10;
    }
    return res;
}

// 回文判定
bool kai (string s) {
    string t{s};
    reverse(begin(t), end(t));
    return s == t;
}

using pint = pair<int, int>;
using pll = pair<ll, ll>;

int dp[3100][3100];

int main(void)
{
    cout.tie(0);
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    int n,t;
    cin >> n >> t;
    vector<pint> p(n);
    rep(i, n){
        cin >> p[i].fs >> p[i].sc;
    }
    sort(all(p));
    int ans = 0;
    rep(i, n){
        rep(j, t){
            chmax(dp[i + 1][j], dp[i][j]);
            if(j < p[i].fs) continue;
            chmax(dp[i + 1][j], dp[i][j - p[i].fs] + p[i].sc);
        }
        int now = dp[i][t - 1] + p[i].sc;
        chmax(ans, now);
    }
    print(ans);



}
