#include<iostream>
#include<cmath>
using namespace std;

#define int long long
const int N = 3005;
int n, d, q;
int c[N], suf[N];

signed main(){
	cin.tie(0) -> ios::sync_with_stdio(0);
	
	cin >> n >> d;
	
	for(int i = 1;i <= n;i ++){
		cin >> c[i];
	}
	
	cin >> q;
	
	while(q --){
		int l, r; cin >> l >> r;
		int cnt = 0;
		int cost = 1e14;
		bool flag = 0;
		for(int i = r;i >= l;i --){
			cost = min(cost, c[i]);
			if((c[i] - cost) % d != 0){
				cout << -1 << '\n';
				flag = 1;
				break;
			}
			cnt += ((c[i] - cost) / d);	
		}
		if(!flag) cout << cnt << '\n';
	}
	return 0;
}
