#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<vector>
#define REP(i,m) for(int i=0;i<m;++i)
#define REPN(i,m,in) for(int i=in;i<m;++i)
#define ALL(t) (t).begin(),(t).end()
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define dump(x)  cerr << #x << " = " << (x) << endl
#define prl cerr<<"called:"<< __LINE__<<endl
using namespace std;
static const int INF =500000000;
template<class T> void debug(T a,T b){ for(;a!=b;++a) cerr<<*a<<' ';cerr<<endl;}
typedef long long int lint;
typedef pair<int,int> pi;
lint n,m,mod;

lint calc(lint a,lint b){
	if(a%2==0) return (a/2)%mod*(b%mod)%mod;
	return a%mod*((b/2)%mod)%mod;
}
lint floor(lint a,lint b){
	if(a>=0) return a/b;
	if(a%b==0) return a/b;
	return a/b-1;
}
lint solve(lint L,lint R,lint l,lint r,lint low,lint up,lint C,lint dif){
	if(l<=L && R<=r){
		lint begin=floor(low-C+dif-1,dif),end=floor(up-1-C,dif);
		begin=max(begin,0ll);

		end=min(end,R-L-1);
		++end;
		if(begin>=end) return 0;
		//[begin...end)
		int len=end-begin;
		lint ans=(len+C%mod*(len%mod)%mod+(calc(begin+end-1,len)%mod*(dif%mod)%mod))%mod;
		return ans;
	}else if(r<=L || R<=l) return 0;

	lint md=(L+R+1)>>1;
	lint A=solve(L,md,l,r,low,up,C,dif*2);
	lint B=solve(md,R,l,r,low,up,C+dif,dif*2);
	return (A+B)%mod;
}

int main(){
	cin>>n>>m>>mod;

	REP(hoge,m){
		lint l,r,u,v;
		cin>>l>>r>>u>>v;
		--l;
		--u;
		cout<<solve(0,n,l,r,u,v,0,1)<<endl;
	}

	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<vector>
#define REP(i,m) for(int i=0;i<m;++i)
#define REPN(i,m,in) for(int i=in;i<m;++i)
#define ALL(t) (t).begin(),(t).end()
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define dump(x)  cerr << #x << " = " << (x) << endl
#define prl cerr<<"called:"<< __LINE__<<endl
using namespace std;
static const int INF =500000000;
template<class T> void debug(T a,T b){ for(;a!=b;++a) cerr<<*a<<' ';cerr<<endl;}
typedef long long int lint;
typedef pair<int,int> pi;
lint n,m,mod;

lint calc(lint a,lint b){
	if(a%2==0) return (a/2)%mod*(b%mod)%mod;
	return a%mod*((b/2)%mod)%mod;
}
lint solve(lint L,lint R,lint l,lint r,lint low,lint up,lint C,lint dif){
	if(l<=L && R<=r){
		lint begin=(low-C+dif-1)/dif,end=((up-1)-C)/dif;
		begin=max(begin,0ll);

		end=min(end,r-l-1);
		++end;
		if(begin>=end) return 0;
		//[begin...end)
		int len=end-begin;
		lint ans=(len+C%mod*(len%mod)%mod+(calc(begin+end-1,len)*(dif%mod)%mod))%mod;
		return ans;
	}else if(r<=L || R<=l) return 0;

	lint md=(L+R+1)>>1;
	lint A=solve(L,md,l,r,low,up,C,dif*2);
	lint B=solve(md,R,l,r,low,up,C+dif,dif*2);
	return (A+B)%mod;
}

int main(){
	cin>>n>>m>>mod;

	REP(hoge,m){
		int l,r,u,v;
		scanf("%d%d%d%d",&l,&r,&u,&v);--l;

		--u;
		cout<<solve(0,n,l,r,u,v,0,1)<<endl;
	}

	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<vector>
#define REP(i,m) for(int i=0;i<m;++i)
#define REPN(i,m,in) for(int i=in;i<m;++i)
#define ALL(t) (t).begin(),(t).end()
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define dump(x)  cerr << #x << " = " << (x) << endl
#define prl cerr<<"called:"<< __LINE__<<endl
using namespace std;
static const int INF =500000000;
template<class T> void debug(T a,T b){ for(;a!=b;++a) cerr<<*a<<' ';cerr<<endl;}
typedef long long int lint;
typedef pair<int,int> pi;
lint n,m,mod;

lint calc(lint a,lint b){
	if(a%2==0) return (a/2)%mod*(b%mod)%mod;
	return a%mod*((b/2)%mod)%mod;
}
lint floor(lint a,lint b){
	if(a>=0) return a/b;
	if(a%b==0) return a/b;
	return a/b-1;
}
lint solve(lint L,lint R,lint l,lint r,lint low,lint up,lint C,lint dif){
	if(l<=L && R<=r){
		lint begin=floor(low-C+dif-1,dif),end=floor(up-1-C,dif);
		begin=max(begin,0ll);

		end=min(end,R-L-1);
		++end;
		if(begin>=end) return 0;
		//[begin...end)
		lint len=end-begin;
		lint ans=(len+C%mod*(len%mod)%mod+(calc(begin+end-1,len)%mod*(dif%mod)%mod))%mod;
		return ans;
	}else if(r<=L || R<=l) return 0;

	lint md=(L+R+1)>>1;
	lint A=solve(L,md,l,r,low,up,C,dif*2);
	lint B=solve(md,R,l,r,low,up,C+dif,dif*2);
	return (A+B)%mod;
}

int main(){
	cin>>n>>m>>mod;

	REP(hoge,m){
		lint l,r,u,v;
		cin>>l>>r>>u>>v;
		--l;
		--u;
		cout<<solve(0,n,l,r,u,v,0,1)<<endl;
	}

	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<vector>
#define REP(i,m) for(int i=0;i<m;++i)
#define REPN(i,m,in) for(int i=in;i<m;++i)
#define ALL(t) (t).begin(),(t).end()
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define dump(x)  cerr << #x << " = " << (x) << endl
#define prl cerr<<"called:"<< __LINE__<<endl
using namespace std;
static const int INF =500000000;
template<class T> void debug(T a,T b){ for(;a!=b;++a) cerr<<*a<<' ';cerr<<endl;}
typedef long long int lint;
typedef pair<int,int> pi;
lint n,m,mod;

lint calc(lint a,lint b){
	if(a%2==0) return (a/2)%mod*(b%mod)%mod;
	return a%mod*((b/2)%mod)%mod;
}
lint floor(lint a,lint b){
	if(a>=0) return a/b;
	if(a%b==0) return a/b;
	return a/b-1;
}
lint solve(lint L,lint R,lint l,lint r,lint low,lint up,lint C,lint dif){
	if(l<=L && R<=r){
		lint begin=floor(low-C+dif-1,dif),end=floor(up-1-C,dif);
		begin=max(begin,0ll);

		end=min(end,R-L-1);
		++end;
		if(begin>=end) return 0;
		//[begin...end)
		int len=end-begin;
		lint ans=(len+C%mod*(len%mod)%mod+(calc(begin+end-1,len)%mod*(dif%mod)%mod))%mod;
		return ans;
	}else if(r<=L || R<=l) return 0;

	lint md=(L+R+1)>>1;
	lint A=solve(L,md,l,r,low,up,C,dif*2);
	lint B=solve(md,R,l,r,low,up,C+dif,dif*2);
	return (A+B)%mod;
}

int main(){
	cin>>n>>m>>mod;

	REP(hoge,m){
		lint l,r,u,v;
		cin>>l>>r>>u>>v;

		--u;
		cout<<solve(0,n,l,r,u,v,0,1)<<endl;
	}

	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<vector>
#define REP(i,m) for(int i=0;i<m;++i)
#define REPN(i,m,in) for(int i=in;i<m;++i)
#define ALL(t) (t).begin(),(t).end()
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define dump(x)  cerr << #x << " = " << (x) << endl
#define prl cerr<<"called:"<< __LINE__<<endl
using namespace std;
static const int INF =500000000;
template<class T> void debug(T a,T b){ for(;a!=b;++a) cerr<<*a<<' ';cerr<<endl;}
typedef long long int lint;
typedef pair<int,int> pi;
lint n,m,mod;

lint calc(lint a,lint b){
	if(a%2==0) return (a/2)%mod*(b%mod)%mod;
	return a%mod*((b/2)%mod)%mod;
}
lint solve(lint L,lint R,lint l,lint r,lint low,lint up,lint C,lint dif){
	if(l<=L && R<=r){
		lint begin=(low-C+dif-1)/dif,end=((up-1)-C)/dif;
		begin=max(begin,0ll);

		end=min(end,r-l-1);
		++end;
		if(begin>=end) return 0;
		//[begin...end)
		int len=end-begin;
		lint ans=(len+C%mod*(len%mod)%mod+(calc(begin+end-1,len)*(dif%mod)%mod))%mod;
		return ans;
	}else if(r<=L || R<=l) return 0;

	lint md=(L+R)>>1;
	lint A=solve(L,md,l,r,low,up,C,dif*2);
	lint B=solve(md,R,l,r,low,up,C+dif,dif*2);
	return (A+B)%mod;
}

int main(){
	cin>>n>>m>>mod;

	REP(hoge,m){
		int l,r,u,v;
		scanf("%d%d%d%d",&l,&r,&u,&v);--l;

		--u;
		cout<<solve(0,n,l,r,u,v,0,1)<<endl;
	}

	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<vector>
#define REP(i,m) for(int i=0;i<m;++i)
#define REPN(i,m,in) for(int i=in;i<m;++i)
#define ALL(t) (t).begin(),(t).end()
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define dump(x)  cerr << #x << " = " << (x) << endl
#define prl cerr<<"called:"<< __LINE__<<endl
using namespace std;
static const int INF =500000000;
template<class T> void debug(T a,T b){ for(;a!=b;++a) cerr<<*a<<' ';cerr<<endl;}
typedef long long int lint;
typedef pair<int,int> pi;
lint n,m,mod;

lint calc(lint a,lint b){
	if(a%2==0) return (a/2)%mod*(b%mod)%mod;
	return a%mod*((b/2)%mod)%mod;
}
lint solve(lint L,lint R,lint l,lint r,lint low,lint up,lint C,lint dif){
	if(l<=L && R<=r){
		lint begin=(low-C+dif-1)/dif,end=((up-1)-C)/dif;
		begin=max(begin,0ll);

		end=min(end,R-L-1);
		++end;
		if(begin>=end) return 0;
		//[begin...end)
		int len=end-begin;
		lint ans=(len+C%mod*(len%mod)%mod+(calc(begin+end-1,len)*(dif%mod)%mod))%mod;
		return ans;
	}else if(r<=L || R<=l) return 0;

	lint md=(L+R+1)>>1;
	lint A=solve(L,md,l,r,low,up,C,dif*2);
	lint B=solve(md,R,l,r,low,up,C+dif,dif*2);
	return (A+B)%mod;
}

int main(){
	cin>>n>>m>>mod;

	REP(hoge,m){
		int l,r,u,v;
		scanf("%d%d%d%d",&l,&r,&u,&v);--l;

		--u;
		cout<<solve(0,n,l,r,u,v,0,1)<<endl;
	}

	return 0;
}


#include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <bitset>
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
#define li       	long long int
#define rep(i,to)	for(li i=0;i<((li)(to));++i)
#define pb       	push_back
#define sz(v)    	((li)(v).size())
#define bit(n)   	(1ll<<(li)(n))

li n,m,mod;
li low,high;
li recur(li left,li right,li a,li b,li l,li r){
	if(right<=l || r<=left) return 0;
	if(l<=left && right<=r){
		if(high<a) return 0;
		if(a+b*(right-left-1)<low) return 0;
		li LOW=(a<low)?((low-a-1)/b+1):0;
		li HIGH=min((right-left),(high-a)/b+1);
		LOW--; HIGH--; LOW%=mod; HIGH%=mod; a%=mod; b%=mod;
		li res0=((LOW+1)*(LOW)/2)%mod;
		li res1=((HIGH+1)*(HIGH)/2)%mod;
		res0=(res0*b)%mod;
		res1=(res1*b)%mod;
		return (res1-res0+(HIGH-LOW)*(a%mod)+mod)%mod;
	}
	li mid=(left+right+1)/2;
	li res0=recur(left,mid,a,b*2,l,r);
	li res1=recur(mid,right,a+b,b*2,l,r);
	return res0+res1;
}

int main(){
	cin>>n>>m>>mod;
	rep(i,m){
		li l,r;
		cin>>l>>r>>low>>high;
		cout<<recur(0,n,1,1,l-1,r)<<endl;
	}
}


#include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <bitset>
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
#define li       	long long int
#define rep(i,to)	for(li i=0;i<((li)(to));++i)
#define pb       	push_back
#define sz(v)    	((li)(v).size())
#define bit(n)   	(1ll<<(li)(n))

li n,m,mod;
li low,high;
li recur(li left,li right,li a,li b,li l,li r){
	if(right<=l || r<=left) return 0;
	if(l<=left && right<=r){
		if(high<a) return 0;
		if(a+b*(right-left-1)<low) return 0;
		li LOW=(a<low)?((low-a-1)/b+1):0;
		li HIGH=min((right-left),(high-a)/b+1);
		LOW--; HIGH--; LOW%=mod; HIGH%=mod; a%=mod; b%=mod;
		li res0=((LOW+1)*(LOW)/2)%mod;
		li res1=((HIGH+1)*(HIGH)/2)%mod;
		res0=(res0*b)%mod;
		res1=(res1*b)%mod;
		return (res1-res0+(HIGH-LOW)*(a%mod)+mod)%mod;
	}
	li mid=(left+right+1)/2;
	li res0=recur(left,mid,a,b*2,l,r);
	li res1=recur(mid,right,a+b,b*2,l,r);
	return (res0+res1)%mod;
}

int main(){
	cin>>n>>m>>mod;
	rep(i,m){
		li l,r;
		cin>>l>>r>>low>>high;
		cout<<recur(0,n,1,1,l-1,r)<<endl;
	}
}


#include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <bitset>
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
#define li       	long long int
#define rep(i,to)	for(li i=0;i<((li)(to));++i)
#define pb       	push_back
#define sz(v)    	((li)(v).size())
#define bit(n)   	(1ll<<(li)(n))

li n,m,mod;
li low,high;
li recur(li left,li right,li a,li b,li l,li r){
	if(right<=l || r<=left) return 0;
	if(l<=left && right<=r){
		if(high<a) return 0;
		if(a+b*(right-left-1)<low) return 0;
		li LOW=(a<low)?((low-a-1)/b+1):0;
		li HIGH=min((right-left),(high-a)/b+1);
		LOW--; HIGH--; LOW%=mod; HIGH%=mod; a%=mod; b%=mod;
		li res0=((LOW+1)*(LOW)/2)%mod;
		li res1=((HIGH+1)*(HIGH)/2)%mod;
		li res2=(HIGH-LOW)*a;
		res0=(res0*b)%mod;
		res1=(res1*b)%mod;
		res2=(res2)%mod;
		return (res1-res0+res2+mod+mod)%mod;
	}
	li mid=(left+right+1)/2;
	li res0=recur(left,mid,a,b*2,l,r);
	li res1=recur(mid,right,a+b,b*2,l,r);
	return (res0+res1)%mod;
}

int main(){
	cin>>n>>m>>mod;
	rep(i,m){
		li l,r;
		cin>>l>>r>>low>>high;
		cout<<recur(0,n,1,1,l-1,r)<<endl;
	}
}


#include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
#define li       	long long int
#define rep(i,to)	for(li i=0;i<((li)(to));i++)
#define pb       	push_back
#define sz(v)    	((li)v.size())
#define bit(n)   	(1ll<<(li)(n))

li n,m,mod;
li mini,maxi;
li recur(li left,li right,li l,li r,li a,li b){
	if(right<=l || r<=left) return 0;
	if(l<=left && right<=r){
		li LEFT=(mini-b-1)/a+1;
		if(mini<=b) LEFT=0;
		li RIGHT=(maxi-b)/a+1;
		if(maxi<b) RIGHT=0;
		right=min(right-left,RIGHT);
		left=max(0ll,LEFT);
		if(right<=left) return 0;
		li left_sum=((left-1)*left/2)%mod;
		li right_sum=((right-1)*right/2)%mod;
		li res=(a*((right_sum-left_sum+mod)%mod)+(right-left)*b)%mod;
		return res;
	}
	li mid=(left+right+1)/2;
	li res=0;
	res+=recur(left,mid,l,r,a*2,b);
	res+=recur(mid,right,l,r,a*2,b+a);
	return res%mod;
}

int main(){
	scanf("%I64d%I64d%I64d",&n,&m,&mod);
//	cin>>n>>m>>mod;
	rep(i,m){
		li a,b;
		scanf("%I64d%I64d%I64d%I64d",&a,&b,&mini,&maxi);
//		cin>>a>>b>>mini>>maxi;
//		cout<<recur(1,n+1,a,b+1,1,1)<<endl;
		printf("%I64d\n",recur(1,n,a,b,1,1));
	}
}


#include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <bitset>
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
#define li       	long long int
#define rep(i,to)	for(li i=0;i<((li)(to));++i)
#define pb       	push_back
#define sz(v)    	((li)(v).size())
#define bit(n)   	(1ll<<(li)(n))

li n,m,mod;
li low,high;
li recur(li left,li right,li a,li b,li l,li r){
	if(right<=l || r<=left) return 0;
	if(l<=left && right<=r){
		if(high<a) return 0;
		li LOW=(a<low)?((low-a-1)/b+1):0;
		li HIGH=min((right-left),(high-a-1)/b+1);
		LOW--; HIGH--; LOW%=mod; HIGH%=mod; a%=mod; b%=mod;
		li res0=((LOW+1)*(LOW)/2)%mod;
		li res1=((HIGH+1)*(HIGH)/2)%mod;
		res0=(res0*b)%mod;
		res1=(res1*b)%mod;
		return (res1-res0+(HIGH-LOW)*(a%mod)+mod)%mod;
	}
	li mid=(left+right+1)/2;
	li res0=recur(left,mid,a,b*2,l,r);
	li res1=recur(mid,right,a+b,b*2,l,r);
	return res0+res1;
}

int main(){
	cin>>n>>m>>mod;
	rep(i,m){
		li l,r;
		cin>>l>>r>>low>>high;
		cout<<recur(0,n,1,1,l-1,r)<<endl;
	}
}


#include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
#define li       	long long int
#define rep(i,to)	for(li i=0;i<((li)(to));i++)
#define pb       	push_back
#define sz(v)    	((li)v.size())
#define bit(n)   	(1ll<<(li)(n))

li n,m,mod;
li mini,maxi;
li recur(li left,li right,li l,li r,li a,li b){
	if(right<=l || r<=left) return 0;
	if(l<=left && right<=r){
		li LEFT=(mini-b-1)/a+1;
		if(mini<=b) LEFT=0;
		li RIGHT=(maxi-b)/a+1;
		if(maxi<b) RIGHT=0;
		right=min(right-left,RIGHT);
		left=max(0ll,LEFT);
		if(right<=left) return 0;
		li left_sum=((left-1)*left/2)%mod;
		li right_sum=((right-1)*right/2)%mod;
		li res=(a*((right_sum-left_sum+mod)%mod)+(right-left)*b)%mod;
		return res;
	}
	li mid=(left+right+1)/2;
	li res=0;
	res+=recur(left,mid,l,r,a*2,b);
	res+=recur(mid,right,l,r,a*2,b+a);
	return res%mod;
}

int main(){
	scanf("%I64d%I64d%I64d",&n,&m,&mod);
//	cin>>n>>m>>mod;
	rep(i,m){
		li a,b;
		scanf("%I64d%I64d%I64d%I64d",&a,&b,&mini,&maxi);
//		cin>>a>>b>>mini>>maxi;
//		cout<<recur(1,n+1,a,b+1,1,1)<<endl;
		printf("%I64d\n",recur(1,n+1,a,b+1,1,1));
	}
}


#include <fstream>
#include <iostream>

using namespace std;

long long n;
int num_queries, mod;

pair <int, long long> Query(long long n, int x, long long upper) {
  if (x == 0 || upper == 0) {
    return make_pair(0, 0);
  }
  if (n == 1) {
    return make_pair(1, 1);
  }
  long long middle = (n+1)/2;

  if (x < middle) {
    pair <int, long long> res = Query(middle, x, (upper+1)/2);
    int sum = (2*res.first-res.second) % mod;
    if (sum < 0) {
      sum += mod;
    }
    return make_pair(sum, res.second);
  } else {
    pair <int, long long> res = Query(n-middle, x-middle, upper/2);
    int alpha = ((min(n, upper)+1)/2) % mod;
    int sum = (alpha*alpha + 2*res.first) % mod;
    return make_pair(sum, alpha+res.second);
  }
}

int main() {
#ifndef ONLINE_JUDGE
  ifstream cin("d.in");
#endif

  cin >> n >> num_queries >> mod;

  for (int i = 1; i <= num_queries; ++i) {
    int x, y;
    long long lower, upper;
    cin >> x >> y >> lower >> upper;
    int result = (Query(n, y, upper).first-Query(n, x-1, upper).first-
                  Query(n, y, lower-1).first+Query(n, x-1, lower-1).first) % mod;
    if (result < 0) {
      result += mod;
    }
    cout << result << '\n';
  }

  return 0;
}


#include <fstream>
#include <iostream>

using namespace std;

long long n;
int num_queries, mod;

pair <int, long long> Query(long long n, long long x, long long upper) {
  if (x == 0 || upper == 0) {
    return make_pair(0, 0);
  }
  if (n == 1) {
    return make_pair(1, 1);
  }

  long long middle = (n+1)/2;
  if (x < middle) {
    pair <int, long long> res = Query(middle, x, (upper+1)/2);
    int sum = (2*res.first-res.second) % mod;
    if (sum < 0) {
      sum += mod;
    }
    return make_pair(sum, res.second);
  } else {
    pair <int, long long> res = Query(n-middle, x-middle, upper/2);
    int alpha = ((min(n, upper)+1)/2) % mod;
    int sum = (alpha*alpha + 2*res.first) % mod;
    return make_pair(sum, alpha+res.second);
  }
}

int main() {
#ifndef ONLINE_JUDGE
  ifstream cin("d.in");
#endif

  cin >> n >> num_queries >> mod;

  for (int i = 1; i <= num_queries; ++i) {
    long long x, y, lower, upper;
    cin >> x >> y >> lower >> upper;
    int result = (Query(n, y, upper).first-Query(n, x-1, upper).first-
                  Query(n, y, lower-1).first+Query(n, x-1, lower-1).first) % mod;
    if (result < 0) {
      result += mod;
    }
    cout << result << '\n';
  }

  return 0;
}


#include <fstream>
#include <iostream>

using namespace std;

long long n;
int num_queries, mod;

pair <int, long long> Query(long long n, int x, long long upper) {
  if (x == 0 || upper == 0) {
    return make_pair(0, 0);
  }
  if (n == 1) {
    return make_pair(1, 1);
  }

  long long middle = (n+1)/2;
  if (x < middle) {
    pair <int, long long> res = Query(middle, x, (upper+1)/2);
    int sum = (2*res.first-res.second) % mod;
    if (sum < 0) {
      sum += mod;
    }
    return make_pair(sum, res.second);
  } else {
    pair <int, long long> res = Query(n-middle, x-middle, upper/2);
    int alpha = ((min(n, upper)+1)/2) % mod;
    int sum = (alpha*alpha + 2*res.first) % mod;
    return make_pair(sum, alpha+res.second);
  }
}

int main() {
#ifndef ONLINE_JUDGE
  ifstream cin("d.in");
#endif

  cin >> n >> num_queries >> mod;

  for (int i = 1; i <= num_queries; ++i) {
    long long x, y, lower, upper;
    cin >> x >> y >> lower >> upper;
    int result = (Query(n, y, upper).first-Query(n, x-1, upper).first-
                  Query(n, y, lower-1).first+Query(n, x-1, lower-1).first) % mod;
    if (result < 0) {
      result += mod;
    }
    cout << result << '\n';
  }

  return 0;
}


#include <fstream>
#include <iostream>
#include <cassert>

using namespace std;

long long n;
int num_queries, mod;

pair <int, long long> Query(long long n, long long x, long long upper) {
  if (x == 0 || upper == 0) {
    return make_pair(0, 0);
  }
  if (n == 1) {
    return make_pair(1, 1);
  }

  long long middle = (n+1)/2;
  if (x < middle) {
    pair <int, long long> res = Query(middle, x, (upper+1)/2);
    int sum = (2*res.first-res.second) % mod;
    if (sum < 0) {
      sum += mod;
    }
    return make_pair(sum, res.second);
  } else {
    pair <int, long long> res = Query(n-middle, x-middle, upper/2);
    int alpha = ((min(n, upper)+1)/2) % mod;
    int sum = ((long long) alpha*alpha + 2*res.first) % mod;
    return make_pair(sum, alpha+res.second);
  }
}

int main() {
#ifndef ONLINE_JUDGE
  ifstream cin("d.in");
#endif

  cin >> n >> num_queries >> mod;

  for (int i = 1; i <= num_queries; ++i) {
    long long x, y, lower, upper;
    cin >> x >> y >> lower >> upper;
    int result = (Query(n, y, upper).first-Query(n, x-1, upper).first-
                  Query(n, y, lower-1).first+Query(n, x-1, lower-1).first) % mod;
    if (result < 0) {
      result += mod;
    }
    cout << result << '\n';
  }

  return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

typedef long long LL;
LL l,r,u,v,P,ans;
void sol(LL n, LL L, LL R, LL a0, LL d)
{
    if(v<a0 || u>a0+(n-1)*d)return;
    if(l<=L && R<=r)
    {
        LL st = a0;
        if(u>a0)st = a0+(u-a0+d-1)/d*d;
        LL ed = a0+(n-1)*d;
        if(v<a0+(n-1)*d)ed = a0+(v-a0)/d*d;
        if(st<=ed)
            ans = (ans+((ed-st)/d+1)%P*((st+ed)%P)%P)%P;
        return;
    }
    int mid = (L+R)/2;
    if(l<=mid)sol((n+1)/2,L,mid,a0,d*2);
    if(r>mid)sol(n/2,mid+1,R,a0+d,d*2);
}

int main()
{
    LL n;
    int m;
    scanf("%I64d%d%I64d",&n,&m,&P);
    P*=2;
    for(int i = 0; i < m; i++)
    {
        scanf("%I64d%I64d%I64d%I64d",&l,&r,&u,&v);
        ans = 0;
        sol(n,1,n,1,1);
        printf("%I64d\n",ans/2);
    }

    return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

typedef long long LL;
LL l,r,u,v,P,ans;
void sol(LL n, LL L, LL R, LL a0, LL d)
{
    if(v<a0 || u>a0+(n-1)*d)return;
    if(l<=L && R<=r)
    {
        LL st = a0;
        if(u>a0)st = a0+(u-a0+d-1)/d*d;
        LL ed = a0+d*min(n-1,(v-a0)/d);
        if(st<=ed)
            ans = (ans+(((ed-st)/d+1)%P)*((st+ed)%P)%P)%P;
        return;
    }
    int mid = (L+R)/2;
    if(l<=mid)sol((n+1)/2,L,mid,a0,d*2);
    if(r>mid)sol(n/2,mid+1,R,a0+d,d*2);
}

int main()
{
    LL n;
    int m;
    scanf("%I64d%d%I64d",&n,&m,&P);
    P*=2;
    for(int i = 0; i < m; i++)
    {
        scanf("%I64d%I64d%I64d%I64d",&l,&r,&u,&v);
        ans = 0;
        sol(n,1,n,1,1);
        printf("%I64d\n",ans/2);
    }

    return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

typedef long long LL;
LL l,r,u,v,P,ans;
void sol(LL n, LL L, LL R, LL a0, LL d)
{
    if(v<a0 || u>a0+(n-1)*d)return;
    if(l<=L && R<=r)
    {
        LL st = max(a0, a0+(u-a0+d-1)/d*d);
        LL ed = min(a0+(n-1)*d, a0+(v-a0)/d*d);
        ans = (ans+((ed-st)/d+1)*(st+ed)%P)%P;
        return;
    }
    int mid = (L+R)/2;
    if(l<=mid)sol((n+1)/2,L,mid,a0,d*2);
    if(r>mid)sol(n/2,mid+1,R,a0+d,d*2);
}

int main()
{
    LL n;
    int m;
    scanf("%I64d%d%I64d",&n,&m,&P);
    P*=2;
    for(int i = 0; i < m; i++)
    {
        scanf("%I64d%I64d%I64d%I64d",&l,&r,&u,&v);
        ans = 0;
        sol(n,1,n,1,1);
        printf("%I64d\n",ans/2);
    }

    return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

typedef long long LL;
LL l,r,u,v,P,ans;
void sol(LL n, LL L, LL R, LL a0, LL d)
{
    if(v<a0 || u>a0+(n-1)*d)return;
    if(l<=L && R<=r)
    {
        LL st = a0;
        if(u>a0)st = a0+(u-a0+d-1)/d*d;
        LL ed = a0+d*min(n-1,(v-a0)/d);
        ans = (ans+(((ed-st)/d+1)%P)*((st+ed)%P)%P)%P;
        return;
    }
    int mid = (L+R)/2;
    if(l<=mid)sol((n+1)/2,L,mid,a0,d*2);
    if(r>mid)sol(n/2,mid+1,R,a0+d,d*2);
}

int main()
{
    LL n;
    int m;
    scanf("%I64d%d%I64d",&n,&m,&P);
    P*=2;
    for(int i = 0; i < m; i++)
    {
        scanf("%I64d%I64d%I64d%I64d",&l,&r,&u,&v);
        if(l>r)while(1);
        ans = 0;
        sol(n,1,n,1,1);
        printf("%I64d\n",ans/2);
    }

    return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

typedef long long LL;
LL l,r,u,v,P,ans;
void sol(LL n, LL L, LL R, LL a0, LL d)
{
    if(v<a0 || u>a0+(n-1)*d)return;
    if(l<=L && R<=r)
    {
        LL st = max(a0, a0+(u-a0+d-1)/d*d);
        LL ed = min(a0+(n-1)*d, a0+(v-a0)/d*d);
        ans = (ans+((ed-st)/d+1)%P*((st+ed)%P)%P)%P;
        return;
    }
    int mid = (L+R)/2;
    if(l<=mid)sol((n+1)/2,L,mid,a0,d*2);
    if(r>mid)sol(n/2,mid+1,R,a0+d,d*2);
}

int main()
{
    LL n;
    int m;
    scanf("%I64d%d%I64d",&n,&m,&P);
    P*=2;
    for(int i = 0; i < m; i++)
    {
        scanf("%I64d%I64d%I64d%I64d",&l,&r,&u,&v);
        ans = 0;
        sol(n,1,n,1,1);
        printf("%I64d\n",ans/2);
    }

    return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

typedef long long LL;
LL l,r,u,v,P,ans;
void sol(LL n, LL L, LL R, LL a0, LL d)
{
    if(v<a0 || u>a0+(n-1)*d)return;
    if(l<=L && R<=r)
    {
        LL st = a0;
        if(u>a0)st = a0+(u-a0+d-1)/d*d;
        LL ed = a0+d*min(n-1,(v-a0)/d);
        ans = (ans+(((ed-st)/d+1)%P)*((st+ed)%P)%P)%P;
        return;
    }
    LL mid = (L+R)/2;
    if(l<=mid)sol((n+1)/2,L,mid,a0,d*2);
    if(r>mid)sol(n/2,mid+1,R,a0+d,d*2);
}

int main()
{
    LL n;
    int m;
    scanf("%I64d%d%I64d",&n,&m,&P);
    P*=2;
    for(int i = 0; i < m; i++)
    {
        scanf("%I64d%I64d%I64d%I64d",&l,&r,&u,&v);
        ans = 0;
        sol(n,1,n,1,1);
        printf("%I64d\n",ans/2);
    }

    return 0;
}


/*
 in the name of GOD
 sepehr! shirali
*/
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>

using namespace std;

#define ff first
#define ss second
#define pb push_back
#define mp make_pair
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;

#define mid (lo + hi >> 1)

ll n, m, mod, v, u, L, R;

ll rmq(ll l, ll r, ll lo = 1, ll hi = n, ll cur = 1, ll x = u, ll y = min(v,n), ll z = u) {
     cout << lo << ' ' << hi << ' ' << x << ' ' << y << ' ' << z << endl;
     if(hi < l || lo > r || !z) return 0;
     cur %= mod, z %= mod;
     if(l <= lo && r >= hi) {
          ll ans  = ((y - x + 1)%mod)*(z%mod); ans%=mod;
          ll res = 0;
          if((y - x)%2) res = (y - x + 1)/2, res %= mod, res *= ((y - x)%mod), res %= mod, res *= cur, res %= mod;
          else res = (y - x)/2, res %= mod, res *= ((y - x+ 1)%mod), res%=mod, res *= cur, res %= mod;
          ans += res;
          return ans;
     }
     ll a, b, c = z, ans = 0;
     if((x - lo)%2) c += cur, c *= (x != y);
     if((x - lo)%2) a = (x - lo + 2)/2+1;
     else a = (x - lo + 1)/2+1;
     if((y - lo)%2) b = (y - lo)/2+1;
     else b = (y - lo + 1)/2+1;
     ans = rmq(l, r, lo, mid, 2*cur, lo+a-1, lo+b-1, c), ans%=mod;
     if((x - lo + 1)%2) c = z + cur, c *= (y != x);
     else c = z;
     if((x - lo + 1)%2) a = (x - lo + 2)/2;
     else a = (x - lo + 1)/2;
     if((y - lo + 1)%2) b = (y - lo)/2;
     else b = (y - lo + 1)/2;
     ans += rmq(l, r, mid+1, hi, 2*cur, mid+a, mid+b, c), ans%=mod;
     return ans;
}

int main() {
     cin >> n >> m >> mod;
     for(int i=0;i<m;i++) {
          cin >> L >> R >> u >> v;
          if(u > n) cout << 0 << endl;
          else cout << rmq(L,R) << endl;
     }
     //system("pause");
}


/*
 in the name of GOD
 sepehr! shirali
*/
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>

using namespace std;

#define ff first
#define ss second
#define pb push_back
#define mp make_pair
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;

#define mid (lo + hi >> 1)

ll n, m, mod, v, u, L, R;

ll rmq(ll l, ll r, ll lo = 1, ll hi = n, ll cur = 1, ll x = u, ll y = min(v,n), ll z = u) {
     if(hi < l || lo > r || !z) return 0;
     cur %= mod, z %= mod;
     if(l <= lo && r >= hi) {
          ll ans  = ((y - x + 1)%mod)*(z%mod); ans%=mod;
          ll res = 0;
          if((y - x)%2) res = (y - x + 1)/2, res %= mod, res *= ((y - x)%mod), res %= mod, res *= cur, res %= mod;
          else res = (y - x)/2, res %= mod, res *= ((y - x+ 1)%mod), res%=mod, res *= cur, res %= mod;
          ans += res;
          return ans;
     }
     ll a, b, c = z, ans = 0;
     if((x - lo)%2) c += cur, c *= (x != y);
     if((x - lo)%2) a = (x - lo + 2)/2+1;
     else a = (x - lo + 1)/2+1;
     if((y - lo)%2) b = (y - lo)/2+1;
     else b = (y - lo + 1)/2+1;
     ans = rmq(l, r, lo, mid, 2*cur, lo+a-1, lo+b-1, c), ans%=mod;
     if((x - lo + 1)%2) c = z + cur, c *= (y != x);
     else c = z;
     if((x - lo + 1)%2) a = (x - lo + 2)/2;
     else a = (x - lo + 1)/2;
     if((y - lo + 1)%2) b = (y - lo)/2;
     else b = (y - lo + 1)/2;
     ans += rmq(l, r, mid+1, hi, 2*cur, mid+a, mid+b, c), ans%=mod;
     return ans;
}

int main() {
     cin >> n >> m >> mod;
     for(int i=0;i<m;i++) {
          cin >> L >> R >> u >> v;
          if(u > n) cout << 0 << endl;
          else cout << rmq(L,R) << endl;
     }
     //system("pause");
}


/*
 in the name of GOD
 sepehr! shirali
*/
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>

using namespace std;

#define ff first
#define ss second
#define pb push_back
#define mp make_pair
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;

#define mid (lo + hi >> 1)

ll n, m, mod, v, u, L, R;

ll rmq(ll l, ll r, ll lo = 1, ll hi = n, ll cur = 1, ll x = u, ll y = min(v,n), ll z = u) {
     if(hi < l || lo > r || !z) return 0;
     cur %= mod, z %= mod;
     if(l <= lo && r >= hi) {
          ll ans  = ((y - x + 1)%mod)*(z%mod); ans%=mod;
          ll res;
          if((y - x)%2) res = (y - x + 1)/2, res %= mod, res *= ((y - x)%mod), res %= mod, res *= cur, res %= mod;
          else res = (y - x)/2, res %= mod, res *= ((y - x+ 1)%mod), res%=mod, res *= cur, res %= mod;
          ans += res;
          return ans;
     }
     ll a = x, b, c = z, ans = 0;
     if((x - lo)%2) c += cur, c *= (x != y);
     if((x - lo)%2) a = (x - lo + 2)/2+1;
     else a = (x - lo + 1)/2+1;
     if((y - lo)%2) b = (y - lo)/2+1;
     else b = (y - lo + 1)/2+1;
     ans = rmq(l, r, lo, mid, 2*cur, a, b, c), ans%=mod;
     if((x - lo + 1)%2) c = z + cur;
     else c = z;
     if((x - lo + 1)%2) a = (x - lo + 2)/2;
     else a = (x - lo + 1)/2;
     if((y - lo + 1)%2) b = (y - lo)/2;
     else b = (y - lo + 1)/2;
     ans += rmq(l, r, mid+1, hi, 2*cur, a, b, c), ans%=mod;
     return ans;
}

int main() {
     cin >> n >> m >> mod;
     for(int i=0;i<m;i++) {
          cin >> L >> R >> u >> v;
          if(u > n) cout << 0 << endl;
          else cout << rmq(L,R) << endl;
     }
     //system("pause");
}


#include <iostream>

#define lli long long int

using namespace std;

lli n,m,mod,u,v;

lli find( lli a,lli b,lli x ){
	lli t = (x+b)/a%mod;
	return (a*t%mod*(t+1)/2%mod-t*b%mod+mod)%mod;
}

lli query( lli bas,lli son,lli x,lli y,lli a,lli b ){
	if( son<x or y<bas )	return 0;
	if( x<=bas and son<=y )	return (find( a,b,v )-find( a,b,u-1 )+mod)%mod;
	lli orta=(bas+son)/2;
	return (query( bas,orta,x,y,a*2,a+b )+query( orta+1,son,x,y,a*2,b ))%mod;
}

int main(){

	ios_base::sync_with_stdio( false );

	cin >> n >> m >> mod;

	lli l,r;

	while( m-- ){
		cin >> l >> r >> u >> v;
		cout << query( 1,n,l,r,1,0 ) << endl;
	}

}


#include <iostream>

#define lli long long int

using namespace std;

lli n,m,mod,u,v;

lli find( lli a,lli b,lli x ){
	lli t = (x+b)/a%mod;
	return (a*t%mod*(t+1)/2%mod-t*b%mod+mod)%mod;
}

lli query( lli bas,lli son,lli x,lli y,lli a,lli b ){
	if( son<x or y<bas )	return 0;
	if( x<=bas and son<=y )	return (find( a,b,v )-find( a,b,u-1 )+mod)%mod;
	lli orta=(bas+son)/2;
	return (query( bas,orta,x,y,a*2,a+b )+query( orta+1,son,x,y,a*2,b ))%mod;
}

int main(){

	ios_base::sync_with_stdio( false );

	cin >> n >> m >> mod;

	lli l,r;

	while( m-- ){
		cin >> l >> r >> u >> v;
		if( u>n ){
			cout << 0 << endl;
			continue;
		}
		v = min( v,n );
		cout << query( 1,n,l,r,1,0 ) << endl;
	}

}


#include <iostream>

#define lli long long int

using namespace std;

lli n,m,mod,u,v;

lli find( lli a,lli b,lli x ){
	lli t = (x+b)/a%mod;
	return (a*(t+1)/2%mod*t%mod-t*b%mod+mod)%mod;
}

lli query( lli bas,lli son,lli x,lli y,lli a,lli b ){
	if( son<x or y<bas )	return 0;
	if( x<=bas and son<=y )	return (find( a,b,v )-find( a,b,u-1 )+mod)%mod;
	lli orta=(bas+son)/2;
	return (query( bas,orta,x,y,a*2,a+b )+query( orta+1,son,x,y,a*2,b ))%mod;
}

int main(){

	ios_base::sync_with_stdio( false );

	cin >> n >> m >> mod;

	lli l,r;

	while( m-- ){
		cin >> l >> r >> u >> v;
		if( u>n ){
			cout << 0 << endl;
			continue;
		}
		v = min( v,n );
		cout << query( 1,n,l,r,1,0 ) << endl;
	}

}


#include <iostream>

#define lli long long int

using namespace std;

lli n,m,mod,u,v;

lli find( lli a,lli b,lli x ){
	lli t = (x+b)/a;
	if( t%2==0 )	return (t/2%mod*(t+1)%mod*a%mod-t*b%mod+mod)%mod;
	else	return ((t+1)/2%mod*t%mod*a%mod-t*b%mod+mod)%mod;
}

lli query( lli bas,lli son,lli x,lli y,lli a,lli b ){
	if( son<x or y<bas )	return 0;
	if( x<=bas and son<=y )	return (find( a,b,v )-find( a,b,u-1 )+mod)%mod;
	lli orta=(bas+son)/2;
	return (query( bas,orta,x,y,a*2,a+b )+query( orta+1,son,x,y,a*2,b ))%mod;
}

int main(){

	ios_base::sync_with_stdio( false );

	cin >> n >> m >> mod;

	lli l,r;

	while( m-- ){
		cin >> l >> r >> u >> v;
		if( u>n ){
			cout << 0 << endl;
			continue;
		}
		v = min( v,n );
		cout << query( 1,n,l,r,1,0 ) << endl;
	}

}


#include <iostream>

#define lli long long int

using namespace std;

lli n,m,mod,u,v;

lli find( lli a,lli b,lli x ){
	lli t = (x+b)/a%mod;
	return (t*(t+1)/2%mod*a%mod-t*b%mod+mod)%mod;
}

lli query( lli bas,lli son,lli x,lli y,lli a,lli b ){
	if( son<x or y<bas )	return 0;
	if( x<=bas and son<=y )	return (find( a,b,v )-find( a,b,u-1 )+mod)%mod;
	lli orta=(bas+son)/2;
	return (query( bas,orta,x,y,a*2,a+b )+query( orta+1,son,x,y,a*2,b ))%mod;
}

int main(){

	ios_base::sync_with_stdio( false );

	cin >> n >> m >> mod;

	lli l,r;

	while( m-- ){
		cin >> l >> r >> u >> v;
		if( u>n ){
			cout << 0 << endl;
			continue;
		}
		v = min( v,n );
		cout << query( 1,n,l,r,1,0 ) << endl;
	}

}


#include <iostream>

#define lli long long int

using namespace std;

lli n,m,mod,u,v;

lli find( lli a,lli b,lli x ){
	lli t = (x+b)/a;
	if( t%2==0 )	return (t/2%mod*((t+1)%mod)%mod*(a%mod)%mod-t%mod*(b%mod)%mod+mod)%mod;
	else		return ((t+1)/2%mod*(t%mod)%mod*(a%mod)%mod-t%mod*(b%mod)%mod+mod)%mod;
}

lli query( lli bas,lli son,lli x,lli y,lli a,lli b ){
	if( son<x or y<bas )	return 0;
	if( x<=bas and son<=y )	return (find( a,b,v )-find( a,b,u-1 )+mod)%mod;
	lli orta=(bas+son)/2;
	return (query( bas,orta,x,y,a*2,a+b )+query( orta+1,son,x,y,a*2,b ))%mod;
}

int main(){

	ios_base::sync_with_stdio( false );

	cin >> n >> m >> mod;

	lli l,r;

	while( m-- ){
		cin >> l >> r >> u >> v;
		if( u>n ){
			cout << 0 << endl;
			continue;
		}
		v = min( v,n );
		cout << query( 1,n,l,r,1,0 ) << endl;
	}

}


#include <iostream>

using namespace std;

typedef long long LL;

LL n,m,Mod,l,r,u,v;
LL calc(LL a,LL d,LL n,LL l,LL r)
{
	if ( v < a || u>a+d*(n-1))
        return 0;
    if (l==1 && r==n)
	{
		if (u<=a)
            l=1;
        else
            l=(u-a-1)/d+2;
		if (v>=a+d*(n-1))
            r=n;
        else
            r=(v-a)/d+1;
		LL x=a*2+(l+r-2)*d;
		LL y=r-l+1;
		return (x * (y%Mod)) %Mod /2;
	}
	LL mid=(n+1)/2;
	if (r<=mid)
        return calc(a,d*2,mid,l,r)% Mod;
	if (l>mid)
        return calc(a+d,d*2,n-mid,l-mid,r-mid)%Mod;
	return ( calc(a,d*2,mid,l,mid) + calc(a+d,d*2,n-mid,1,r-mid) )%Mod;
}
int main()
{
    cin >> n>>m>>Mod;
    for(int i=0;i<m;i++)
    {
        cin>>l>>r>>u>>v;
        cout<<calc(1,1,n,l,r)<<endl;
    }
    return 0;
}


#include <iostream>

using namespace std;

typedef long long LL;

LL n,m,Mod,l,r,u,v;
LL calc(LL a,LL d,LL n,LL l,LL r)
{
	if ( v < a || u>a+d*(n-1))
        return 0;
    if (l==1 && r==n)
	{
		if (u<=a)
            l=1;
        else
            l=(u-a-1)/d+2;
		if (v>=a+d*(n-1))
            r=n;
        else
            r=(v-a)/d+1;
		LL x=a*2+(l+r-2)*d;
		LL y=r-l+1;
		if(x%2==0)
            x/=2;
        else
            y/=2;
		return ((x % Mod) * (y%Mod))%Mod;
	}
	LL mid=(n+1)/2;
	if (r<=mid)
        return calc(a,d*2,mid,l,r)% Mod;
	if (l>mid)
        return calc(a+d,d*2,n-mid,l-mid,r-mid)%Mod;
	return ( calc(a,d*2,mid,l,mid) + calc(a+d,d*2,n-mid,1,r-mid) )%Mod;
}
int main()
{
    cin >> n>>m>>Mod;
    for(int i=0;i<m;i++)
    {
        cin>>l>>r>>u>>v;
        cout<<calc(1,1,n,l,r)<<endl;
    }
    return 0;
}


#include <iostream>

using namespace std;

typedef long long LL;

LL n,m,Mod,l,r,u,v;
LL calc(LL a,LL d,LL n,LL l,LL r)
{
	if ( v < a || u>a+d*(n-1))
        return 0;
    if (l==1 && r==n)
	{
		if (u<=a)
            l=1;
        else
            l=(u-a-1)/d+2;
		if (v>=a+d*(n-1))
            r=n;
        else
            r=(v-a)/d+1;
		LL x=a*2+(l+r-2)*d;
		LL y=r-l+1;
		return (x * y) %Mod /2;
	}
	LL mid=(n+1)/2;
	if (r<=mid)
        return calc(a,d*2,mid,l,r)% Mod;
	if (l>mid)
        return calc(a+d,d*2,n-mid,l-mid,r-mid)%Mod;
	return ( calc(a,d*2,mid,l,mid) + calc(a+d,d*2,n-mid,1,r-mid) )%Mod;
}
int main()
{
    cin >> n>>m>>Mod;
    for(int i=0;i<m;i++)
    {
        cin>>l>>r>>u>>v;
        cout<<calc(1,1,n,l,r)<<endl;
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <set>
#include <queue>
#include <cmath>
#include <cstring>
#include <cassert>
using namespace std;

#define mp make_pair
#define pb push_back
#define f first
#define s second

int n, m, mod;

long long up(long long a, long long b) {
  return (a + b - 1) / b;
}

long long calc(long long a1, long long d, long long pl, long long pr, long long l, long long r, long long u, long long v) {
  if (l > pr || pl > r) return 0;
  if (pl > pr) return 0;

  if (l <= pl && pr <= r) {
    long long an = a1 + (pr - pl) * d;

    if (u > a1) {
      long long k = up(u - a1, d);
      a1 += k * d;
    }
    if (an > v) {
      long long k = up(an - v, d);
      an -= k * d;
    }
    long long res = 0;

    if (a1 <= an) {
      res = ((a1 + an) / 2) % mod;
      long long n = (an - a1) / d + 1;
      res = (res * n) % mod;
    }
    return res;
  }

  long long res = 0;
  long long pm = (pl + pr) / 2;

  (res += calc(a1, d * 2, pl, pm, l, r, u, v)) %= mod;
  (res += calc(a1 + d, d * 2, pm + 1, pr, l, r, u, v)) %= mod;

  return res;
}

int main() {
  cin >> n >> m >> mod;
  for (int i = 0; i < m; ++i) {
    long long l, r, u, v;
    scanf("%I64d %I64d %I64d %I64d", &l, &r, &u, &v);
    long long sum = calc(1, 1, 1, n, l, r, u, v);
    printf("%I64d\n", sum);
  }
  return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <set>
#include <queue>
#include <cmath>
#include <cstring>
#include <cassert>
using namespace std;

#define mp make_pair
#define pb push_back
#define f first
#define s second

long long n, m, mod;

long long up(long long a, long long b) {
  return (a + b - 1) / b;
}

long long calc(long long a1, long long d, long long pl, long long pr, long long l, long long r, long long u, long long v) {
  if (l > pr || pl > r) return 0;
  if (pl > pr) return 0;

  if (l <= pl && pr <= r) {
    long long an = a1 + (pr - pl) * d;

    if (u > a1) {
      long long k = up(u - a1, d);
      a1 += k * d;
    }
    if (an > v) {
      long long k = up(an - v, d);
      an -= k * d;
    }
    long long res = 0;

    if (a1 <= an) {
      res = ((a1 + an) / 2) % mod;
      long long n = (an - a1) / d + 1;
      res = (res * n) % mod;
    }
    return res;
  }

  long long res = 0;
  long long pm = (pl + pr) / 2;

  (res += calc(a1, d * 2, pl, pm, l, r, u, v)) %= mod;
  (res += calc(a1 + d, d * 2, pm + 1, pr, l, r, u, v)) %= mod;

  return res;
}

int main() {
  cin >> n >> m >> mod;
  for (int i = 0; i < m; ++i) {
    long long l, r, u, v;
    scanf("%I64d %I64d %I64d %I64d", &l, &r, &u, &v);
    long long sum = calc(1, 1, 1, n, l, r, u, v);
    printf("%I64d\n", sum);
  }
  return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <set>
#include <queue>
#include <cmath>
#include <cstring>
#include <cassert>
using namespace std;

#define mp make_pair
#define pb push_back
#define f first
#define s second

long long n, m, mod;

long long up(long long a, long long b) {
  return (a + b - 1) / b;
}

long long calc(long long a1, long long d, long long pl, long long pr, long long l, long long r, long long u, long long v) {
  if (l > pr || pl > r) return 0;
  if (pl > pr) return 0;

  if (l <= pl && pr <= r) {
    long long an = a1 + (pr - pl) * d;

    if (u > a1) {
      long long k = up(u - a1, d);
      a1 += k * d;
    }
    if (an > v) {
      long long k = up(an - v, d);
      an -= k * d;
    }
    long long res = 0;

    if (a1 <= an) {
      long long n = (an - a1) / d + 1;
      long long a, b;
      if (n % 2 == 0) {
        a = (n / 2) % mod;
        b = (a1 + an) % mod;
      }
      else {
        a = n % mod;
        b = ((a1 + an) / 2) % mod;
      }
      res = (a * b) % mod;
    }
    return res;
  }

  long long res = 0;
  long long pm = (pl + pr) / 2;

  (res += calc(a1, d * 2, pl, pm, l, r, u, v)) %= mod;
  (res += calc(a1 + d, d * 2, pm + 1, pr, l, r, u, v)) %= mod;

  return res;
}

int main() {
  cin >> n >> m >> mod;

  for (int i = 0; i < m; ++i) {
    long long l, r, u, v;

    scanf("%I64d %I64d %I64d %I64d", &l, &r, &u, &v);

    long long sum = calc(1, 1, 1, n, l, r, u, v);

    printf("%I64d\n", sum);
  }
  return 0;
}


#include<stdio.h>
long long n;
int m,mod;
long long l,r,u,v;
inline long long min(long long a,long long b){return a<b?a:b;}
inline long long max(long long a,long long b){return a>b?a:b;}
inline long long odds(long long l,long long r){
    long long uu=u/2+1;
    long long vv=(v+1)/2;
    uu=max(uu,l);
    vv=min(vv,r);
    if(uu>vv)return 0;
    uu--;
    vv%=mod;uu%=mod;
    return (((vv*vv)%mod-(uu*uu)%mod)%mod+mod)%mod;
}
inline long long sum(long long n){
    n%=mod;
    return (n*(n+1ll))%mod;
}
inline long long evens(long long l,long long r){
    long long uu=(u+1)/2;
    long long vv=v/2;
    uu=max(uu,l);
    vv=min(vv,r);
    if(uu>vv)return 0;
    uu--;
    return ((sum(vv)-sum(uu))%mod+mod)%mod;
}
inline long long solve(){
    if(n==1){
	if(u<=1&&1<=v)return 1%mod;
	return 0;
    }
    long long oddc=(n+1)/2;
    long long ans=0;
    if(r<=oddc)ans=(ans+odds(l,r))%mod;
    else if(l<=oddc)ans=(ans+odds(l,oddc))%mod;
    if(l>oddc)ans=(ans+evens(l-oddc,r-oddc))%mod;
    else if(r>oddc)ans=(ans+evens(1,r-oddc))%mod;
    return ans;
}
int main(){
    scanf("%I64d%d%d",&n,&m,&mod);
    while(m--){
	scanf("%I64d%I64d%I64d%I64d",&l,&r,&u,&v);
	if(l>n||r>n||l>r||u>v)while(1)puts("!");
	printf("%d\n",(int)solve());
    }
}


#include<stdio.h>
long long n;
int m,mod;
long long u,v;
inline long long min(long long a,long long b){return a<b?a:b;}
inline long long max(long long a,long long b){return a>b?a:b;}
inline long long solve(long long q,long long w,long long l,long long r,long long beg,long long st){
    if(q==l&&w==r){
	//printf("%lld %lld %lld %lld %lld %lld v=%lld u=%lld\n",q,w,l,r,beg,st,v,u);
	long long n=(r-l+1);
	long long las=beg+(n-1)*st;
	if(u<beg||v>las)return 0;
	long long vv=(v-beg+st-1)/st;
	long long uu=(u-beg)/st;
	vv=max(vv,0);
	uu=min(uu,n-1);
	//printf("%lld %lld\n",vv,uu);
	if(vv>uu)return 0;
	long long f=beg+vv*st;
	long long g=beg+uu*st;
	long long cnt=(uu-vv+1);
	f%=2*mod;g%=2*mod;cnt%=2*mod;
	return (((f+g)%(2*mod)*cnt)/2)%mod;
    }
    long long oddc=(q+w)/2;
    long long ans=0;
    if(r<=oddc)ans=(ans+solve(q,oddc,l,r,beg,st*2))%mod;
    else if(l<=oddc)ans=(ans+solve(q,oddc,l,oddc,beg,st*2))%mod;
    if(l>oddc)ans=(ans+solve(oddc+1,w,l,r,beg+st,st*2))%mod;
    else if(r>oddc)ans=(ans+solve(oddc+1,w,oddc+1,r,beg+st,st*2))%mod;
    return ans;
}
int main(){
    scanf("%I64d%d%d",&n,&m,&mod);
    while(m--){
	long long l,r;
	scanf("%I64d%I64d%I64d%I64d",&l,&r,&v,&u);//v,u is swapped
	printf("%d\n",(int)solve(1,n,l,r,1,1));
    }
}


#include<stdio.h>
#include<algorithm>
using namespace std;
long long n;
int m,mod;
long long l,r,u,v;
inline long long odds(long long l,long long r){
    long long uu=u/2+1;
    long long vv=(v+1)/2;
    uu=max(uu,l);
    vv=min(vv,r);
    if(uu>vv)return 0;
    uu--;
    vv%=mod;uu%=mod;
    return (((vv*vv)%mod-(uu*uu)%mod)%mod+mod)%mod;
}
inline long long sum(long long n){
    n%=mod;
    return (n*(n+1ll))%mod;
}
inline long long evens(long long l,long long r){
    long long uu=(u+1)/2;
    long long vv=v/2;
    uu=max(uu,l);
    vv=min(vv,r);
    if(uu>vv)return 0;
    uu--;
    return ((sum(vv)-sum(uu))%mod+mod)%mod;
}
inline long long solve(){
    if(n==1){
	if(u<=1&&1<=v)return 1%mod;
	return 0;
    }
    long long oddc=(n+1)/2;
    long long ans=0;
    if(r<=oddc)ans=(ans+odds(l,r))%mod;
    else if(l<=oddc)ans=(ans+odds(l,oddc))%mod;
    if(l>oddc)ans=(ans+evens(l-oddc,r-oddc))%mod;
    else if(r>oddc)ans=(ans+evens(1,r-oddc))%mod;
    return ans;
}
int main(){
    scanf("%I64d%d%d",&n,&m,&mod);
    while(m--){
	scanf("%I64d%I64d%I64d%I64d",&l,&r,&u,&v);
	printf("%I64d\n",solve());
    }
}


#include<stdio.h>
#include<algorithm>
using namespace std;
long long n;
int m,mod;
long long l,r,u,v;
inline long long odds(long long l,long long r){
    long long uu=u/2+1;
    long long vv=(v+1)/2;
    uu=max(uu,l);
    vv=min(vv,r);
    if(uu>vv)return 0;
    vv%=mod;uu%=mod;
    return (((vv*vv)%mod-((uu-1)*(uu-1))%mod)%mod+mod)%mod;
}
inline long long sum(long long n){
    return (n*(n+1ll))%mod;
}
inline long long evens(long long l,long long r){
    long long uu=(u+1)/2;
    long long vv=v/2;
    uu=max(uu,l);
    vv=min(vv,r);
    if(uu>vv)return 0;
    uu--;
    vv%=mod;uu%=mod;
    return ((sum(vv)-sum(uu))%mod+mod)%mod;
}
inline long long solve(){
    if(n==1){
	if(u<=1&&1<=v)return 1%mod;
	return 0;
    }
    long long oddc=(n+1)/2;
    long long ans=0;
    if(r<=oddc)ans=(ans+odds(l,r))%mod;
    else if(l<=oddc)ans=(ans+odds(l,oddc))%mod;
    if(l>oddc)ans=(ans+evens(l-oddc,r-oddc))%mod;
    else if(r>oddc)ans=(ans+evens(1,r-oddc))%mod;
    return ans;
}
int main(){
    scanf("%I64d%d%d",&n,&m,&mod);
    while(m--){
	scanf("%I64d%I64d%I64d%I64d",&l,&r,&u,&v);
	printf("%I64d\n",solve());
    }
}


// Codeforces Beta Round #88
// Problem D -- Not Quick Transformation
#include <cassert>
#include <cstdio>
#include <cstring>
#include <utility>
#include <iostream>
using namespace std;

typedef long long LL;

LL n, m, mod;

pair <LL, LL> solve(LL n, LL l, LL r, LL u, LL v) {
    if (u > v || v < l || u > r) {
        return make_pair(0, 0);
    }
    u = max(u, 1LL);
    v = min(v, n);
    if (l == 1 && r == n) {
        return make_pair((u + v) * (v - u + 1) / 2 % mod, v - u + 1);
    }
    assert(n > 1);
    LL m = (n + 1) / 2;
    pair <LL, LL> result;
    if (l <= m) {
        pair <LL, LL> ret = solve(m, l, min(m, r), (u + 2) / 2, (v + 1) / 2);
        result.first += ret.first * 2 - ret.second;
        result.second += ret.second;
    }
    if (m < r) {
        pair <LL, LL> ret = solve(n / 2, max(l - m, 1LL), r - m, (u + 1) / 2, v / 2);
        result.first += ret.first * 2;
        result.second += ret.second;
    }
    result.first %= mod;
    result.first += mod;
    result.first %= mod;
    return result;
}

int main() {
    ios::sync_with_stdio(false);
    cin >> n >> m >> mod;
    while (m --) {
        LL l, r, u, v;
        cin >> l >> r >> u >> v;
        cout << solve(n, l, r, u, v).first << endl;
    }
    return 0;
}


// Codeforces Beta Round #88
// Problem D -- Not Quick Transformation
#include <cassert>
#include <cstdio>
#include <cstring>
#include <utility>
#include <iostream>
using namespace std;

typedef long long LL;

LL n, m, mod;

pair <LL, LL> solve(LL n, LL l, LL r, LL u, LL v) {
    if (u > v || v < l || u > r) {
        return make_pair(0, 0);
    }
    if (l == 1 && r == n) {
        u = max(u, 1LL);
        v = min(v, n);
        return make_pair((u + v) * (v - u + 1) / 2 % mod, v - u + 1);
    }
    assert(n > 1);
    LL m = (n + 1) / 2;
    pair <LL, LL> result;
    if (l <= m) {
        pair <LL, LL> ret = solve(m, l, min(m, r), (u + 2) / 2, (v + 1) / 2);
        result.first += ret.first * 2 - ret.second;
        result.second += ret.second;
    }
    if (m < r) {
        pair <LL, LL> ret = solve(n / 2, max(l - m, 1LL), r - m, (u + 1) / 2, v / 2);
        result.first += ret.first * 2;
        result.second += ret.second;
    }
    result.first %= mod;
    result.first += mod;
    result.first %= mod;
    return result;
}

int main() {
    ios::sync_with_stdio(false);
    cin >> n >> m >> mod;
    while (m --) {
        LL l, r, u, v;
        cin >> l >> r >> u >> v;
        cout << solve(n, l, r, u, v).first << endl;
    }
    return 0;
}


// Codeforces Beta Round #88
// Problem D -- Not Quick Transformation
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;

typedef long long Long;

Long mod;

struct Pair {
    Long length, sum;

    Pair(Long length, Long sum) : length(length), sum(sum) {}

    Pair oddMap() {
        return Pair(length, ((sum * 2 - length) % mod + mod) % mod );
    }

    Pair evenMap() {
        return Pair(length, sum * 2 % mod);
    }
};

Pair operator + (const Pair &a, const Pair &b) {
    return Pair(a.length + b.length, (a.sum + b.sum) % mod);
}

Pair solve(Long n, Long l, Long r, Long u, Long v) {
    if (r < 1 || n < l || v < 1 || n < u || v < u) {
        return Pair(0, 0);
    }
    if (l <= 1 && n <= r) {
        Long a = max(u, 1LL);
        Long b = min(v, n);
        Long length = b - a + 1;
        return Pair(length, ((length % (mod * 2)) * ((a + b) % (mod * 2)) / 2) % mod);
    }
    int odd = n + 1 >> 1;
    return solve(odd, l, r, u + 2 >> 1, v + 1 >> 1).oddMap() + solve(n - odd, l - odd, r - odd, u + 1 >> 1, v >> 1).evenMap();
}

int main() {
    Long n, m;
    cin >> n >> m >> mod;
    for (int i = 0; i < m; ++ i) {
        Long l, r, u, v;
        cin >> l >> r >> u >> v;
        cout << solve(n, l, r, u, v).sum << endl;
    }
    return 0;
}


// Codeforces Beta Round #88
// Problem D -- Not Quick Transformation
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;

typedef long long Long;

Long mod;

struct Pair {
    Long length, sum;

    Pair(Long length, Long sum) : length(length), sum(sum) {}

    Pair oddMap() {
        return Pair(length, ((sum * 2 - length) % mod + mod) % mod );
    }

    Pair evenMap() {
        return Pair(length, sum * 2 % mod);
    }
};

Pair operator + (const Pair &a, const Pair &b) {
    return Pair(a.length + b.length, (a.sum + b.sum) % mod);
}

Pair solve(Long n, Long l, Long r, Long u, Long v) {
    if (r < 1 || n < l || v < 1 || n < u || v < u) {
        return Pair(0, 0);
    }
    if (l <= 1 && n <= r) {
        Long a = max(u, 1LL);
        Long b = min(v, n);
        Long length = b - a + 1;
        return Pair(length, ((length % (mod * 2)) * ((a + b) % (mod * 2)) / 2) % mod);
    }
    Long odd = n + 1 >> 1;
    return solve(odd, l, r, u + 2 >> 1, v + 1 >> 1).oddMap() + solve(n - odd, l - odd, r - odd, u + 1 >> 1, v >> 1).evenMap();
}

int main() {
    Long n, m;
    cin >> n >> m >> mod;
    for (int i = 0; i < m; ++ i) {
        Long l, r, u, v;
        cin >> l >> r >> u >> v;
        cout << solve(n, l, r, u, v).sum << endl;
    }
    return 0;
}


// Codeforces Beta Round #88
// Problem D -- Not Quick Transformation
#include <cassert>
#include <cstdio>
#include <cstring>
#include <utility>
#include <iostream>
using namespace std;

typedef long long LL;

LL n, m, mod;

LL sum(LL a, LL b) {
    LL u = a + b;
    LL v = b - a + 1;
    if (u & 1) {
        v >>= 1;
    } else {
        u >>= 1;
    }
    return u % mod * v % mod;
}

pair <LL, LL> solve(LL n, LL l, LL r, LL u, LL v) {
    if (u > v || v < l || u > r) {
        return make_pair(0, 0);
    }
    u = max(u, 1LL);
    v = min(v, n);
    if (l == 1 && r == n) {
        return make_pair(sum(u, v), v - u + 1);
    }
    assert(n > 1);
    LL m = (n + 1) / 2;
    pair <LL, LL> result;
    if (l <= m) {
        pair <LL, LL> ret = solve(m, l, min(m, r), (u + 2) / 2, (v + 1) / 2);
        result.first += ret.first * 2 - ret.second;
        result.second += ret.second;
    }
    if (m < r) {
        pair <LL, LL> ret = solve(n / 2, max(l - m, 1LL), r - m, (u + 1) / 2, v / 2);
        result.first += ret.first * 2;
        result.second += ret.second;
    }
    result.first %= mod;
    result.first += mod;
    result.first %= mod;
    return result;
}

int main() {
    ios::sync_with_stdio(false);
    cin >> n >> m >> mod;
    while (m --) {
        LL l, r, u, v;
        cin >> l >> r >> u >> v;
        cout << solve(n, l, r, u, v).first << endl;
    }
    return 0;
}


// Codeforces Beta Round #88
// Problem D -- Not Quick Transformation
#include <cassert>
#include <cstdio>
#include <cstring>
#include <utility>
#include <iostream>
using namespace std;

typedef long long LL;

LL n, m, mod;

LL sum(LL a, LL b) {
    LL u = a + b;
    LL v = b - a + 1;
    if (u & 1) {
        u >>= 1;
    } else {
        v >>= 1;
    }
    return u % mod * v % mod;
}

pair <LL, LL> solve(LL n, LL l, LL r, LL u, LL v) {
    if (u > v || v < l || u > r) {
        return make_pair(0, 0);
    }
    u = max(u, 1LL);
    v = min(v, n);
    if (l == 1 && r == n) {
        return make_pair(sum(u, v), v - u + 1);
    }
    assert(n > 1);
    LL m = (n + 1) / 2;
    pair <LL, LL> result;
    if (l <= m) {
        pair <LL, LL> ret = solve(m, l, min(m, r), (u + 2) / 2, (v + 1) / 2);
        result.first += ret.first * 2 - ret.second;
        result.second += ret.second;
    }
    if (m < r) {
        pair <LL, LL> ret = solve(n / 2, max(l - m, 1LL), r - m, (u + 1) / 2, v / 2);
        result.first += ret.first * 2;
        result.second += ret.second;
    }
    result.first %= mod;
    result.first += mod;
    result.first %= mod;
    return result;
}

int main() {
    ios::sync_with_stdio(false);
    cin >> n >> m >> mod;
    while (m --) {
        LL l, r, u, v;
        cin >> l >> r >> u >> v;
        cout << solve(n, l, r, u, v).first << endl;
    }
    return 0;
}


#include <stdio.h>
#include <algorithm>
using namespace std;
long long u, v, mod;
long long pd(long long n, long long k, long long b, long long l, long long r){
    long long x, y, p, q, s, mid;
    if (l == 1 && r == n){
        x = (u + b) / k;
        if (x * k - b < u) x++;
        y = (v + b) / k;
        if (y * k - b > v) y--;
        if (x > y || y < 0 || x > n) return 0;
        x = max(1LL, x);
        y = min(n, y);
        p = x + y;
        q = y - x + 1;
        s = mod - b;
        s = ((s % mod) * (q % mod)) % mod;
        if (p % 2 == 0){
            p /= 2;
        }
        else{
            q /= 2;
        }
        p %= mod;
        q %= mod;
        return ((((p * q) % mod * k) % mod) + s) % mod;
    }
    mid = (n + 1) / 2;
    if (r <= mid){
        return pd(mid, 2 * k, k + b, l, r);
    }
    if (l > mid){
        return pd(n - mid, 2 * k, b, l - mid, r - mid);
    }
    return (pd(mid, 2 * k, k + b, l, mid) +
            pd(n - mid, 2 * k, b, 1, r - mid)) % mod;
}
int main(){
    long long n, m, l, r;
    scanf("%I64d%I64d%I64d", &n, &m, &mod);
    while (m--){
        scanf("%I64d%I64d%I64d%I64d", &l, &r, &u, &v);
        printf("%I64d\n", pd(n, 1, 0, l, r));
    }
    return 0;
}


#include <stdio.h>
#include <algorithm>
using namespace std;
long long u, v, mod;
long long pd(long long n, long long k, long long b, long long l, long long r){
    long long x, y, p, q, s, mid;
    if (l == 1 && r == n){
        x = (u + b) / k;
        if (x * k - b < u) x++;
        y = (v + b) / k;
        if (y * k - b > v) y--;
        if (x > y || y < 0 || x > n) return 0;
        x = max(1LL, x);
        y = min(n, y);
        p = x + y;
        q = y - x + 1;
        s = mod - (b % mod);
        s = (s * (q % mod)) % mod;
        if (p % 2 == 0){
            p /= 2;
        }
        else{
            q /= 2;
        }
        p %= mod;
        q %= mod;
        return (((((p * q) % mod) * (k % mod)) % mod) + s) % mod;
    }
    mid = (n + 1) / 2;
    if (r <= mid){
        return pd(mid, 2 * k, k + b, l, r);
    }
    if (l > mid){
        return pd(n - mid, 2 * k, b, l - mid, r - mid);
    }
    return (pd(mid, 2 * k, k + b, l, mid) +
            pd(n - mid, 2 * k, b, 1, r - mid)) % mod;
}
int main(){
    long long n, m, l, r;
    scanf("%I64d%I64d%I64d", &n, &m, &mod);
    while (m--){
        scanf("%I64d%I64d%I64d%I64d", &l, &r, &u, &v);
        printf("%I64d\n", pd(n, 1, 0, l, r));
    }
    return 0;
}


#include <stdio.h>
#include <algorithm>
using namespace std;
long long u, v, mod;
long long pd(long long n, long long k, long long b, long long l, long long r){
    long long x, y, p, q, s, mid;
    if (l == 1 && r == n){
        x = (u + b) / k;
        if (x * k - b < u) x++;
        y = (v + b) / k;
        if (y * k - b > v) y--;
        if (x > y || y < 0 || x > n) return 0;
        x = max(1LL, x);
        y = min(n, y);
        p = x + y;
        q = y - x + 1;
        s = mod - b;
        s = ((s % mod) * (q % mod)) % mod;
        if (p % 2 == 0){
            p /= 2;
        }
        else{
            q /= 2;
        }
        p %= mod;
        q %= mod;
        return ((((p * q) % mod * k) % mod) + s) % mod;
    }
    mid = (n + 1) / 2;
    if (r <= mid){
        return pd(mid, (2 * k) % mod, (k + b) % mod, l, r);
    }
    if (l > mid){
        return pd(n - mid, (2 * k) % mod, b, l - mid, r - mid);
    }
    return (pd(mid, (2 * k) % mod, (k + b) % mod, l, mid) +
            pd(n - mid, (2 * k) % mod, b, 1, r - mid)) % mod;
}
int main(){
    long long n, m, l, r;
    scanf("%I64d%I64d%I64d", &n, &m, &mod);
    while (m--){
        scanf("%I64d%I64d%I64d%I64d", &l, &r, &u, &v);
        printf("%I64d\n", pd(n, 1, 0, l, r));
    }
    return 0;
}


#include <stdio.h>
#include <algorithm>
using namespace std;
long long u, v, mod;
long long pd(long long n, long long k, long long b, long long l, long long r){
    long long x, y, p, q, s, mid;
    if (l == 1 && r == n){
        x = (u + b) / k;
        if (x * k - b < u) x++;
        y = (v + b) / k;
        if (y * k - b > v) y--;
        if (x > y || y < 0 || x > n) return 0;
        x = max(1LL, x);
        y = min(n, y);
        p = x + y;
        q = y - x + 1;
        s = mod - b;
        s = ((s % mod) * (q % mod)) % mod;
        if (p % 2 == 0){
            p /= 2;
        }
        else{
            q /= 2;
        }
        p %= mod;
        q %= mod;
        return ((((p * q) % mod * k) % mod) + s) % mod;
    }
    mid = (n + 1) / 2;
    if (r <= mid){
        return pd(mid, (2 * k) % mod, (k + b) % mod, l, r);
    }
    if (l > mid){
        return pd(n - mid, (2 * k) % mod, b, l - mid, r - mid);
    }
    return pd(mid, (2 * k) % mod, (k + b) % mod, l, mid) +
            pd(n - mid, (2 * k) % mod, b, 1, r - mid);
}
int main(){
    long long n, m, l, r;
    scanf("%I64d%I64d%I64d", &n, &m, &mod);
    while (m--){
        scanf("%I64d%I64d%I64d%I64d", &l, &r, &u, &v);
        printf("%I64d\n", pd(n, 1, 0, l, r));
    }
    return 0;
}


#include <stdio.h>
#include <algorithm>
using namespace std;
long long u, v, mod;
long long pd(long long n, long long k, long long b, long long l, long long r){
    long long x, y, p, q, s, mid;
    if (l == 1 && r == n){
        x = (u + b) / k;
        if (x * k - b < u) x++;
        y = (v + b) / k;
        if (y * k - b > v) y--;
        if (x > y || y < 0 || x > n) return 0;
        x = max(1LL, x);
        y = min(n, y);
        p = x + y;
        q = y - x + 1;
        s = ((s % mod) * (q & mod)) % mod;
        if (p % 2 == 0){
            p /= 2;
        }
        else{
            q /= 2;
        }
        p %= mod;
        q %= mod;
        s = mod - b;
        return ((((p * q) % mod * k) % mod) + s) % mod;
    }
    mid = (n + 1) / 2;
    if (r <= mid){
        return pd(mid, (2 * k) % mod, (k + b) % mod, l, r);
    }
    if (l > mid){
        return pd(n - mid, (2 * k) % mod, b, l - mid, r - mid);
    }
    return pd(mid, (2 * k) % mod, (k + b) % mod, l, mid) +
            pd(n - mid, (2 * k) % mod, b, 1, r - mid);
}
int main(){
    long long n, m, l, r;
    scanf("%I64d%I64d%I64d", &n, &m, &mod);
    while (m--){
        scanf("%I64d%I64d%I64d%I64d", &l, &r, &u, &v);
        printf("%I64d\n", pd(n, 1, 0, l, r));
    }
    return 0;
}


#include<iostream>
#include<sstream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#define rep(i,n) for(int i=0;i<n;i++)
#define fr(i,c) for(__typeof (c.begin()) i=c.begin(); i!=c.end(); i++)
#define all(c) (c).begin(), (c).end()
#define pb push_back
using namespace std;

typedef vector<int> vi;
typedef long long ll;

int mod;
ll u,v;
int query(ll l,ll r,ll a,ll b,ll a0,ll d){
  //cerr<<l<<" "<<r<<" "<<a<<" "<<b<<" "<<a0<<" "<<d<<endl;
  if(r<a||b<l)return 0;
  if(l<=a&&b<=r){
    ll s=a0,t=a0+d*(r-l);
    if(s<u)s+=((u-s)/d+((u-s)%d!=0))*d;
    if(t>v)t-=((t-v)/d+((t-v)%d!=0))*d;

    //cerr<<"s: "<<s<<" t: "<<t<<endl;
    if(s>t)return 0;
    ll n=(t-s)/d+1;
    s+=t; if(s%2==0)s/=2; else n/=2;
    return n%mod*(s%mod)%mod;
  }
  return query(l,r,a,(a+b)/2,a0,d*2)+query(l,r,(a+b)/2+1,b,a0+d,d*2);
}
void run(){
  ll n,l,r; int m;
  cin>>n>>m>>mod;
  //scanf("%I64d%d%d",&n,&m,&mod);
  rep(i,m){
    cin>>l>>r>>u>>v;
    //scanf("%I64d%I64d%I64d%I64d",&l,&r,&u,&v);
    cout<<query(l,r,1,n,1,1)<<endl;
    //printf("%I64d\n",query(l,r,1,n,1,1));
  }
}
int main(){
#ifndef ONLINE_JUDGE
  rep(i,2)
#endif
    run();
  return 0;
}


#include<iostream>
#include<sstream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#define rep(i,n) for(int i=0;i<n;i++)
#define fr(i,c) for(__typeof (c.begin()) i=c.begin(); i!=c.end(); i++)
#define all(c) (c).begin(), (c).end()
#define pb push_back
using namespace std;

typedef vector<int> vi;
typedef long long ll;

int mod;
ll u,v;
int query(ll l,ll r,ll a,ll b,ll a0,ll d){
  //cerr<<l<<" "<<r<<" "<<a<<" "<<b<<" "<<a0<<" "<<d<<endl;
  if(r<a||b<l)return 0;
  if(l<=a&&b<=r){
    ll s=a0,t=a0+d*(r-l);
    if(s<u)s+=((u-s)/d+((u-s)%d!=0))*d;
    if(t>v)t-=((t-v)/d+((t-v)%d!=0))*d;

    //cerr<<"s: "<<s<<" t: "<<t<<endl;
    if(s>t)return 0;
    ll n=(t-s)/d+1;
    s+=t; if(s%2==0)s/=2; else n/=2;
    return n%mod*(s%mod)%mod;
  }
  return query(l,r,a,(a+b)/2,a0,d*2)+query(l,r,(a+b)/2+1,b,a0+d,d*2);
}
void run(){
  ll n,l,r; int m;
  //cin>>n>>m>>mod;
  scanf("%I64d%d%d",&n,&m,&mod);
  rep(i,m){
    //cin>>l>>r>>u>>v;
    scanf("%I64d%I64d%I64d%I64d",&l,&r,&u,&v);
    //cout<<query(l,r,1,n,1,1)<<endl;
    printf("%I64d\n",query(l,r,1,n,1,1));
  }
}
int main(){
#ifndef ONLINE_JUDGE
  rep(i,2)
#endif
    run();
  return 0;
}


#include<iostream>
#include<sstream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#define rep(i,n) for(int i=0;i<n;i++)
#define fr(i,c) for(__typeof (c.begin()) i=c.begin(); i!=c.end(); i++)
#define all(c) (c).begin(), (c).end()
#define pb push_back
using namespace std;

typedef vector<int> vi;
typedef long long ll;

int mod;
ll u,v;
int query(ll l,ll r,ll a,ll b,ll a0,ll d){
  //cerr<<l<<" "<<r<<" "<<a<<" "<<b<<" "<<a0<<" "<<d<<endl;
  if(r<a||b<l)return 0;
  if(l<=a&&b<=r){
    ll s=a0,t=a0+d*(b-a);
    if(s<u)s+=((u-s)/d+((u-s)%d!=0))*d;
    if(t>v)t-=((t-v)/d+((t-v)%d!=0))*d;

    //cerr<<"s: "<<s<<" t: "<<t<<endl;
    if(s>t)return 0;
    ll n=(t-s)/d+1;
    s+=t; if(s%2==0)s/=2; else n/=2;
    return n%mod*(s%mod)%mod;
  }
  return (query(l,r,a,(a+b)/2,a0,d*2)+query(l,r,(a+b)/2+1,b,a0+d,d*2))%mod;
}
void run(){
  ll n,l,r; int m;
  cin>>n>>m>>mod;
  rep(i,m){
    cin>>l>>r>>u>>v;
    cout<<query(l,r,1,n,1,1)<<endl;
  }
}
int main(){
#ifndef ONLINE_JUDGE
  //rep(i,3)
#endif
    run();
  return 0;
}


#include<iostream>
#include<sstream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#define rep(i,n) for(int i=0;i<n;i++)
#define fr(i,c) for(__typeof (c.begin()) i=c.begin(); i!=c.end(); i++)
#define all(c) (c).begin(), (c).end()
#define pb push_back
using namespace std;

typedef vector<int> vi;
typedef unsigned long long ll;

int mod;
ll u,v;
int query(ll l,ll r,ll a,ll b,ll a0,ll d){
  //cerr<<l<<" "<<r<<" "<<a<<" "<<b<<" "<<a0<<" "<<d<<endl;
  if(r<a||b<l)return 0;
  if(l<=a&&b<=r){
    ll s=a0,t=a0+d*(b-a);
    if(s<u)s+=((u-s)/d+((u-s)%d!=0))*d;
    if(t>v)t-=((t-v)/d+((t-v)%d!=0))*d;

    //cerr<<"s: "<<s<<" t: "<<t<<endl;
    if(s>t)return 0;
    ll n=(t-s)/d+1;
    s+=t; if(s%2==0)s/=2; else n/=2;
    return n%mod*(s%mod)%mod;
  }
  return query(l,r,a,(a+b)/2,a0,d*2)+query(l,r,(a+b)/2+1,b,a0+d,d*2);
}
void run(){
  ll n,l,r; int m;
  cin>>n>>m>>mod;
  rep(i,m){
    cin>>l>>r>>u>>v;
    cout<<query(l,r,1,n,1,1)<<endl;
  }
}
int main(){
#ifndef ONLINE_JUDGE
  rep(i,3)
#endif
    run();
  return 0;
}


#include<iostream>
#include<sstream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#define rep(i,n) for(int i=0;i<n;i++)
#define fr(i,c) for(__typeof (c.begin()) i=c.begin(); i!=c.end(); i++)
#define all(c) (c).begin(), (c).end()
#define pb push_back
using namespace std;

typedef vector<int> vi;
typedef long long ll;

int mod;
ll u,v;
int query(ll l,ll r,ll a,ll b,ll a0,ll d){
  //cerr<<l<<" "<<r<<" "<<a<<" "<<b<<" "<<a0<<" "<<d<<endl;
  if(r<a||b<l)return 0;
  if(l<=a&&b<=r){
    ll s=a0,t=a0+d*(b-a);
    if(s<u)s+=((u-s)/d+((u-s)%d!=0))*d;
    if(t>v)t-=((t-v)/d+((t-v)%d!=0))*d;

    //cerr<<"s: "<<s<<" t: "<<t<<endl;
    if(s>t)return 0;
    ll n=(t-s)/d+1;
    s+=t; if(s%2==0)s/=2; else n/=2;
    return n%mod*(s%mod)%mod;
  }
  return query(l,r,a,(a+b)/2,a0,d*2)+query(l,r,(a+b)/2+1,b,a0+d,d*2);
}
void run(){
  ll n,l,r; int m;
  cin>>n>>m>>mod;
  rep(i,m){
    cin>>l>>r>>u>>v;
    cout<<query(l,r,1,n,1,1)<<endl;
  }
}
int main(){
#ifndef ONLINE_JUDGE
  rep(i,3)
#endif
    run();
  return 0;
}


#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <cstring>
//#include <conio.h>

using namespace std;

#define oo 1000000000
#define fi first
#define se second
#define sqr(a) ((a)*(a))
#define FR(i,n) for (int i = 0; i < (n); i++)
#define DN(i,a) for(int i = (a)-1; i >= 0; i--)
#define FOR(i,a,b) for (int i = (a); i <= (b); i++)
#define DOWN(i,a,b) for(int i = (a); i >= (b); i--)
#define FORV(i,a) for(typeof(a.begin()) i = a.begin(); i != a.end(); i++)

typedef pair<int, int> PII;
typedef vector<int> VI;
typedef long long ll;

long long m, n, base, l, r, u, v;

long long tinh(ll lt, ll sd, ll u) {
    if (u < sd) return 0;
    ll st = sd;
    ll x = (u - sd) / lt;
    ll fn = x * lt + sd;
    //cout << lt << ' ' << sd << ' ' << u << ' ' << st << ' ' << fn << ' ' << x <<  ' ' << (st + fn) * (x + 1) / 2 << endl;
    ll y = st + fn;
    //cout << st << ' ' << fn << ' ' << x << endl;
    x = x + 1;
    if (y % 2 == 0) y /= 2; else x /= 2;
    x %= base;
    y %= base;
    return x * y % base;
}

long long calc(ll lt, ll sd, ll u, ll v) {
    if (u > m) return 0;
    v = min(v, m);
    //ll x = tinh(lt, sd, v) - tinh(lt, sd, u - 1);
    //cout << lt << ' ' << sd << ' ' << u << ' ' << v << ' ' << x << endl;
    return tinh(lt, sd, v) - tinh(lt, sd, u - 1);
}

long long get(ll lt, ll sd, ll l, ll r, ll i, ll j) {
    //cout << lt << ' ' << sd << ' ' << l << ' ' << r << endl;
    if (l > j || r < i) return 0;
    if (l >= i && r <= j) {
        //cout << "OK " << lt << ' ' << sd << ' ' << l << ' ' << r << endl;
        return calc(lt, sd, u, v);
    }
    int m = (l + r) / 2;
    if (sd % 2)
        return (get(lt * 2, sd, l, m, i, j) + get(lt * 2, sd + lt, m + 1, r, i, j)) % base;
        else return (get(lt * 2, sd + lt, l, m, i, j) + get(lt * 2, sd, m + 1, r, i, j)) % base;
}

int main () {
    //freopen("D.in", "r", stdin);
    //freopen(".out", "w", stdout);
    cin >> m >> n >> base;
    long long k = (1 + m) / 2;
    FR(i, n) {
        l, r, u, v;
        cin >> l >> r >> u >> v;
        cout << get(1, 0, 1, m, l, r) << endl;
    }
    return 0;
}


#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <cstring>
//#include <conio.h>

using namespace std;

#define oo 1000000000
#define fi first
#define se second
#define sqr(a) ((a)*(a))
#define FR(i,n) for (int i = 0; i < (n); i++)
#define DN(i,a) for(int i = (a)-1; i >= 0; i--)
#define FOR(i,a,b) for (int i = (a); i <= (b); i++)
#define DOWN(i,a,b) for(int i = (a); i >= (b); i--)
#define FORV(i,a) for(typeof(a.begin()) i = a.begin(); i != a.end(); i++)

typedef pair<int, int> PII;
typedef vector<int> VI;
typedef long long ll;

long long m, n, base, l, r, u, v;

long long tinh(ll lt, ll sd, ll u) {
    if (u < sd) return 0;
    ll st = sd;
    ll x = (u - sd) / lt;
    ll fn = x * lt + sd;
    //cout << lt << ' ' << sd << ' ' << u << ' ' << st << ' ' << fn << ' ' << x <<  ' ' << (st + fn) * (x + 1) / 2 << endl;
    ll y = st + fn;
    //cout << st << ' ' << fn << ' ' << x << endl;
    x = x + 1;
    if (y % 2 == 0) y /= 2; else x /= 2;
    x %= base;
    y %= base;
    return x * y % base;
}

long long calc(ll lt, ll sd, ll u, ll v) {
    if (u > m) return 0;
    v = min(v, m);
    //ll x = tinh(lt, sd, v) - tinh(lt, sd, u - 1);
    //cout << lt << ' ' << sd << ' ' << u << ' ' << v << ' ' << x << endl;
    return (tinh(lt, sd, v) - tinh(lt, sd, u - 1) + base) % base;
}

long long get(ll lt, ll sd, ll l, ll r, ll i, ll j) {
    //cout << lt << ' ' << sd << ' ' << l << ' ' << r << endl;
    if (l > j || r < i) return 0;
    if (l >= i && r <= j) {
   //     cout << "OK " << lt << ' ' << sd << ' ' << l << ' ' << r << endl;
        return calc(lt, sd, u, v);
    }
    int m = (l + r) / 2;
    if (sd != 0)
        return (get(lt * 2, sd, l, m, i, j) + get(lt * 2, sd + lt, m + 1, r, i, j)) % base;
        else return (get(lt * 2, sd + lt, l, m, i, j) + get(lt * 2, sd, m + 1, r, i, j)) % base;
}

int main () {
    //freopen("D.in", "r", stdin);
    //freopen(".out", "w", stdout);
    cin >> m >> n >> base;
    long long k = (1 + m) / 2;
    FR(i, n) {
        l, r, u, v;
        cin >> l >> r >> u >> v;
        cout << get(1, 0, 1, m, l, r) << endl;
    }
    return 0;
}


#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <cstring>
#include <conio.h>

using namespace std;

#define oo 1000000000
#define fi first
#define se second
#define sqr(a) ((a)*(a))
#define FR(i,n) for (int i = 0; i < (n); i++)
#define DN(i,a) for(int i = (a)-1; i >= 0; i--)
#define FOR(i,a,b) for (int i = (a); i <= (b); i++)
#define DOWN(i,a,b) for(int i = (a); i >= (b); i--)
#define FORV(i,a) for(typeof(a.begin()) i = a.begin(); i != a.end(); i++)

typedef pair<int, int> PII;
typedef vector<int> VI;
typedef long long ll;

long long m, n, base, le, ri, u, v;

long long tinh(ll lt, ll sd, ll u) {
    if (u < sd) return 0;
    ll st = sd;
    ll x = (u - sd) / lt;
    ll fn = x * lt + sd;
    //cout << lt << ' ' << sd << ' ' << u << ' ' << st << ' ' << fn << ' ' << x <<  ' ' << (st + fn) * (x + 1) / 2 << endl;
    ll y = st + fn;
    //cout << st << ' ' << fn << ' ' << x << endl;
    x = x + 1;
    if (y % 2 == 0) y /= 2; else x /= 2;
    x %= base;
    y %= base;
    return x * y % base;
}

long long calc(ll lt, ll sd) {
    if (u > m) return 0;
    v = min(v, m);
    //ll x = tinh(lt, sd, v) - tinh(lt, sd, u - 1);
    //cout << lt << ' ' << sd << ' ' << u << ' ' << v << ' ' << x << endl;
    return (tinh(lt, sd, v) - tinh(lt, sd, u - 1) + base) % base;
}

long long get(ll lt, ll sd, ll l, ll r) {
    //cout << lt << ' ' << sd << ' ' << l << ' ' << r << endl;
    //getch();
    //cout << lt << ' ' << sd << ' ' << l << ' ' << r << endl;
    if (l > ri || r < le) return 0;
    if (l >= le && r <= ri) {
   //     cout << "OK " << lt << ' ' << sd << ' ' << l << ' ' << r << endl;
        return calc(lt, sd);
    }
    long long m = (l + r) / 2;
    if (sd != 0)
        return (get(lt * 2, sd, l, m) + get(lt * 2, sd + lt, m + 1, r)) % base;
        else return (get(lt * 2, sd + lt, l, m) + get(lt * 2, sd, m + 1, r)) % base;
}

int main () {
    //freopen("D.in", "r", stdin);
    //freopen(".out", "w", stdout);
    cin >> m >> n >> base;
    long long k = (1 + m) / 2;
    FR(i, n) {
        cin >> le >> ri >> u >> v;
        cout << get(1, 0, 1, m) << endl;
    }
    return 0;
}


#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <cstring>
//#include <conio.h>

using namespace std;

#define oo 1000000000
#define fi first
#define se second
#define sqr(a) ((a)*(a))
#define FR(i,n) for (int i = 0; i < (n); i++)
#define DN(i,a) for(int i = (a)-1; i >= 0; i--)
#define FOR(i,a,b) for (int i = (a); i <= (b); i++)
#define DOWN(i,a,b) for(int i = (a); i >= (b); i--)
#define FORV(i,a) for(typeof(a.begin()) i = a.begin(); i != a.end(); i++)

typedef pair<int, int> PII;
typedef vector<int> VI;
typedef long long ll;

long long m, n, base, l, r, u, v;

long long tinh(ll lt, ll sd, ll u) {
    if (u < sd) return 0;
    ll st = sd;
    ll x = (u - sd) / lt;
    ll fn = x * lt + sd;
    //cout << lt << ' ' << sd << ' ' << u << ' ' << st << ' ' << fn << ' ' << x <<  ' ' << (st + fn) * (x + 1) / 2 << endl;
    ll y = st + fn;
    //cout << st << ' ' << fn << ' ' << x << endl;
    x = x + 1;
    if (y % 2 == 0) y /= 2; else x /= 2;
    x %= base;
    y %= base;
    return x * y % base;
}

long long calc(ll lt, ll sd, ll u, ll v) {
    if (u > m) return 0;
    v = min(v, m);
    //ll x = tinh(lt, sd, v) - tinh(lt, sd, u - 1);
    //cout << lt << ' ' << sd << ' ' << u << ' ' << v << ' ' << x << endl;
    return tinh(lt, sd, v) - tinh(lt, sd, u - 1);
}

long long get(ll lt, ll sd, ll l, ll r, ll i, ll j) {
    //cout << lt << ' ' << sd << ' ' << l << ' ' << r << endl;
    if (l > j || r < i) return 0;
    if (l >= i && r <= j) {
   //     cout << "OK " << lt << ' ' << sd << ' ' << l << ' ' << r << endl;
        return calc(lt, sd, u, v);
    }
    int m = (l + r) / 2;
    if (sd != 0)
        return (get(lt * 2, sd, l, m, i, j) + get(lt * 2, sd + lt, m + 1, r, i, j)) % base;
        else return (get(lt * 2, sd + lt, l, m, i, j) + get(lt * 2, sd, m + 1, r, i, j)) % base;
}

int main () {
    //freopen("D.in", "r", stdin);
    //freopen(".out", "w", stdout);
    cin >> m >> n >> base;
    long long k = (1 + m) / 2;
    FR(i, n) {
        l, r, u, v;
        cin >> l >> r >> u >> v;
        cout << get(1, 0, 1, m, l, r) << endl;
    }
    return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <climits>
//#include <ext/hash_map>

using namespace std;
using namespace __gnu_cxx;

#define REP(i,n) for(int i = 0; i < int(n); ++i)
#define REPV(i, n) for (int i = (n) - 1; (int)i >= 0; --i)
#define FOR(i, a, b) for(int i = (int)(a); i < (int)(b); ++i)

#define FE(i,t) for (__typeof((t).begin())i=(t).begin();i!=(t).end();++i)
#define FEV(i,t) for (__typeof((t).rbegin())i=(t).rbegin();i!=(t).rend();++i)

#define two(x) (1LL << (x))
#define ALL(a) (a).begin(), (a).end()

#define pb push_back
#define ST first
#define ND second
#define MP(x,y) make_pair(x, y)

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<string> vs;

template<class T> void checkmin(T &a, T b){if (b<a)a=b;}
template<class T> void checkmax(T &a, T b){if (b>a)a=b;}
template<class T> void out(T t[], int n){REP(i, n)cout<<t[i]<<" "; cout<<endl;}
template<class T> void out(vector<T> t, int n=-1){for (int i=0; i<(n==-1?t.size():n); ++i) cout<<t[i]<<" "; cout<<endl;}
inline int count_bit(int n){return (n==0)?0:1+count_bit(n&(n-1));}
inline int low_bit(int n){return (n^n-1)&n;}
inline int ctz(int n){return (n==0?-1:ctz(n>>1)+1);}
int toInt(string s){int a; istringstream(s)>>a; return a;}
string toStr(int a){ostringstream os; os<<a; return os.str();}

ll n;
int mod;
ll u, v, l, r;

int go(ll s, ll d, ll a, ll b){
    if (a+b<=l || a>r) return 0;
    if (a>=l&& a+b-1<=r){
        ll na, nb;
        na=(u-s+d-1)/d; nb=(v-s)/d+1;
        if (na<0) na=0; if (nb>b) nb=b;
        nb-=na;
        if (nb<=0) return 0;

        nb%=mod; na%=mod; d%=mod;
        return ((s+d*na%mod)*nb+nb*(nb-1)/2%mod*d)%mod;
    }
    int m=(b+1)/2;
    return (go(s,2*d,a,m)+go(s+d,2*d,a+m,b-m))%mod;
}
//1 3 2 4

int main(){
    int m; cin>>n>>m>>mod;
    REP(i,m){
        //scanf("%Ld %Ld %Ld %Ld", &l,&r,&u,&v);
        scanf("%I64d %I64d %I64d %I64d", &l,&r,&u,&v);
        ll res=go(1,1,1,n);
        //printf("%Ld\n", res);
        printf("%I64d\n", res);
    }

}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <climits>
//#include <ext/hash_map>

using namespace std;
using namespace __gnu_cxx;

#define REP(i,n) for(int i = 0; i < int(n); ++i)
#define REPV(i, n) for (int i = (n) - 1; (int)i >= 0; --i)
#define FOR(i, a, b) for(int i = (int)(a); i < (int)(b); ++i)

#define FE(i,t) for (__typeof((t).begin())i=(t).begin();i!=(t).end();++i)
#define FEV(i,t) for (__typeof((t).rbegin())i=(t).rbegin();i!=(t).rend();++i)

#define two(x) (1LL << (x))
#define ALL(a) (a).begin(), (a).end()

#define pb push_back
#define ST first
#define ND second
#define MP(x,y) make_pair(x, y)

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<string> vs;

template<class T> void checkmin(T &a, T b){if (b<a)a=b;}
template<class T> void checkmax(T &a, T b){if (b>a)a=b;}
template<class T> void out(T t[], int n){REP(i, n)cout<<t[i]<<" "; cout<<endl;}
template<class T> void out(vector<T> t, int n=-1){for (int i=0; i<(n==-1?t.size():n); ++i) cout<<t[i]<<" "; cout<<endl;}
inline int count_bit(int n){return (n==0)?0:1+count_bit(n&(n-1));}
inline int low_bit(int n){return (n^n-1)&n;}
inline int ctz(int n){return (n==0?-1:ctz(n>>1)+1);}
int toInt(string s){int a; istringstream(s)>>a; return a;}
string toStr(int a){ostringstream os; os<<a; return os.str();}

ll n;
int mod;
ll u, v, l, r;

int go(ll s, ll d, ll a, ll b){
    if (a+b<=l || a>r) return 0;
    if (a>=l&& a+b-1<=r){
        ll na, nb;
        if (s>v) return 0;
        na=(u-s+d-1)/d; nb=(v-s)/d+1;
        if (na<0) na=0; if (nb>b) nb=b;
        nb-=na;
        if (nb<=0) return 0;

        nb%=mod; na%=mod; d%=mod;
        return ((s+d*na%mod)*nb+nb*(nb-1)/2%mod*d)%mod;
    }
    ll m=(b+1)/2;
    return (go(s,2*d,a,m)+go(s+d,2*d,a+m,b-m))%mod;
}
//1 3 2 4

int main(){
    int m; cin>>n>>m>>mod;
    REP(i,m){
        //scanf("%Ld %Ld %Ld %Ld", &l,&r,&u,&v);
        scanf("%I64d %I64d %I64d %I64d", &l,&r,&u,&v);
        ll res=go(1,1,1,n);
        //printf("%Ld\n", res);
        printf("%I64d\n", res);
    }

}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <climits>
//#include <ext/hash_map>

using namespace std;
using namespace __gnu_cxx;

#define REP(i,n) for(int i = 0; i < int(n); ++i)
#define REPV(i, n) for (int i = (n) - 1; (int)i >= 0; --i)
#define FOR(i, a, b) for(int i = (int)(a); i < (int)(b); ++i)

#define FE(i,t) for (__typeof((t).begin())i=(t).begin();i!=(t).end();++i)
#define FEV(i,t) for (__typeof((t).rbegin())i=(t).rbegin();i!=(t).rend();++i)

#define two(x) (1LL << (x))
#define ALL(a) (a).begin(), (a).end()

#define pb push_back
#define ST first
#define ND second
#define MP(x,y) make_pair(x, y)

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<string> vs;

template<class T> void checkmin(T &a, T b){if (b<a)a=b;}
template<class T> void checkmax(T &a, T b){if (b>a)a=b;}
template<class T> void out(T t[], int n){REP(i, n)cout<<t[i]<<" "; cout<<endl;}
template<class T> void out(vector<T> t, int n=-1){for (int i=0; i<(n==-1?t.size():n); ++i) cout<<t[i]<<" "; cout<<endl;}
inline int count_bit(int n){return (n==0)?0:1+count_bit(n&(n-1));}
inline int low_bit(int n){return (n^n-1)&n;}
inline int ctz(int n){return (n==0?-1:ctz(n>>1)+1);}
int toInt(string s){int a; istringstream(s)>>a; return a;}
string toStr(int a){ostringstream os; os<<a; return os.str();}

ll n;
int mod;
ll u, v, l, r;

int go(ll s, ll d, ll a, ll b){
    if (a+b<=l || a>r) return 0;
    if (a>=l&& a+b-1<=r){
        ll na, nb;
        if (s>v) return 0;
        na=(u-s+d-1)/d; nb=(v-s)/d+1;
        if (na<0) na=0; if (nb>b) nb=b;
        nb-=na;
        if (nb<=0) return 0;

        nb%=mod; na%=mod; d%=mod;
        return ((s+d*na%mod)*nb+nb*(nb-1)/2%mod*d)%mod;
    }
    int m=(b+1)/2;
    return (go(s,2*d,a,m)+go(s+d,2*d,a+m,b-m))%mod;
}
//1 3 2 4

int main(){
    int m; cin>>n>>m>>mod;
    REP(i,m){
        //scanf("%Ld %Ld %Ld %Ld", &l,&r,&u,&v);
        scanf("%I64d %I64d %I64d %I64d", &l,&r,&u,&v);
        ll res=go(1,1,1,n);
        //printf("%Ld\n", res);
        printf("%I64d\n", res);
    }

}


#include <cstdio>
#include <algorithm>
using namespace std;

const double eps = 1.0e-19;

long long n,l,r,u,v,mod;
int m;

long long ceil(long long x, long long y) {
    if (x>=0) return (x+y-1)/y;
    else return x/y;
}

long long floor(long long x, long long y) {
    if (x>=0) return x/y;
    else return (x-y+1)/y;
}

int solve(long long x,long long y,long long z,long long l,long long r,long long u,long long v) {
    if ((l>r)||(u>v)) return 0;
//for (int i=1; i<z; i++) printf(" ");
//printf("%I64d %I64d %I64d %I64d %I64d %I64d %I64d -> %d\n",x,y,z,l,r,u,v,SOLVE(x,y,z,l,r,u,v));
    long long cnt=(y-x)/z+1;
    if (cnt == r-l+1) {
        if ((x>v)||(y<u)) return 0;
        if (ceil((u-x),z)>=cnt) return 0;
        long long X = x+max(0LL,ceil((u-x),z))*z;
        if (floor((v-x),z)<0) return 0;
        long long Y = x+min(cnt-1,floor((v-x),z))*z;
        if (X>Y) return 0;
        if ((X+Y)&1)
            return (int) ((((X+Y)%mod)*(((Y-X)/z+1)/2)%mod)%mod);
        else
            return (int) ((((X+Y)/2)%mod)*(((Y-X)/z+1)%mod)%mod);
    }
    long long m = (cnt+1)>>1;
    if (r<=m) {
        if (cnt&1)
            return solve(x,y,z<<1,l,r,u,v);
        else
            return solve(x,y-z,z<<1,l,r,u,v);
    }
    else if (l>m) {
        if (cnt&1)
            return solve(x+z,y-z,z<<1,l-m,r-m,u,v);
        else
            return solve(x+z,y,z<<1,l-m,r-m,u,v);
    }
    else {
        if (cnt&1)
            return (int) ( ( solve(x,y,z<<1,l,m,u,v) + solve(x+z,y-z,z<<1,1,r-m,u,v) ) % mod );
        else
            return (int) ( ( solve(x,y-z,z<<1,l,m,u,v) + solve(x+z,y,z<<1,1,r-m,u,v) ) % mod );
    }
}

int main() {
    scanf("%I64d%d%I64d",&n,&m,&mod);
    while (m--) {
        scanf("%I64d%I64d%I64d%I64d",&l,&r,&u,&v);
        printf("%d\n",solve(1,n,1,l,r,u,v));
    }
    return 0;
}


#include <cstdio>
#include <algorithm>
using namespace std;

const double eps = 1.0e-19;

long long n,l,r,u,v,mod;
int m;

long long ceil(long long x, long long y) {
    if (x>=0)
        return (x+y-1)/y;
    else
        return x/y;
}

long long floor(long long x, long long y) {
    if (x>=0)
        return x/y;
    else
        return  (x-y+1)/y;
}

int solve(long long x,long long y,long long z,long long l,long long r,long long u,long long v) {
    if ((l>r)||(u>v)) return 0;
//for (int i=1; i<z; i++) printf(" ");
//printf("%lld %lld %lld %lld %lld %lld %lld -> %d\n",x,y,z,l,r,u,v,SOLVE(x,y,z,l,r,u,v));
    long long cnt=(y-x)/z+1;
    if (cnt == r-l+1) {
        if ((x>v)||(y<u)) return 0;
        long long X = x+max(0LL,ceil((u-x),z))*z;
        long long Y = x+min(cnt-1,floor((v-x),z))*z;
        if ((Y<x)||(X>y)||(X>Y)) return 0;
        if ((X+Y)&1)
            return (int) ((((X+Y)%mod)*(((Y-X)/z+1)/2)%mod)%mod);
        else
            return (int) ((((X+Y)/2)%mod)*(((Y-X)/z+1)%mod)%mod);
    }
    long long m = (cnt+1)>>1;
    if (r<=m) {
        if (cnt&1)
            return solve(x,y,z<<1,l,r,u,v);
        else
            return solve(x,y-z,z<<1,l,r,u,v);
    }
    else if (l>m) {
        if (cnt&1)
            return solve(x+z,y-z,z<<1,l-m,r-m,u,v);
        else
            return solve(x+z,y,z<<1,l-m,r-m,u,v);
    }
    else {
        if (cnt&1)
            return (int) ( ( solve(x,y,z<<1,l,m,u,v) + solve(x+z,y-z,z<<1,1,r-m,u,v) ) % mod );
        else
            return (int) ( ( solve(x,y-z,z<<1,l,m,u,v) + solve(x+z,y,z<<1,1,r-m,u,v) ) % mod );
    }
}

int main() {
    scanf("%lld%d%lld",&n,&m,&mod);
    while (m--) {
        scanf("%lld%lld%lld%lld",&l,&r,&u,&v);
        printf("%d\n",solve(1,n,1,l,r,u,v));
    }
    return 0;
}


#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;

const double eps = 1.0e-19;

long long n,l,r,u,v;
int m,mod;

int solve(long long x,long long y,long long z,long long l,long long r,long long u,long long v) {
    if ((l>r)||(u>v)) return 0;
//for (int i=1; i<z; i++) printf(" ");
//printf("%I64d %I64d %I64d %I64d %I64d %I64d %I64d -> %d\n",x,y,z,l,r,u,v,SOLVE(x,y,z,l,r,u,v));
    long long cnt=(y-x)/z+1;
    if (cnt == r-l+1) {
        long long X = x+max(0LL,(long long)ceil((u-x)/(double)z-eps))*z;
        long long Y = x+min(cnt-1,(long long)floor((v-x)/(double)z+eps))*z;
        if (X>Y) return 0;
        cnt = (Y-X)/z+1;
        if (cnt&1)
            return ( ( (cnt % mod) * (X % mod) ) % mod + ( ( ( ( ((cnt-1)/2) % mod )* ( cnt % mod ) ) % mod ) * ( z % mod ) ) % mod ) % mod;
        else
            return ( ( (cnt % mod) * (X % mod) ) % mod + ( ( ( ( (cnt-1) % mod )* ( (cnt / 2) % mod ) ) % mod ) * ( z % mod ) ) % mod ) % mod;
//      return ((X+Y)*((Y-X)/z+1)/2) % mod;
    }
    long long m = (cnt+1)>>1;
    if (r<=m) {
        if (cnt&1)
            return solve(x,y,z<<1,l,r,u,v);
        else
            return solve(x,y-z,z<<1,l,r,u,v);
    }
    else if (l>m) {
        if (cnt&1)
            return solve(x+z,y-z,z<<1,l-m,r-m,u,v);
        else
            return solve(x+z,y,z<<1,l-m,r-m,u,v);
    }
    else {
        if (cnt&1)
            return ( solve(x,y,z<<1,l,m,u,v) + solve(x+z,y-z,z<<1,1,r-m,u,v) ) % mod;
        else
            return ( solve(x,y-z,z<<1,l,m,u,v) + solve(x+z,y,z<<1,1,r-m,u,v) ) % mod;
    }
}

int main() {
    scanf("%I64d%d%d",&n,&m,&mod);
    while (m--) {
        scanf("%I64d%I64d%I64d%I64d",&l,&r,&u,&v);
        printf("%d\n",solve(1,n,1,l,r,u,v));
    }
    return 0;
}


#include <cstdio>
#include <algorithm>
using namespace std;

long long n,l,r,u,v;
int m,mod;

int solve(long long x,long long y,long long z,long long l,long long r,long long u,long long v) {
    if ((l>r)||(u>v)) return 0;
//printf("%lld %lld %lld %lld %lld %lld %lld\n",x,y,z,l,r,u,v);
    long long cnt=(y-x)/z+1;
    if (cnt == r-l+1) {
        int X = x+max(0LL,(u-x+z-1)/z)*z;
        int Y = x+min(cnt-1,(v-x)/z)*z;
        if (X>Y) return 0;
        cnt = (Y-X)/z+1;
        if (cnt&1)
            return ( ( (cnt % mod) * (X % mod) ) % mod + ( ( ( ( ((cnt-1)/2) % mod )* ( cnt % mod ) ) % mod ) * ( z % mod ) ) % mod ) % mod;
        else
            return ( ( (cnt % mod) * (X % mod) ) % mod + ( ( ( ( (cnt-1) % mod )* ( (cnt / 2) % mod ) ) % mod ) * ( z % mod ) ) % mod ) % mod;
//      return ((X+Y)*((Y-X)/z+1)/2) % mod;
    }
    long long m = (cnt+1)>>1;
    if (r<=m) {
        if (cnt&1)
            return solve(x,y,z<<1,l,r,u,v);
        else
            return solve(x,y-z,z<<1,l,r,u,v);
    }
    else if (l>m) {
        if (cnt&1)
            return solve(x+z,y-z,z<<1,l-m,r-m,u,v);
        else
            return solve(x+z,y,z<<1,l-m,r-m,u,v);
    }
    else {
        if (cnt&1)
            return ( solve(x,y,z<<1,l,m,u,v) + solve(x+z,y-z,z<<1,1,r-m,u,v) ) % mod;
        else
            return ( solve(x,y-z,z<<1,l,m,u,v) + solve(x+z,y,z<<1,1,r-m,u,v) ) % mod;
    }
}

int main() {
    scanf("%lld%d%d",&n,&m,&mod);
    while (m--) {
        scanf("%lld%lld%lld%lld",&l,&r,&u,&v);
        printf("%d\n",solve(1,n,1,l,r,u,v));
    }
    return 0;
}


#include <cstdio>
#include <algorithm>
using namespace std;

long long n,l,r,u,v;
int m,mod;

int solve(long long x,long long y,long long z,long long l,long long r,long long u,long long v) {
    if ((l>r)||(u>v)) return 0;
//printf("%lld %lld %lld %lld %lld %lld %lld\n",x,y,z,l,r,u,v);
    long long cnt=(y-x)/z+1;
    if (cnt == r-l+1) {
        int X = x+max(0LL,(u-x+z-1)/z)*z;
        int Y = x+min(cnt-1,(v-x)/z)*z;
        if (X>Y) return 0;
        cnt = (Y-X)/z+1;
        if (cnt&1)
            return (cnt % mod) * (X % mod) + ( ((cnt-1)/2) % mod )* ( cnt % mod ) * ( z % mod ) ;
        else
            return (cnt % mod) * (X % mod) + ( (cnt-1) % mod )* ( (cnt/2) % mod ) * ( z % mod ) ;
//      return ((X+Y)*((Y-X)/z+1)/2) % mod;
    }
    long long m = (cnt+1)>>1;
    if (r<=m) {
        if (cnt&1) {
            return solve(x,y,z<<1,l,r,u,v);
        }
        else {
            return solve(x,y-z,z<<1,l,r,u,v);
        }
    }
    else if (l>m) {
        if (cnt&1) {
            return solve(x+z,y-z,z<<1,l-m,r-m,u,v);
        }
        else {
            return solve(x+z,y,z<<1,l-m,r-m,u,v);
        }
    }
    else {
        if (cnt&1) {
            return solve(x,y,z<<1,l,m,u,v) + solve(x+z,y-z,z<<1,1,r-m,u,v);
        }
        else {
            return solve(x,y-z,z<<1,l,m,u,v) + solve(x+z,y,z<<1,1,r-m,u,v);
        }
    }
/*
    if (cnt&1)
        return (solve(x,y,z<<1,(l+2)>>1,(r+1)>>1,u,v) + solve(x+z,y-z,z<<1,(l+1)>>1,(r)>>1,u,v)) % mod;
    else
        return (solve(x,y-z,z<<1,(l+2)>>1,(r+1)>>1,u,v) + solve(x+z,y,z<<1,(l+1)>>1,(r)>>1,u,v)) % mod;
*/
}

int main() {
    scanf("%lld%d%d",&n,&m,&mod);
    while (m--) {
        scanf("%lld%lld%lld%lld",&l,&r,&u,&v);
        printf("%d\n",solve(1,n,1,l,r,u,v));
    }
    return 0;
}


#include <cstdio>
#include <algorithm>
using namespace std;

const double eps = 1.0e-19;

long long n,l,r,u,v,mod;
int m;

long long ceil(long long x, long long y) {
    if (x>=0)
        return (x+y-1)/y;
    else
        return x/y;
}

long long floor(long long x, long long y) {
    if (x>=0)
        return x/y;
    else
        return  (x-y+1)/y;
}

int solve(long long x,long long y,long long z,long long l,long long r,long long u,long long v) {
    if ((l>r)||(u>v)) return 0;
//for (int i=1; i<z; i++) printf(" ");
//printf("%lld %lld %lld %lld %lld %lld %lld -> %d\n",x,y,z,l,r,u,v,SOLVE(x,y,z,l,r,u,v));
    long long cnt=(y-x)/z+1;
    if (cnt == r-l+1) {
        if ((x>v)||(y<u)) return 0;
        if (ceil((u-x),z)>=cnt) return 0;
        long long X = x+max(0LL,ceil((u-x),z))*z;
        if (floor((v-x),z)<0) return 0;
        long long Y = x+min(cnt-1,floor((v-x),z))*z;
        if (X>Y) return 0;
        if ((X+Y)&1)
            return (int) ((((X+Y)%mod)*(((Y-X)/z+1)/2)%mod)%mod);
        else
            return (int) ((((X+Y)/2)%mod)*(((Y-X)/z+1)%mod)%mod);
    }
    long long m = (cnt+1)>>1;
    if (r<=m) {
        if (cnt&1)
            return solve(x,y,z<<1,l,r,u,v);
        else
            return solve(x,y-z,z<<1,l,r,u,v);
    }
    else if (l>m) {
        if (cnt&1)
            return solve(x+z,y-z,z<<1,l-m,r-m,u,v);
        else
            return solve(x+z,y,z<<1,l-m,r-m,u,v);
    }
    else {
        if (cnt&1)
            return (int) ( ( solve(x,y,z<<1,l,m,u,v) + solve(x+z,y-z,z<<1,1,r-m,u,v) ) % mod );
        else
            return (int) ( ( solve(x,y-z,z<<1,l,m,u,v) + solve(x+z,y,z<<1,1,r-m,u,v) ) % mod );
    }
}

int main() {
    scanf("%lld%d%lld",&n,&m,&mod);
    while (m--) {
        scanf("%lld%lld%lld%lld",&l,&r,&u,&v);
        printf("%d\n",solve(1,n,1,l,r,u,v));
    }
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <assert.h>
#include <vector>

using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c) for (__typeof((c).begin())it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

ll n, m, M;
ll u, v;

ll Arith(ll first, ll diff, ll left, ll right) {
  //cout << first << " " << left << " " << right << endl;
  assert(left <= right);
  //if (u == v && u % 2 != first % 2) { return 0; }
  if (first + right * diff < u || v < first + left * diff) { return 0; }
  ll S = max(u, first + left * diff);
  ll E = min(v, first + right * diff);
  {
    ll plus = first % diff - S % diff;
    if (plus < 0) { plus += diff; }
    S += plus;
    plus = E % diff - first % diff;
    if (plus < 0) { plus += diff; }
    E -= plus;
  }
  //cout << S << " "<< E << endl;
  if (S > E) { return 0; }
  ll N = (E - S) / diff + 1;
  return (S % M * (N % M) % M + N % M * ((N - 1) % M) % M / 2 * (diff % M)) % M;
}

ll calc(ll first, ll diff, ll l, ll r, ll w) {
  if (l == 0 && r == w - 1) {
    return Arith(first, diff, l, r);
  }
  ll ret = 0;
  ll center = (w + 1) / 2;
  if (l < center) {
    ret += calc(first, diff * 2, l, min(r, center  - 1), center);
  }
  if (r >= center) {
    ret += calc(first + diff, diff * 2, max(l - center, 0LL), r - center, w - center);
  }
  ret %= M;
  return ret;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  while (cin >> n >> m >> M) {
    REP(i, m) {
      ll l, r;
      cin >> l >> r >> u >> v;
      l--; r--;
      ll ans = calc(1, 1, l, r, n);
      printf("%d\n", (int)ans);
    }
  }
}


#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <assert.h>
#include <vector>

using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c) for (__typeof((c).begin())it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

ll n, m, M;
ll u, v;

ll calc(ll first, ll left, ll right) {
  //cout << first << " " << left << " " << right << endl;
  assert(left <= right);
  if (u == v && v % 2 != first % 2) { return 0; }
  if (first + right * 2 < u || v < first + left * 2) { return 0; }
  ll S = max(u + ((u % 2 == first % 2) ? 0 : 1), first + left * 2);
  ll E = min(v - ((v % 2 == first % 2) ? 0 : 1), first + right * 2);
  if (S > E) { return 0; }
  ll N = (E - S) / 2 + 1;
  return (N % M) * ((S % M + N % M - 1) % M) % M;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  while (cin >> n >> m >> M) {
    REP(i, m) {
      ll l, r;
//#ifdef LLD
      //scanf("%lld %lld %lld %lld", &l, &r, &u, &v);
//#else
      cin >> l >> r >> u >> v;
      //scanf("%I64d %I64d %I64d %I64d", &l &r, &u, &v);
//#endif
      l--; r--;
      ll ans = 0;
      ll center = (n + 1) / 2;
      if (l < center) {
        ans += calc(1, l, min(r, center - 1));
      }
      if (r >= center) {
        ans += calc(2, max(l - center, 0LL), r - center);
      }
      ans %= M;
      printf("%d\n", (int)ans);
    }
  }
}


#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <assert.h>
#include <vector>

using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c) for (__typeof((c).begin())it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

ll n, m, M;
ll u, v;

ll calc(ll first, ll left, ll right) {
  //cout << first << " " << left << " " << right << endl;
  assert(left <= right);
  if (u == v && v % 2 != first % 2) { return 0; }
  if (first + right * 2 < u || v < first + left * 2) { return 0; }
  ll S = max(u + (u % 2 == first % 2 ? 0 : 1), first + left * 2);
  ll E = min(v - (v % 2 == first % 2 ? 0 : 1), first + right * 2);
  if (S > E) { return 0; }
  ll N = (E - S) / 2 + 1;
  return (N % M) * ((S % M + N % M - 1) % M) % M;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  while (cin >> n >> m >> M) {
    REP(i, m) {
      ll l, r;
//#ifdef LLD
      //scanf("%lld %lld %lld %lld", &l, &r, &u, &v);
//#else
      cin >> l >> r >> u >> v;
      //scanf("%I64d %I64d %I64d %I64d", &l &r, &u, &v);
//#endif
      l--; r--;
      ll ans = 0;
      ll center = (n + 1) / 2;
      if (l < center) {
        ans += calc(1, l, min(r, center - 1));
      }
      if (r >= center) {
        ans += calc(2, max(l - center, 0LL), r - center);
      }
      ans %= M;
      printf("%d\n", (int)ans);
    }
  }
}