#include <bits/stdc++.h>

using namespace std;

using int64 = long long;

struct Edge {
  int from, to;
};

vector<vector<Edge>> ReadDirectedGraph(
    int n, int m, bool is_one_indexed=false) {
  vector<vector<Edge>> graph(n);
  for (int i = 0; i < m; i++) {
    int v1, v2;
    cin >> v1 >> v2;
    if (is_one_indexed) {
      v1--;
      v2--;
    }
    graph[v1].push_back(Edge({v1, v2}));
  }
  return graph;
}

vector<int> StronglyConnectedComponents(const vector<vector<Edge>>& graph) {
  int n = graph.size();
  vector<int> vs;
  vector<int> visited1(n);
  function<void(int)> dfs1 = [&](int v) {
    if (visited1[v]) return;
    visited1[v] = 1;
    for (const auto& e : graph[v]) dfs1(e.to);
    vs.push_back(v);
  };
  for (int v = 0; v < n; v++) dfs1(v);
  reverse(vs.begin(), vs.end());

  vector<vector<Edge>> r_graph(n);
  for (int v = 0; v < n; v++) {
    for (const auto& e : graph[v]) {
      r_graph[e.to].push_back(Edge({e.to, e.from}));
    }
  }

  vector<int> to_id(n);
  int current_id = 0;
  vector<int> visited2(n);
  function<void(int)> dfs2 = [&](int v) {
    if (visited2[v]) return;
    visited2[v] = 1;
    to_id[v] = current_id;
    for (const auto& e : r_graph[v]) dfs2(e.to);
  };
  for (int v : vs) {
    if (!visited2[v]) {
      dfs2(v);
      current_id++;
    }
  }
  return to_id;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, m;
  cin >> n >> m;
  auto graph = ReadDirectedGraph(n, m, false);
  auto to_scc_id = StronglyConnectedComponents(graph);
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int v1, v2;
    cin >> v1 >> v2;
    if (to_scc_id[v1] == to_scc_id[v2]) {
      cout << 1 << endl;
    } else {
      cout << 0 << endl;
    }
  }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define sqr(x) ((x)*(x))
#define mp make_pair
#define uint unsigned
#define ld long double
#define PI pair<int,int>
inline char gc(){
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
#define gc getchar
inline ll read(){
	ll x = 0; char ch = gc(); bool positive = 1;
	for (; !isdigit(ch); ch = gc())	if (ch == '-')	positive = 0;
	for (; isdigit(ch); ch = gc())	x = x * 10 + ch - '0';
	return positive ? x : -x;
}
inline void write(ll a){
    if(a<0){
    	a=-a; putchar('-');
	}
    if(a>=10)write(a/10);
    putchar('0'+a%10);
}
inline void writeln(ll a){write(a); puts("");}
inline void wri(ll a){write(a); putchar(' ');}
inline ull rnd(){
	return ((ull)rand()<<30^rand())<<4|rand()%4;
}
const int N=100005;
int dq,n,s,tot,q[N];
ll sum,ans;
int x[N],p[N];
signed main(){
	n=read(); s=read();
	for(int i=1;i<=n;i++){
		x[i]=read(); p[i]=read();
	}
	int l=1,r=n;
	int pos=lower_bound(&x[1],&x[n+1],s)-x;
	dq=0;
	if(pos==1){
		cout<<x[n]-s<<endl; return 0;
	}
	if(pos>n){
		cout<<s-x[1]<<endl; return 0;
	}
	if(p[1]<p[n]){dq=1; sum=p[1]; q[++tot]=x[1]; l++;}else {sum=p[n]; q[++tot]=x[n]; r--;}
	while(1){
		if(dq){
			if(r<pos)break;
			if(p[r]>sum){
				dq^=1; q[++tot]=x[r];
			}
			sum+=p[r];
			r--;
		}else{
			if(l>=pos)break;
			if(p[l]>=sum){
				dq^=1; q[++tot]=x[l];
			}
			sum+=p[l];
			l++;
		}
	}
	reverse(&q[1],&q[tot+1]);
	q[0]=s; for(int i=1;i<=tot;i++)ans+=abs(q[i]-q[i-1]);
	cout<<ans<<endl;
}