//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")
  
#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <assert.h>
#include <memory.h>
#include <time.h>
#include <bitset>

#include "atcoder/convolution.hpp"
#include "atcoder/dsu.hpp"
#include "atcoder/fenwicktree.hpp"
#include "atcoder/lazysegtree.hpp"
#include "atcoder/math.hpp"
#include "atcoder/maxflow.hpp"
#include "atcoder/mincostflow.hpp"
#include "atcoder/modint.hpp"
#include "atcoder/scc.hpp"
#include "atcoder/segtree.hpp"
#include "atcoder/string.hpp"
#include "atcoder/twosat.hpp"

#define uint unsigned int
#define ll long long
#define ull unsigned long long
#define ld long double
#define rep(i, l, r) for (int i = (l); i < (r); i++)
#define repb(i, r, l) for (int i = (r); i > (l); i--)
#define sz(a) (int)a.size()
#define fi first
#define se second
#define mp(a, b) make_pair(a, b)
#define rank qwertyuio
#define next dfghjk
#define prev fhsgfhjf
#define plus fsghsf
#define minus ytryr
  
using namespace std;
using namespace atcoder;
  
inline bool setmin(int &x, int y) { return (y < x) ? x = y, 1 : 0; }
inline bool setmax(int &x, int y) { return (y > x) ? x = y, 1 : 0; }
inline bool setmin(ll &x, ll y) { return (y < x) ? x = y, 1 : 0; }
inline bool setmax(ll &x, ll y) { return (y > x) ? x = y, 1 : 0; }
  
const int N = 200000;
const int inf = (int)1e9 + 1;
const ll big = (ll)1e18 + 1;
const int P = 239;
const int P1 = 31;
const int P2 = 57;
const int MOD = (int)1e9 + 7;
const int MOD1 = (int)1e9 + 9;
const int MOD2 = 998244353;
const ld eps = 1e-9;
const double pi = atan2(0, -1);
const int ABC = 26;

vector<int> g[N];
bool is_root[N];
int height[N];
int up[N][18];
ll sum[N][18];

void dfs(int u, int par, int h, vector<int> &w) {
    fill(up[u], up[u] + 18, -1);
    height[u] = h;
    sum[u][0] = w[u];
    up[u][0] = par;
    for (int p = 1; (1 << p) <= h + 1; p++) {
        up[u][p] = up[up[u][p - 1]][p - 1];
        sum[u][p] = sum[u][p - 1] + sum[up[u][p - 1]][p - 1];
    }
    for (int v : g[u]) {
        dfs(v, u, h + 1, w);
    }
}

int get_cnt(int u, int v) {
    int res = 0;
    repb(i, 17, -1) {
        if (up[u][i] >= v) {
            res += (1 << i);
            u = up[u][i];
        }
    }
    return res + 1;
}

ll get_sum(int u, int k) {
    ll res = 0;
    repb(i, 17, -1) {
        if ((k >> i) & 1) {
            res += sum[u][i];
            u = up[u][i];
        }
    }
    return res;
}

vector<ll> solve(int n, int k, vector<int> a, vector<int> w, vector<pair<int, int> > Q) {
    rep(i, 0, n) {
        g[i].clear();
        is_root[i] = false;
    }
    rep(i, 0, n) {
        int pos = upper_bound(a.begin(), a.end(), a[i] - k) - a.begin() - 1;
        if (pos >= 0) {
            g[pos].push_back(i);
        } else {
            is_root[i] = true;
        }
    }
    rep(i, 0, n) {
        if (is_root[i]) {
            dfs(i, -1, 0, w);
        }
    }
    vector<ll> res(sz(Q), 0);
    rep(q, 0, sz(Q)) {
        int l = Q[q].fi, r = Q[q].se;
        int cnt = get_cnt(r, l);
        res[q] = get_sum(r, cnt);
    }
    return res;
}

int main()
{
    //freopen("circus.in", "r", stdin);
    //freopen("circus.out", "w", stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.precision(20);
    cout << fixed;
    //ll TL = 10.95 * CLOCKS_PER_SEC;
    //clock_t time = clock();
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    rep(i, 0, n) {
        cin >> a[i];
    }
    int q;
    cin >> q;
    vector<pair<int, int> > Q(q);
    rep(i, 0, q) {
        int l, r;
        cin >> l >> r; l--; r--;
        Q[i] = {l, r};
    }

    vector<int> w(n, 0);
    rep(i, 0, n) {
        w[i] = i + 1;
    }

    vector<ll> res1 = solve(n, k, a, w, Q);

    rep(i, 0, n) {
        w[n - i - 1] = -i;
        a[i] = -a[i];
    }
    reverse(a.begin(), a.end());
    rep(i, 0, q) {
        Q[i] = {n - Q[i].se - 1, n - Q[i].fi - 1};
    }

    vector<ll> res2 = solve(n, k, a, w, Q);

    rep(i, 0, q) {
        cout << res1[i] + res2[i] << "\n";
    }
    return 0;
}



Measured memory_bytes: 76000