//Karol Kaszuba
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <unordered_map>
#include <unordered_set>
#include <cassert>

using namespace std;

typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef double D;
typedef long double LD;
typedef vector<PII> VII;

#define FOR(i,x,y) for(auto i=(x);i<=(y);++i)
#define REP(i,x) FOR(i,0,(x)-1)
#define FORD(i,x,y) for(auto i=(x);i>=(y);--i)
#define VAR(i,c) __typeof(c) i=(c)
#define FORE(i,c) for(VAR(i,(c).begin());i!=(c).end();++i)

#define SIZE(c) (int)((c).size())
#define ALL(c) (c).begin(),(c).end()
#define PB push_back
#define IN insert
#define ER erase
#define MP make_pair
#define ST first
#define ND second
#define IOSYNC ios_base::sync_with_stdio(0)
#define FREOPEN(f) freopen(f, "r", stdin),freopen(f, "w", stdout)

LL qmnoz(LL a, LL b, LL MOD)
{
	LL res = 0;
	while(b)
	{
		if(b & 1)
		{
			res = (res + a) % MOD;
		}
		a = (a + a) % MOD;
		b = b >> 1;
	}
	return res;
}

inline LL qpot(LL a, LL b, LL MOD)
{
	LL res = 1;
	if(b == 0) return 1;
	if(a % MOD == 0) return 0;
	while(b)
	{
		if(b & 1)
		{
			//res = qmnoz(res, a, MOD);
			res = (res * a) % MOD;
		}
		a = (a * a) % MOD;
		//a = qmnoz(a, a, MOD);
		b = b >> 1;
	}
	return res;
}

void jebaj()
{
	LL k, l, r, p;
	cin >> k >> l >> r >> p;
	if(p == 2)
	{
		if(k % 2)
		{
			cout << "0\n";
			return;
		}
		else
		{
			cout << "1\n";
			return;
		}
	}
	LL x = qpot(k, qpot(2, l, p - 1), p);
	LL res = 0;
	if(x == 1)
	{
		res = qpot(2, r - l + 1, p);
	}
	else
	{
		res = (qpot(x, qpot(2, r - l + 1, p - 1), p) + p - 1) % p;
		res = (res * qpot((x + p - 1) % p, p - 2, p)) % p;
	}
	if(k % 2 == 1)
	{
		res = (res * qpot(qpot(2, r - l, p), p - 2, p)) % p;
	}
	if(res < 0) res += p;
	cout << res << "\n";
	return;
}

int main()
{
	IOSYNC;
	int t;
	t = 1;
	cin >> t;

	REP(i, t)
	{
		jebaj();
	}
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<vector>
#define pi acos(-1.0)
#define MAX 20002
using namespace std;
typedef long long LL;
int mod;
LL k, l, r, p, phi;
LL POW(LL a, LL b, LL mod) {
    a %= mod;
    LL res = 1;
    while(b) {
        if(b & 1)
            res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return res;
}
int main() {
    //cout<<POW(8,1024,9973)<<endl;
    int T;
    scanf("%d", &T);
    LL x, t,ans;
    while(T--) {
        scanf("%I64d%I64d%I64d%I64d", &k, &l, &r, &p);
        if(k == 1) {
            printf("%d\n", (int) (2 % p));
            continue;
        }
        if(p == 2) {
            printf("%d\n", !(k & 1));
            continue;
        }

        if(l <= 30) {
            t = POW(k, 1 << l, p);
        }
        else {
            x = POW(2, l, p - 1);
            x += p - 1;
            t = POW(k, x, p);
        }
        if(!t) {
            ans=1;
        }
        else if(t == 1)
            ans = POW(2, r - l + 1, p);
        else {
            LL y = POW(2, r - l + 1, p - 1);
            LL a = POW(t, y, p);
            //cout << a << " " << t << endl;
            ans = (a - 1) * POW(t - 1, p - 2, p) % p;
        }
        //cout<<"ans "<<a<<endl;
        if((k & 1) && (r ^ l)) {
            t = POW(2, r - l, p);
            t = POW(t, p - 2, p);
            //cout << "t " << t << endl;
            ans = ans * t % p;
        }
        ans = (ans % p + p) % p;
        printf("%I64d\n", ans);
    }
    return 0;
}
/*
 8 10 16 9973
 9 10 16 9973
 1 10000 1000000 9973
 */


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<vector>
#define pi acos(-1.0)
#define MAX 20002
using namespace std;
typedef long long LL;
int mod;
LL k, l, r, p, phi;
LL POW(LL a, LL b, LL mod) {
    a %= mod;
    LL res = 1;
    while(b) {
        if(b & 1)
            res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return res;
}
int main() {
    //cout<<POW(8,1024,9973)<<endl;
    int T;
    scanf("%d", &T);

    while(T--) {
        scanf("%I64d%I64d%I64d%I64d", &k, &l, &r, &p);
        LL x = POW(2, l, p - 1);
        LL y = POW(2, r - l + 1, p - 1);
        LL t = POW(k, x, p);
        //cout << t << " " << y << endl;
        LL a = POW(t, y, p);
        //cout << " " << a << " " << endl;
        LL ans = (a - 1) * POW(t - 1, p - 2, p) % p;

        if((k & 1) && (r ^ l)) {
            t = POW(2, r - l, p);
            t = POW(t, p - 2, p);
            ans = ans * t % p;
        }
        ans = (ans % p + p) % p;
        printf("%I64d\n", ans);
    }
    return 0;
}
/*
 8 10 16 9973
 9 10 16 9973

 */


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<vector>
#define ll __int64
#define pi acos(-1.0)
#define MAX 20002
using namespace std;
typedef long long LL;
int mod;
LL k, l, r, p, phi;
LL POW(LL a, LL b, LL mod) {
    a %= mod;
    LL res = 1;
    while(b) {
        if(b & 1)
            res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return res;
}
int main() {
    int T;
    scanf("%d", &T);
    while(T--) {
        scanf("%I64d%I64d%I64d%I64d", &k, &l, &r, &p);
        LL x = POW(2, l, p - 1);
        LL y = POW(2, r - l + 1, p - 1);
        LL t = POW(k, x, p);
        LL a = POW(t, y, p);
        LL ans = (a - 1) * POW(t - 1, p - 2, p) % p;
        printf("%I64d\n",ans);
    }
    return 0;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<vector>
#define ll __int64
#define pi acos(-1.0)
#define MAX 20002
using namespace std;
typedef long long LL;
int mod;
LL k, l, r, p, phi;
LL POW(LL a, LL b, LL mod) {
    a %= mod;
    LL res = 1;
    while(b) {
        if(b & 1)
            res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return res;
}
int main() {
    int T;
    scanf("%d", &T);
    while(T--) {
        scanf("%I64d%I64d%I64d%I64d", &k, &l, &r, &p);
        LL x = POW(2, l, p - 1);
        LL y = POW(2, r - l + 1, p - 1);
        LL t = POW(k, x, p);
        LL a = POW(t, y, p);
        LL ans = (a - 1) * POW(t - 1, p - 2, p) % p;
        if(k & 1) {
            t = POW(2, r - l, p - 1);
            t = POW(t, p - 2, p);
            ans = ans * t % p;
        }
        printf("%I64d\n", ans);
    }
    return 0;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<vector>
#define pi acos(-1.0)
#define MAX 20002
using namespace std;
typedef long long LL;
int mod;
LL k, l, r, p, phi;
LL POW(LL a, LL b, LL mod) {
    a %= mod;
    LL res = 1;
    while(b) {
        if(b & 1)
            res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return res;
}
int main() {
    //cout<<POW(8,1024,9973)<<endl;
    int T;
    scanf("%d", &T);
    LL x, t,ans;
    while(T--) {
        scanf("%I64d%I64d%I64d%I64d", &k, &l, &r, &p);
        if(k == 1) {
            printf("%d\n", (int) (2 % p));
            continue;
        }
        if(p == 2) {
            printf("%d\n", !(k & 1));
            continue;
        }

        if(l <= 30) {
            t = POW(k, 1 << l, p);
        }
        else {
            x = POW(2, l, p - 1);
            x += p - 1;
            t = POW(k, x, p);
        }
        if(!t) {
            puts("1");
            continue;
        }
        if(t == 1)
            ans = POW(2, r - l + 1, p);
        else {
            LL y = POW(2, r - l + 1, p - 1);
            LL a = POW(t, y, p);
            //cout << a << " " << t << endl;
            ans = (a - 1) * POW(t - 1, p - 2, p) % p;
        }
        //cout<<"ans "<<a<<endl;
        if((k & 1) && (r ^ l)) {
            t = POW(2, r - l, p);
            t = POW(t, p - 2, p);
            cout << "t " << t << endl;
            ans = ans * t % p;
        }
        ans = (ans % p + p) % p;
        printf("%I64d\n", ans);
    }
    return 0;
}
/*
 8 10 16 9973
 9 10 16 9973
 1 10000 1000000 9973
 */


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<vector>
#define pi acos(-1.0)
#define MAX 20002
using namespace std;
typedef long long LL;
int mod;
LL k, l, r, p, phi;
LL POW(LL a, LL b, LL mod) {
    a %= mod;
    LL res = 1;
    while(b) {
        if(b & 1)
            res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return res;
}
int main() {
    //cout<<POW(8,1024,9973)<<endl;
    int T;
    scanf("%d", &T);

    while(T--) {
        scanf("%I64d%I64d%I64d%I64d", &k, &l, &r, &p);
        if(k == 1) {
            printf("%d\n", (int) (2 % p));
            continue;
        }
        if(p == 2) {
            printf("%d\n", !(k & 1));
            continue;
        }
        LL x = POW(2, l, p - 1);
        LL y = POW(2, r - l + 1, p - 1);
        LL t = POW(k, x, p);
        //cout << t << " " << y << endl;
        LL a = POW(t, y, p);
        //cout << a << " "<<t << endl;
        LL ans;
        if(t == 1)
            ans = (r - l + 1) % p;
        else
            ans = (a - 1) * POW(t - 1, p - 2, p) % p;
        //cout<<"ans "<<a<<endl;
        if((k & 1) && (r ^ l)) {
            t = POW(2, r - l, p);
            t = POW(t, p - 2, p);
            ans = ans * t % p;
        }
        ans = (ans % p + p) % p;
        printf("%I64d\n", ans);
    }
    return 0;
}
/*
 8 10 16 9973
 9 10 16 9973
 1 10000 1000000 9973
 */


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<vector>
#define pi acos(-1.0)
#define MAX 20002
using namespace std;
typedef long long LL;
int mod;
LL k, l, r, p, phi;
LL POW(LL a, LL b, LL mod) {
	a %= mod;
	LL res = 1;
	while(b) {
		if(b & 1)
			res = res * a % mod;
		a = a * a % mod;
		b >>= 1;
	}
	return res;
}
int main() {
	//cout<<POW(8,1024,9973)<<endl;
	int T;
	scanf("%d", &T);

	while(T--) {
		scanf("%I64d%I64d%I64d%I64d", &k, &l, &r, &p);
		if(k == 1) {
			printf("%d\n", (int) (2 % p));
			continue;
		}
		if(p==2){
			printf("%d\n",!(k&1));
			continue;
		}
		LL x = POW(2, l, p - 1);
		LL y = POW(2, r - l + 1, p - 1);
		LL t = POW(k, x, p);
		//cout << t << " " << y << endl;
		LL a = POW(t, y, p);
		//cout << a << " "<<t << endl;
		LL ans = (a - 1) * POW(t - 1, p - 2, p) % p;
		//cout<<"ans "<<a<<endl;
		if((k & 1) && (r ^ l)) {
			t = POW(2, r - l, p);
			t = POW(t, p - 2, p);
			ans = ans * t % p;
		}
		ans = (ans % p + p) % p;
		printf("%I64d\n", ans);
	}
	return 0;
}
/*
 8 10 16 9973
 9 10 16 9973
 1 10000 1000000 9973
 */


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<vector>
#define pi acos(-1.0)
#define MAX 20002
using namespace std;
typedef long long LL;
int mod;
LL k, l, r, p, phi;
LL POW(LL a, LL b, LL mod) {
    a %= mod;
    LL res = 1;
    while(b) {
        if(b & 1)
            res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return res;
}
int main() {
    //cout<<POW(8,1024,9973)<<endl;
    int T;
    scanf("%d", &T);

    while(T--) {
        scanf("%I64d%I64d%I64d%I64d", &k, &l, &r, &p);
        if(k == 1) {
            printf("%d\n", (int) (2 % p));
            continue;
        }
        if(p == 2) {
            printf("%d\n", !(k & 1));
            continue;
        }
        LL x = POW(2, l, p - 1);

        LL t = POW(k, x, p);
        LL ans;
        //cout << t << endl;
        if(t == 1)
            ans = POW(2, r - l + 1, p);
        else {
            LL y = POW(2, r - l + 1, p - 1);
            LL a = POW(t, y, p);
            cout << a << " " << t << endl;
            ans = (a - 1) * POW(t - 1, p - 2, p) % p;
        }
        //cout<<"ans "<<a<<endl;
        if((k & 1) && (r ^ l)) {
            t = POW(2, r - l, p);
            t = POW(t, p - 2, p);
            ans = ans * t % p;
        }
        ans = (ans % p + p) % p;
        printf("%I64d\n", ans);
    }
    return 0;
}
/*
 8 10 16 9973
 9 10 16 9973
 1 10000 1000000 9973
 */


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<vector>
#define pi acos(-1.0)
#define MAX 20002
using namespace std;
typedef long long LL;
int mod;
LL k, l, r, p, phi;
LL POW(LL a, LL b, LL mod) {
    a %= mod;
    LL res = 1;
    while(b) {
        if(b & 1)
            res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return res;
}
int main() {
    //cout<<POW(8,1024,9973)<<endl;
    int T;
    scanf("%d", &T);

    while(T--) {
        scanf("%I64d%I64d%I64d%I64d", &k, &l, &r, &p);
        if(k == 1) {
            printf("%d\n", (int) (2 % p));
            continue;
        }
        if(p == 2) {
            printf("%d\n", !(k & 1));
            continue;
        }
        LL x = POW(2, l, p - 1);

        LL t = POW(k, x, p);
        LL ans;
        //cout << t << endl;
        if(t == 1)
            ans = POW(2, r - l + 1, p);
        else {
            LL y = POW(2, r - l + 1, p - 1);
            LL a = POW(t, y, p);
            //cout << a << " " << t << endl;
            ans = (a - 1) * POW(t - 1, p - 2, p) % p;
        }
        //cout<<"ans "<<a<<endl;
        if((k & 1) && (r ^ l)) {
            t = POW(2, r - l, p);
            t = POW(t, p - 2, p);
            ans = ans * t % p;
        }
        ans = (ans % p + p) % p;
        printf("%I64d\n", ans);
    }
    return 0;
}
/*
 8 10 16 9973
 9 10 16 9973
 1 10000 1000000 9973
 */


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<vector>
#define ll __int64
#define pi acos(-1.0)
#define MAX 20002
using namespace std;
typedef long long LL;
int mod;
LL k, l, r, p, phi;
LL POW(LL a, LL b, LL mod) {
    a %= mod;
    LL res = 1;
    while(b) {
        if(b & 1)
            res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return res;
}
int main() {
    int T;
    scanf("%d", &T);
    while(T--) {
        scanf("%I64d%I64d%I64d%I64d", &k, &l, &r, &p);
        LL x = POW(2, l, p - 1);
        LL y = POW(2, r - l + 1, p - 1);
        LL t = POW(k, x, p);
        LL a = POW(t, y, p);
        LL ans = (a + p - 1) * POW(t - 1, p - 2, p) % p;
        if((k & 1)) {
            t = POW(2, r - l, p);
            t = POW(t, p - 2, p);
            ans = ans * t % p;
        }
        printf("%I64d\n", ans);
    }
    return 0;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<vector>
#define pi acos(-1.0)
#define MAX 20002
using namespace std;
typedef long long LL;
int mod;
LL k, l, r, p, phi;
LL POW(LL a, LL b, LL mod) {
    a %= mod;
    LL res = 1;
    while(b) {
        if(b & 1)
            res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return res;
}
int main() {
    //cout<<POW(8,1024,9973)<<endl;
    int T;
    scanf("%d", &T);
    LL x, t,ans;
    while(T--) {
        scanf("%I64d%I64d%I64d%I64d", &k, &l, &r, &p);
        if(k == 1) {
            printf("%d\n", (int) (2 % p));
            continue;
        }
        if(p == 2) {
            printf("%d\n", !(k & 1));
            continue;
        }

        if(l <= 30) {
            t = POW(k, 1 << l, p);
        }
        else {
            x = POW(2, l, p - 1);
            x += p - 1;
            t = POW(k, x, p);
        }
        if(!t) {
            puts("1");
            continue;
        }
        if(t == 1)
            ans = POW(2, r - l + 1, p);
        else {
            LL y = POW(2, r - l + 1, p - 1);
            LL a = POW(t, y, p);
            //cout << a << " " << t << endl;
            ans = (a - 1) * POW(t - 1, p - 2, p) % p;
        }
        //cout<<"ans "<<a<<endl;
        if((k & 1) && (r ^ l)) {
            t = POW(2, r - l, p);
            t = POW(t, p - 2, p);
            //cout << "t " << t << endl;
            ans = ans * t % p;
        }
        ans = (ans % p + p) % p;
        printf("%I64d\n", ans);
    }
    return 0;
}
/*
 8 10 16 9973
 9 10 16 9973
 1 10000 1000000 9973
 */


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<vector>
#define pi acos(-1.0)
#define MAX 20002
using namespace std;
typedef long long LL;
int mod;
LL k, l, r, p, phi;
LL POW(LL a, LL b, LL mod) {
	a %= mod;
	LL res = 1;
	while(b) {
		if(b & 1)
			res = res * a % mod;
		a = a * a % mod;
		b >>= 1;
	}
	return res;
}
int main() {
	//cout<<POW(8,1024,9973)<<endl;
	int T;
	scanf("%d", &T);

	while(T--) {
		scanf("%I64d%I64d%I64d%I64d", &k, &l, &r, &p);
		if(k == 1) {
			printf("%d\n", (int) (2 % p));
			continue;
		}
		if(p == 2) {
			printf("%d\n", !(k & 1));
			continue;
		}
		LL x = POW(2, l, p - 1);

		LL t = POW(k, x, p);
		LL ans;
		if(t == 1)
			ans = (r - l + 1) % p;
		else{
			LL y = POW(2, r - l + 1, p - 1);
			LL a = POW(t, y, p);
			//cout<<a<<" "<<t<<endl;
			ans = (a - 1) * POW(t - 1, p - 2, p) % p;
		}
		//cout<<"ans "<<a<<endl;
		if((k & 1) && (r ^ l)) {
			t = POW(2, r - l, p);
			t = POW(t, p - 2, p);
			ans = ans * t % p;
		}
		ans = (ans % p + p) % p;
		printf("%I64d\n", ans);
	}
	return 0;
}
/*
 8 10 16 9973
 9 10 16 9973
 1 10000 1000000 9973
 */


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<vector>
#define ll __int64
#define pi acos(-1.0)
#define MAX 20002
using namespace std;
typedef long long LL;
int mod;
LL k, l, r, p, phi;
LL POW(LL a, LL b, LL mod) {
    a %= mod;
    LL res = 1;
    while(b) {
        if(b & 1)
            res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return res;
}
int main() {
    int T;
    scanf("%d", &T);
    while(T--) {
        scanf("%I64d%I64d%I64d%I64d", &k, &l, &r, &p);
        LL x = POW(2, l, p - 1);
        LL y = POW(2, r - l + 1, p - 1);
        LL t = POW(k, x, p);
        LL a = POW(t, y, p);
        LL ans = (a - 1) * POW(t - 1, p - 2, p) % p;
        ans = (ans % p + p) % p;
        if(k & 1) {
            t = POW(2, r - l, p);
            t = POW(t, p - 2, p);
            ans = ans * t % p;
        }
        printf("%I64d\n", ans);
    }
    return 0;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<vector>
#define pi acos(-1.0)
#define MAX 20002
using namespace std;
typedef long long LL;
int mod;
LL k, l, r, p, phi;
LL POW(LL a, LL b, LL mod) {
    a %= mod;
    LL res = 1;
    while(b) {
        if(b & 1)
            res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return res;
}
int main() {
    //cout<<POW(8,1024,9973)<<endl;
    int T;
    scanf("%d", &T);

    while(T--) {
        scanf("%I64d%I64d%I64d%I64d", &k, &l, &r, &p);
        LL x = POW(2, l, p - 1);
        LL y = POW(2, r - l + 1, p - 1);
        LL t = POW(k, x, p);
        //cout << t << " " << y << endl;
        LL a = POW(t, y, p);
        //cout << " " << a << " " << endl;
        LL ans = (a - 1) * POW(t - 1, p - 2, p) % p;

        if((k & 1) && (r ^ l)) {
            t = POW(2, r - l + ((r-l)>1), p);
            t = POW(t, p - 2, p);
            ans = ans * t % p;
        }
        ans = (ans % p + p) % p;
        printf("%I64d\n", ans);
    }
    return 0;
}
/*
 8 10 16 9973
 9 10 16 9973

 */


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<vector>
#define ll __int64
#define pi acos(-1.0)
#define MAX 20002
using namespace std;
typedef long long LL;
int mod;
LL k, l, r, p, phi;
LL POW(LL a, LL b, LL mod) {
    a %= mod;
    LL res = 1;
    while(b) {
        if(b & 1)
            res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return res;
}
int main() {
    int T;
    scanf("%d", &T);
    while(T--) {
        scanf("%I64d%I64d%I64d%I64d", &k, &l, &r, &p);
        LL x = POW(2, l, p - 1);
        LL y = POW(2, r - l + 1, p - 1);
        LL t = POW(k, x, p);
        LL a = POW(t, y, p);
        LL ans = (a - 1) * POW(t - 1, p - 2, p) % p;
        if((k & 1)) {
            t = POW(2, r - l, p);
            t = POW(t, p - 2, p);
            ans = ans * t % p;
        }
        printf("%I64d\n", ans);
    }
    return 0;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<vector>
#define pi acos(-1.0)
#define MAX 20002
using namespace std;
typedef long long LL;
int mod;
LL k, l, r, p, phi;
LL POW(LL a, LL b, LL mod) {
    a %= mod;
    LL res = 1;
    while(b) {
        if(b & 1)
            res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return res;
}
int main() {
    //cout<<POW(8,1024,9973)<<endl;
    int T;
    scanf("%d", &T);

    while(T--) {
        scanf("%I64d%I64d%I64d%I64d", &k, &l, &r, &p);
        if(k == 1) {
            printf("%d\n", (int) (2 % p));
            continue;
        }
        LL x = POW(2, l, p - 1);
        LL y = POW(2, r - l + 1, p - 1);
        LL t = POW(k, x, p);
        //cout << t << " " << y << endl;
        LL a = POW(t, y, p);
        //cout << a << " "<<t << endl;
        LL ans = (a - 1) * POW(t - 1, p - 2, p) % p;
        //cout<<"ans "<<a<<endl;
        if((k & 1) && (r ^ l)) {
            t = POW(2, r - l, p);
            t = POW(t, p - 2, p);
            ans = ans * t % p;
        }
        ans = (ans % p + p) % p;
        printf("%I64d\n", ans);
    }
    return 0;
}
/*
 8 10 16 9973
 9 10 16 9973
 1 10000 1000000 9973
 */


#include <iostream>
using namespace std;
#define ll long long

ll exp(ll A, ll B, ll N)
{
	ll result=1;
	ll temp=A;
	while(B)
	{
		if(B&1)
		{
			result*=temp;
			result%=N;
		}
		temp*=temp;
		temp%=N;
		B>>=1;
	}
	return result;
}

ll exp2exp(ll K, ll U, ll P) // returns k^(2^U) mod P
{
	ll e=exp(2,U,P-1);
	return exp(K,e,P);
}

ll inverse(ll k, ll p)
{
	return exp(k, p-2, p);
}

ll solve(ll k, ll l, ll r, ll p)
{
	if(p==2 && k%2==0) return 1;
	// solve equation k^(2^(r+1))-1=X*(k^(2^l)-1) mod P
	ll LHS=exp2exp(k,r+1,p)-1;
	ll RHS=exp2exp(k,l,p)-1;

	if(RHS%p==0) // trivial case, right side is 0 modulo p
	{
		return exp(2,r-l+1,p);
	}
	else
	{
		return LHS*inverse(RHS,p);
	}
}

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

	ll k, l, r, p;
	for(int i=0; i<n; i++)
	{
		cin >> k >> l >> r >> p;
		cout << solve(k, l, r, p) << '\n';
	}
	return 0;
}


#include <iostream>
using namespace std;
#define ll long long

ll exp(ll A, ll B, ll N)
{
	ll result=1;
	ll temp=A;
	while(B)
	{
		if(B&1)
		{
			result*=temp;
			result%=N;
		}
		temp*=temp;
		temp%=N;
		B>>=1;
	}
	return result;
}

ll exp2exp(ll K, ll U, ll P) // returns k^(2^U) mod P
{
	ll e=exp(2,U,P-1);
	return exp(K,e,P);
}

ll inverse(ll k, ll p)
{
	return exp(k, p-2, p);
}

ll solve(ll k, ll l, ll r, ll p)
{
	if(k==1)
	{
		return exp(2, r-l+1, p);
	}
	if(p==2)
	{
		if(k%2==0) return 1;
		else return 0;
	}
	// solve equation k^(2^(r+1))-1=X*(k^(2^l)-1) mod P
	ll LHS=exp2exp(k,r+1,p)-1;
	ll RHS=exp2exp(k,l,p)-1;

	if(RHS%p==0) // trivial case, right side is 0 modulo p
	{
		return exp(2,r-l+1,p);
	}
	else
	{
		return LHS*inverse(RHS,p);
	}
}

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

	ll k, l, r, p;
	for(int i=0; i<n; i++)
	{
		cin >> k >> l >> r >> p;
		cout << solve(k, l, r, p) << '\n';
	}
	return 0;
}


#include <iostream>
using namespace std;
#define ll long long

ll exp(ll A, ll B, ll N)
{
	ll result=1;
	ll temp=A;
	while(B)
	{
		if(B&1)
		{
			result*=temp;
			result%=N;
		}
		temp*=temp;
		temp%=N;
		B>>=1;
	}
	return result;
}

ll exp2exp(ll K, ll U, ll P) // returns k^(2^U) mod P
{
	ll e=exp(2,U,P-1);
	return exp(K,e,P);
}

ll inverse(ll k, ll p)
{
	return exp(k, p-2, p);
}

ll solve(ll k, ll l, ll r, ll p)
{
	// solve equation k^(2^(r+1))-1=X*(k^(2^l)-1) mod P
	ll LHS=exp2exp(k,r+1,p)-1;
	ll RHS=exp2exp(k,l,p)-1;

	if(RHS%p==0) // trivial case, left side becomes 0
	{
		return exp(2,r-l+1,p);
	}
	else
	{
		return LHS*inverse(RHS,p);
	}
}

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

	ll k, l, r, p;
	for(int i=0; i<n; i++)
	{
		cin >> k >> l >> r >> p;
		cout << solve(k, l, r, p);
	}
	return 0;
}


#include <iostream>
using namespace std;
#define ll long long

ll exp(ll A, ll B, ll N)
{
	ll result=1;
	ll temp=A;
	while(B)
	{
		if(B&1)
		{
			result*=temp;
			result%=N;
		}
		temp*=temp;
		temp%=N;
		B>>=1;
	}
	return result;
}

ll exp2exp(ll K, ll U, ll P) // returns k^(2^U) mod P
{
	ll e=exp(2,U,P-1);
	return exp(K,e,P);
}

ll inverse(ll k, ll p)
{
	return exp(k, p-2, p);
}

ll solve(ll k, ll l, ll r, ll p)
{
	if(k==1)
	{
		return 2%p;
	}
	if(p==2)
	{
		if(k%2==0) return 1;
		else return 0;
	}

	if(k%2==0)
	{
		if(k%p==0)
		{
			return 1;
		}
		// solve equation k^(2^(r+1))-1=X*(k^(2^l)-1) mod P
		ll LHS=exp2exp(k,r+1,p)-1;
		ll RHS=exp2exp(k,l,p)-1;

		if(RHS%p==0) // trivial case, right side is 0 modulo p
		{
			return exp(2,r-l+1,p);
		}
		else
		{
			return (LHS*inverse(RHS,p) )%p;
		}
	}
	else
	{
		// solve equation k^(2^(r+1))-1=X*(k^(2^l)-1) mod P
		ll LHS=exp2exp(k,r+1,p)-1;
		ll RHS=exp2exp(k,l,p)-1;
		if(k%p==0)
		{
			return exp(inverse(2, p), r-l, p);
		}

		if(RHS%p==0) // trivial case, right side is 0 modulo p
		{
			return (exp(2,r-l+1,p) exp(inverse(2,p),r-l,p) )%p;
		}
		else
		{
			return (LHS*inverse(RHS,p)/* *exp(inverse(2,p),r-l,p) */)%p;
		}
	}
}

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

	ll k, l, r, p;
	for(int i=0; i<n; i++)
	{
		cin >> k >> l >> r >> p;
		cout << solve(k, l, r, p) << '\n';
	}
	return 0;
}


#include <iostream>
using namespace std;
#define ll long long

ll exp(ll A, ll B, ll N)
{
	ll result=1;
	ll temp=A;
	while(B)
	{
		if(B&1)
		{
			result*=temp;
			result%=N;
		}
		temp*=temp;
		temp%=N;
		B>>=1;
	}
	return result;
}

ll exp2exp(ll K, ll U, ll P) // returns k^(2^U) mod P
{
	ll e=exp(2,U,P-1);
	return exp(K,e,P);
}

ll inverse(ll k, ll p)
{
	return exp(k, p-2, p);
}

ll solve(ll k, ll l, ll r, ll p)
{
	if(k==1)
	{
		return 2%p;
	}
	if(p==2)
	{
		if(k%2==0) return 1;
		else return 0;
	}
	// solve equation k^(2^(r+1))-1=X*(k^(2^l)-1) mod P
	ll LHS=exp2exp(k,r+1,p)-1;
	ll RHS=exp2exp(k,l,p)-1;

	if(RHS%p==0) // trivial case, right side is 0 modulo p
	{
		return exp(2,r-l+1,p);
	}
	else
	{
		return LHS*inverse(RHS,p);
	}
}

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

	ll k, l, r, p;
	for(int i=0; i<n; i++)
	{
		cin >> k >> l >> r >> p;
		cout << solve(k, l, r, p) << '\n';
	}
	return 0;
}


#include <iostream>
using namespace std;
#define ll long long

ll exp(ll A, ll B, ll N)
{
	ll result=1;
	ll temp=A;
	while(B)
	{
		if(B&1)
		{
			result*=temp;
			result%=N;
		}
		temp*=temp;
		temp%=N;
		B>>=1;
	}
	return result;
}

ll exp2exp(ll K, ll U, ll P) // returns k^(2^U) mod P
{
	ll e=exp(2,U,P-1);
	return exp(K,e,P);
}

ll inverse(ll k, ll p)
{
	return exp(k, p-2, p);
}

ll solve(ll k, ll l, ll r, ll p)
{
	if(k==1)
	{
		return 2%p;
	}
	if(p==2)
	{
		if(k%2==0) return 1;
		else return 0;
	}

	if(k%2==0)
	{
		if(k%p==0)
		{
			return 1;
		}
		// solve equation k^(2^(r+1))-1=X*(k^(2^l)-1) mod P
		ll LHS=exp2exp(k,r+1,p)-1;
		ll RHS=exp2exp(k,l,p)-1;

		if(RHS%p==0) // trivial case, right side is 0 modulo p
		{
			return exp(2,r-l+1,p);
		}
		else
		{
			return (LHS*inverse(RHS,p) )%p;
		}
	}
	else
	{
		// solve equation k^(2^(r+1))-1=X*(k^(2^l)-1) mod P
		ll LHS=exp2exp(k,r+1,p)-1;
		ll RHS=exp2exp(k,l,p)-1;
		if(k%p==0)
		{
			return exp(inverse(2, p), r-l, p);
		}

		if(RHS%p==0) // trivial case, right side is 0 modulo p
		{
			return ( exp(2,r-l+1,p)* exp(inverse(2,p),r-l,p) )%p;
		}
		else
		{
			return ( (LHS*inverse(RHS,p)%p) *exp(inverse(2,p),r-l,p) )%p;
		}
	}
}

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

	ll k, l, r, p;
	for(int i=0; i<n; i++)
	{
		cin >> k >> l >> r >> p;
		cout << solve(k, l, r, p) << '\n';
	}
	return 0;
}


#include <iostream>
using namespace std;
#define ll long long

ll exp(ll A, ll B, ll N)
{
	ll result=1;
	ll temp=A;
	while(B)
	{
		if(B&1)
		{
			result*=temp;
			result%=N;
		}
		temp*=temp;
		temp%=N;
		B>>=1;
	}
	return result;
}

ll exp2exp(ll K, ll U, ll P) // returns k^(2^U) mod P
{
	ll e=exp(2,U,P-1);
	return exp(K,e,P);
}

ll inverse(ll k, ll p)
{
	return exp(k, p-2, p);
}

ll solve(ll k, ll l, ll r, ll p)
{
	if(k==1)
	{
		return 2%p;
	}
	if(p==2)
	{
		if(k%2==0) return 1;
		else return 0;
	}
	// solve equation k^(2^(r+1))-1=X*(k^(2^l)-1) mod P
	ll LHS=exp2exp(k,r+1,p)-1;
	ll RHS=exp2exp(k,l,p)-1;

	if(k%p==0) return 1;

	if(RHS%p==0) // trivial case, right side is 0 modulo p
	{
		return exp(2,r-l+1,p);
	}
	else
	{
		return LHS*inverse(RHS,p);
	}
}

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

	ll k, l, r, p;
	for(int i=0; i<n; i++)
	{
		cin >> k >> l >> r >> p;
		cout << solve(k, l, r, p) << '\n';
	}
	return 0;
}


#include <iostream>
using namespace std;
#define ll long long

ll exp(ll A, ll B, ll N)
{
	ll result=1;
	ll temp=A;
	while(B)
	{
		if(B&1)
		{
			result*=temp;
			result%=N;
		}
		temp*=temp;
		temp%=N;
		B>>=1;
	}
	return result;
}

ll exp2exp(ll K, ll U, ll P) // returns k^(2^U) mod P
{
	ll e=exp(2,U,P-1);
	return exp(K,e,P);
}

ll inverse(ll k, ll p)
{
	return exp(k, p-2, p);
}

ll solve(ll k, ll l, ll r, ll p)
{
	if(k==1)
	{
		return 2%p;
	}
	if(p==2)
	{
		if(k%2==0) return 1;
		else return 0;
	}
	if(k%2==0)
	{
		// solve equation k^(2^(r+1))-1=X*(k^(2^l)-1) mod P
		ll LHS=exp2exp(k,r+1,p)-1;
		ll RHS=exp2exp(k,l,p)-1;

		if(RHS%p==0) // trivial case, right side is 0 modulo p
		{
			return exp(2,r-l+1,p);
		}
		else
		{
			return (LHS*inverse(RHS,p) )%p;
		}
	}
	else
	{
		// solve equation k^(2^(r+1))-1=X*(k^(2^l)-1) mod P
		ll LHS=exp2exp(k,r+1,p)-1;
		ll RHS=exp2exp(k,l,p)-1;

		if(RHS%p==0) // trivial case, right side is 0 modulo p
		{
			return (exp(2,r-l+1,p)*exp(inverse(2,p),r-l,p) )%p;
		}
		else
		{
			return (LHS*inverse(RHS,p)*exp(inverse(2,p),r-l,p) )%p;
		}
	}
}

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

	ll k, l, r, p;
	for(int i=0; i<n; i++)
	{
		cin >> k >> l >> r >> p;
		cout << solve(k, l, r, p) << '\n';
	}
	return 0;
}


#include <iostream>
using namespace std;
#define ll long long

ll exp(ll A, ll B, ll N)
{
	ll result=1;
	ll temp=A;
	while(B)
	{
		if(B&1)
		{
			result*=temp;
			result%=N;
		}
		temp*=temp;
		temp%=N;
		B>>=1;
	}
	return result;
}

ll exp2exp(ll K, ll U, ll P) // returns k^(2^U) mod P
{
	ll e=exp(2,U,P-1);
	return exp(K,e,P);
}

ll inverse(ll k, ll p)
{
	return exp(k, p-2, p);
}

ll solve(ll k, ll l, ll r, ll p)
{
	if(k==1)
	{
		return 2;
	}
	if(p==2)
	{
		if(k%2==0) return 1;
		else return 0;
	}
	// solve equation k^(2^(r+1))-1=X*(k^(2^l)-1) mod P
	ll LHS=exp2exp(k,r+1,p)-1;
	ll RHS=exp2exp(k,l,p)-1;

	if(RHS%p==0) // trivial case, right side is 0 modulo p
	{
		return exp(2,r-l+1,p);
	}
	else
	{
		return LHS*inverse(RHS,p);
	}
}

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

	ll k, l, r, p;
	for(int i=0; i<n; i++)
	{
		cin >> k >> l >> r >> p;
		cout << solve(k, l, r, p) << '\n';
	}
	return 0;
}


#include <iostream>
using namespace std;
#define ll long long

ll exp(ll A, ll B, ll N)
{
	ll result=1;
	ll temp=A;
	while(B)
	{
		if(B&1)
		{
			result*=temp;
			result%=N;
		}
		temp*=temp;
		temp%=N;
		B>>=1;
	}
	return result;
}

ll exp2exp(ll K, ll U, ll P) // returns k^(2^U) mod P
{
	ll e=exp(2,U,P-1);
	return exp(K,e,P);
}

ll inverse(ll k, ll p)
{
	return exp(k, p-2, p);
}

ll solve(ll k, ll l, ll r, ll p)
{
	if(k==1)
	{
		return 2%p;
	}
	if(p==2)
	{
		if(k%2==0) return 1;
		else return 0;
	}
	if(k%2==0)
	{
		// solve equation k^(2^(r+1))-1=X*(k^(2^l)-1) mod P
		ll LHS=exp2exp(k,r+1,p)-1;
		ll RHS=exp2exp(k,l,p)-1;

		if(RHS%p==0) // trivial case, right side is 0 modulo p
		{
			return exp(2,r-l+1,p);
		}
		else
		{
			return LHS*inverse(RHS,p);
		}
	}
	else
	{
		// solve equation k^(2^(r+1))-1=X*(k^(2^l)-1) mod P
		ll LHS=exp2exp(k,r+1,p)-1;
		ll RHS=exp2exp(k,l,p)-1;

		if(RHS%p==0) // trivial case, right side is 0 modulo p
		{
			return exp(2,r-l+1,p)*exp(inverse(2,p),r-l+1,p);
		}
		else
		{
			return LHS*inverse(RHS,p)*exp(inverse(2,p),r-l+1,p);
		}
	}
}

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

	ll k, l, r, p;
	for(int i=0; i<n; i++)
	{
		cin >> k >> l >> r >> p;
		cout << solve(k, l, r, p) << '\n';
	}
	return 0;
}


#include <iostream>
using namespace std;
#define ll long long

ll exp(ll A, ll B, ll N)
{
	ll result=1;
	ll temp=A;
	while(B)
	{
		if(B&1)
		{
			result*=temp;
			result%=N;
		}
		temp*=temp;
		temp%=N;
		B>>=1;
	}
	return result;
}

ll exp2exp(ll K, ll U, ll P) // returns k^(2^U) mod P
{
	ll e=exp(2,U,P-1);
	return exp(K,e,P);
}

ll inverse(ll k, ll p)
{
	return exp(k, p-2, p);
}

ll solve(ll k, ll l, ll r, ll p)
{
	if(k==1)
	{
		return 2%p;
	}
	if(p==2)
	{
		if(k%2==0) return 1;
		else return 0;
	}
	if(k%2==0)
	{
		// solve equation k^(2^(r+1))-1=X*(k^(2^l)-1) mod P
		ll LHS=exp2exp(k,r+1,p)-1;
		ll RHS=exp2exp(k,l,p)-1;

		if(RHS%p==0) // trivial case, right side is 0 modulo p
		{
			return exp(2,r-l+1,p);
		}
		else
		{
			return LHS*inverse(RHS,p);
		}
	}
	else
	{
		// solve equation k^(2^(r+1))-1=X*(k^(2^l)-1) mod P
		ll LHS=exp2exp(k,r+1,p)-1;
		ll RHS=exp2exp(k,l,p)-1;

		if(RHS%p==0) // trivial case, right side is 0 modulo p
		{
			return exp(2,r-l+1,p)*exp(inverse(2,p),r-l,p);
		}
		else
		{
			return LHS*inverse(RHS,p)*exp(inverse(2,p),r-l,p);
		}
	}
}

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

	ll k, l, r, p;
	for(int i=0; i<n; i++)
	{
		cin >> k >> l >> r >> p;
		cout << solve(k, l, r, p) << '\n';
	}
	return 0;
}


#include <iostream>
using namespace std;
#define ll long long

ll exp(ll A, ll B, ll N)
{
	ll result=1;
	ll temp=A;
	while(B)
	{
		if(B&1)
		{
			result*=temp;
			result%=N;
		}
		temp*=temp;
		temp%=N;
		B>>=1;
	}
	return result;
}

ll exp2exp(ll K, ll U, ll P) // returns k^(2^U) mod P
{
	ll e=exp(2,U,P-1);
	return exp(K,e,P);
}

ll inverse(ll k, ll p)
{
	return exp(k, p-2, p);
}

ll solve(ll k, ll l, ll r, ll p)
{
	if(k==1)
	{
		return 2%p;
	}
	if(p==2)
	{
		if(k%2==0) return 1;
		else return 0;
	}
	// solve equation k^(2^(r+1))-1=X*(k^(2^l)-1) mod P
	ll LHS=exp2exp(k,r+1,p)-1;
	ll RHS=exp2exp(k,l,p)-1;

	if(k%p==1) return 1;

	if(RHS%p==0) // trivial case, right side is 0 modulo p
	{
		return exp(2,r-l+1,p);
	}
	else
	{
		return LHS*inverse(RHS,p);
	}
}

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

	ll k, l, r, p;
	for(int i=0; i<n; i++)
	{
		cin >> k >> l >> r >> p;
		cout << solve(k, l, r, p) << '\n';
	}
	return 0;
}


#include <iostream>
using namespace std;
#define ll long long

ll exp(ll A, ll B, ll N)
{
	ll result=1;
	ll temp=A;
	while(B)
	{
		if(B&1)
		{
			result*=temp;
			result%=N;
		}
		temp*=temp;
		temp%=N;
		B>>=1;
	}
	return result;
}

ll exp2exp(ll K, ll U, ll P) // returns k^(2^U) mod P
{
	ll e=exp(2,U,P-1);
	return exp(K,e,P);
}

ll inverse(ll k, ll p)
{
	return exp(k, p-2, p);
}

ll solve(ll k, ll l, ll r, ll p)
{
	if(k==1)
	{
		return 2%p;
	}
	if(p==2)
	{
		if(k%2==0) return 1;
		else return 0;
	}

	if(k%2==0)
	{
		if(k%p==0)
		{
			return 1;
		}
		// solve equation k^(2^(r+1))-1=X*(k^(2^l)-1) mod P
		ll LHS=exp2exp(k,r+1,p)-1;
		ll RHS=exp2exp(k,l,p)-1;

		if(RHS%p==0) // trivial case, right side is 0 modulo p
		{
			return exp(2,r-l+1,p);
		}
		else
		{
			return (LHS*inverse(RHS,p) )%p;
		}
	}
	else
	{
		// solve equation k^(2^(r+1))-1=X*(k^(2^l)-1) mod P
		ll LHS=exp2exp(k,r+1,p)-1;
		ll RHS=exp2exp(k,l,p)-1;
		if(k%p==0)
		{
			return exp(inverse(2, p), r-l, p);
		}

		if(RHS%p==0) // trivial case, right side is 0 modulo p
		{
			return (exp(2,r-l+1,p)/* *exp(inverse(2,p),r-l,p) */)%p;
		}
		else
		{
			return (LHS*inverse(RHS,p)/* *exp(inverse(2,p),r-l,p) */)%p;
		}
	}
}

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

	ll k, l, r, p;
	for(int i=0; i<n; i++)
	{
		cin >> k >> l >> r >> p;
		cout << solve(k, l, r, p) << '\n';
	}
	return 0;
}


#include <iostream>
using namespace std;
#define ll long long

ll exp(ll A, ll B, ll N)
{
	ll result=1;
	ll temp=A;
	while(B)
	{
		if(B&1)
		{
			result*=temp;
			result%=N;
		}
		temp*=temp;
		temp%=N;
		B>>=1;
	}
	return result;
}

ll exp2exp(ll K, ll U, ll P) // returns k^(2^U) mod P
{
	ll e=exp(2,U,P-1);
	return exp(K,e,P);
}

ll inverse(ll k, ll p)
{
	return exp(k, p-2, p);
}

ll solve(ll k, ll l, ll r, ll p)
{
	if(p==2) return 1;
	// solve equation k^(2^(r+1))-1=X*(k^(2^l)-1) mod P
	ll LHS=exp2exp(k,r+1,p)-1;
	ll RHS=exp2exp(k,l,p)-1;

	if(RHS%p==0) // trivial case, right side is 0 modulo p
	{
		return exp(2,r-l+1,p);
	}
	else
	{
		return LHS*inverse(RHS,p);
	}
}

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

	ll k, l, r, p;
	for(int i=0; i<n; i++)
	{
		cin >> k >> l >> r >> p;
		cout << solve(k, l, r, p) << '\n';
	}
	return 0;
}


#include <iostream>
using namespace std;
#define ll long long

ll exp(ll A, ll B, ll N)
{
	ll result=1;
	ll temp=A;
	while(B)
	{
		if(B&1)
		{
			result*=temp;
			result%=N;
		}
		temp*=temp;
		temp%=N;
		B>>=1;
	}
	return result;
}

ll exp2exp(ll K, ll U, ll P) // returns k^(2^U) mod P
{
	ll e=exp(2,U,P-1);
	return exp(K,e,P);
}

ll inverse(ll k, ll p)
{
	return exp(k, p-2, p);
}

ll solve(ll k, ll l, ll r, ll p)
{
	// solve equation k^(2^(r+1))-1=X*(k^(2^l)-1) mod P
	ll LHS=exp2exp(k,r+1,p)-1;
	ll RHS=exp2exp(k,l,p)-1;

	if(RHS%p==0) // trivial case, left side becomes 0
	{
		return exp(2,r-l+1,p);
	}
	else
	{
		return LHS*inverse(RHS,p);
	}
}

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

	ll k, l, r, p;
	for(int i=0; i<n; i++)
	{
		cin >> k >> l >> r >> p;
		cout << solve(k, l, r, p) << '\n';
	}
	return 0;
}


#include <iostream>
using namespace std;
#define ll long long

ll exp(ll A, ll B, ll N)
{
	ll result=1;
	ll temp=A;
	while(B)
	{
		if(B&1)
		{
			result*=temp;
			result%=N;
		}
		temp*=temp;
		temp%=N;
		B>>=1;
	}
	return result;
}

ll exp2exp(ll K, ll U, ll P) // returns k^(2^U) mod P
{
	ll e=exp(2,U,P-1);
	return exp(K,e,P);
}

ll inverse(ll k, ll p)
{
	return exp(k, p-2, p);
}

ll solve(ll k, ll l, ll r, ll p)
{
	if(k==1)
	{
		return 2%p;
	}
	if(p==2)
	{
		if(k%2==0) return 1;
		else return 0;
	}
	if(k%2==0)
	{
		// solve equation k^(2^(r+1))-1=X*(k^(2^l)-1) mod P
		ll LHS=exp2exp(k,r+1,p)-1;
		ll RHS=exp2exp(k,l,p)-1;

		if(RHS%p==0) // trivial case, right side is 0 modulo p
		{
			return exp(2,r-l+1,p);
		}
		else
		{
			return LHS*inverse(RHS,p);
		}
	}
	else
	{
		// solve equation k^(2^(r+1))-1=X*(k^(2^l)-1) mod P
		ll LHS=exp2exp(k,r+1,p)-1;
		ll RHS=exp2exp(k,l,p)-1;

		if(RHS%p==0) // trivial case, right side is 0 modulo p
		{
			return (exp(2,r-l+1,p)*exp(inverse(2,p),r-l,p) )%p;
		}
		else
		{
			return (LHS*inverse(RHS,p)*exp(inverse(2,p),r-l,p) )%p;
		}
	}
}

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

	ll k, l, r, p;
	for(int i=0; i<n; i++)
	{
		cin >> k >> l >> r >> p;
		cout << solve(k, l, r, p) << '\n';
	}
	return 0;
}


#include <iostream>
using namespace std;
#define ll long long

ll exp(ll A, ll B, ll N)
{
	ll result=1;
	ll temp=A;
	while(B)
	{
		if(B&1)
		{
			result*=temp;
			result%=N;
		}
		temp*=temp;
		temp%=N;
		B>>=1;
	}
	return result;
}

ll exp2exp(ll K, ll U, ll P) // returns k^(2^U) mod P
{
	ll e=exp(2,U,P-1);
	return exp(K,e,P);
}

ll inverse(ll k, ll p)
{
	return exp(k, p-2, p);
}

ll solve(ll k, ll l, ll r, ll p)
{
	if(k==1)
	{
		return 2%p;
	}
	if(p==2)
	{
		if(k%2==0) return 1;
		else return 0;
	}

	if(k%2==0)
	{
		if(k%p==0)
		{
			return 1;
		}
		// solve equation k^(2^(r+1))-1=X*(k^(2^l)-1) mod P
		ll LHS=exp2exp(k,r+1,p)-1;
		ll RHS=exp2exp(k,l,p)-1;

		if(RHS%p==0) // trivial case, right side is 0 modulo p
		{
			return exp(2,r-l+1,p);
		}
		else
		{
			return (LHS*inverse(RHS,p) )%p;
		}
	}
	else
	{
		// solve equation k^(2^(r+1))-1=X*(k^(2^l)-1) mod P
		ll LHS=exp2exp(k,r+1,p)-1;
		ll RHS=exp2exp(k,l,p)-1;
		if(k%p==0)
		{
			return exp(inverse(2, p), r-l, p);
		}

		if(RHS%p==0) // trivial case, right side is 0 modulo p
		{
			return (exp(2,r-l+1,p)*exp(inverse(2,p),r-l,p) )%p;
		}
		else
		{
			return (LHS*inverse(RHS,p)/* *exp(inverse(2,p),r-l,p) */)%p;
		}
	}
}

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

	ll k, l, r, p;
	for(int i=0; i<n; i++)
	{
		cin >> k >> l >> r >> p;
		cout << solve(k, l, r, p) << '\n';
	}
	return 0;
}


#include <iostream>
using namespace std;
#define ll long long

ll exp(ll A, ll B, ll N)
{
	ll result=1;
	ll temp=A;
	while(B)
	{
		if(B&1)
		{
			result*=temp;
			result%=N;
		}
		temp*=temp;
		temp%=N;
		B>>=1;
	}
	return result;
}

ll exp2exp(ll K, ll U, ll P) // returns k^(2^U) mod P
{
	ll e=exp(2,U,P-1);
	return exp(K,e,P);
}

ll inverse(ll k, ll p)
{
	return exp(k, p-2, p);
}

ll solve(ll k, ll l, ll r, ll p)
{
	if(k==1)
	{
		return 2%p;
	}
	if(p==2)
	{
		if(k%2==0) return 1;
		else return 0;
	}

	if(k%2==0)
	{
		if(k%p==0)
		{
			return 1;
		}
		// solve equation k^(2^(r+1))-1=X*(k^(2^l)-1) mod P
		ll LHS=exp2exp(k,r+1,p)-1;
		ll RHS=exp2exp(k,l,p)-1;

		if(RHS%p==0) // trivial case, right side is 0 modulo p
		{
			return exp(2,r-l+1,p);
		}
		else
		{
			return (LHS*inverse(RHS,p) )%p;
		}
	}
	else
	{
		// solve equation k^(2^(r+1))-1=X*(k^(2^l)-1) mod P
		ll LHS=exp2exp(k,r+1,p)-1;
		ll RHS=exp2exp(k,l,p)-1;
		if(k%p==0)
		{
			return exp(inverse(2, p), r-l, p);
		}

		if(RHS%p==0) // trivial case, right side is 0 modulo p
		{
			return (exp(2,r-l+1,p)*exp(inverse(2,p),r-l,p) )%p;
		}
		else
		{
			return (LHS*inverse(RHS,p)*exp(inverse(2,p),r-l,p) )%p;
		}
	}
}

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

	ll k, l, r, p;
	for(int i=0; i<n; i++)
	{
		cin >> k >> l >> r >> p;
		cout << solve(k, l, r, p) << '\n';
	}
	return 0;
}


#include <iostream>
using namespace std;
#define ll long long

ll exp(ll A, ll B, ll N)
{
	ll result=1;
	ll temp=A;
	while(B)
	{
		if(B&1)
		{
			result*=temp;
			result%=N;
		}
		temp*=temp;
		temp%=N;
		B>>=1;
	}
	return result;
}

ll exp2exp(ll K, ll U, ll P) // returns k^(2^U) mod P
{
	ll e=exp(2,U,P-1);
	return exp(K,e,P);
}

ll inverse(ll k, ll p)
{
	return exp(k, p-2, p);
}

ll solve(ll k, ll l, ll r, ll p)
{
	if(k==1)
	{
		return 2%p;
	}
	if(p==2)
	{
		if(k%2==0) return 1;
		else return 0;
	}
	if(k%2==0)
	{
		// solve equation k^(2^(r+1))-1=X*(k^(2^l)-1) mod P
		ll LHS=exp2exp(k,r+1,p)-1;
		ll RHS=exp2exp(k,l,p)-1;

		if(RHS%p==0) // trivial case, right side is 0 modulo p
		{
			return exp(2,r-l+1,p);
		}
		else
		{
			return LHS*inverse(RHS,p);
		}
	}
	else
	{
		// solve equation k^(2^(r+1))-1=X*(k^(2^l)-1) mod P
		ll LHS=exp2exp(k,r+1,p)-1;
		ll RHS=exp2exp(k,l,p)-1;

		if(RHS%p==0) // trivial case, right side is 0 modulo p
		{
			return exp(2,r-l+1,p)*exp(inverse(2,p),r-l-1,p);
		}
		else
		{
			return LHS*inverse(RHS,p)*exp(inverse(2,p),r-l-1,p);
		}
	}
}

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

	ll k, l, r, p;
	for(int i=0; i<n; i++)
	{
		cin >> k >> l >> r >> p;
		cout << solve(k, l, r, p) << '\n';
	}
	return 0;
}


#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
#include<set>
#include<assert.h>
using namespace std;
#define FOR(i,a,b) for(int i = a; i <= b; ++i)
#define FORD(i,a,b) for(int i = a; i >= b; --i)
#define REP(i,n) FOR(i,0,(n)-1)
#define RI(i,n) FOR(i,1,n)
#define pb push_back
#define mp make_pair
#define st first
#define nd second
#define mini(a,b) a=min(a,b)
#define maxi(a,b) a=max(a,b)
bool debug;
typedef vector<int> vi;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
const int inf = 1e9 + 5;
const int nax = 1e6 + 5;

int test;
ll k,l,r,p;

ll pot(ll a, ll b, ll mod) {
	ll res = 1;
	while (b) {
		if (b&1) res = (res*a)%mod;
		a = (a*a)%mod;
		b /= 2;
	}
	return res;
}

int main(int argc, char * argv[]) {
	debug = argc > 1;
	scanf("%d",&test);
	while (test--) {
		scanf("%lld%lld%lld%lld",&k,&l,&r,&p);
		if (p == 2) {
			puts((k&1) ? "0" : "1");
		}
		else {
			ll x = pot(k, pot(2, l, p-1), p);
			if (x == 1)
				printf("%lld\n",pot(2,r-l+1,p));
			else {
				ll g = (pot(x, pot(2, r-l+2, p-1), p) - 1 + p)%p;
				g = (g*pot(x-1, p-2, p))%p;

				if (k&1)
					g = (g*pot(2,r-l,p));

				printf("%lld\n",g);
			}
		}
	}
	return 0;
}


#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
#include<set>
#include<assert.h>
using namespace std;
#define FOR(i,a,b) for(int i = a; i <= b; ++i)
#define FORD(i,a,b) for(int i = a; i >= b; --i)
#define REP(i,n) FOR(i,0,(n)-1)
#define RI(i,n) FOR(i,1,n)
#define pb push_back
#define mp make_pair
#define st first
#define nd second
#define mini(a,b) a=min(a,b)
#define maxi(a,b) a=max(a,b)
bool debug;
typedef vector<int> vi;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
const int inf = 1e9 + 5;
const int nax = 1e6 + 5;

int test;
ll k,l,r,p;

ll pot(ll a, ll b, ll mod) {
	ll res = 1;
	while (b) {
		if (b&1) res = (res*a)%mod;
		a = (a*a)%mod;
		b /= 2;
	}
	return res;
}

int main(int argc, char * argv[]) {
	debug = argc > 1;
	scanf("%d",&test);
	while (test--) {
		scanf("%lld%lld%lld%lld",&k,&l,&r,&p);
		if (p == 2) {
			puts((k&1) ? "0" : "1");
		}
		else {
			ll x;
			if (k%p == 0)
				x = 0;
			else
				x = pot(k%p, pot(2%(p-1), l, p-1), p);

			ll g;
			if (x == 1)
				g = pot(2,r-l+1,p);
			else {
				if (x == 0)
					g = p-1;
				else
					g = (pot(x, pot(2%(p-1), r-l+2, p-1), p) - 1 + p)%p;

				g = (g*pot((x-1+p)%p, p-2, p))%p;
			}

			if (k&1)
				g = (g*pot(pot(2,r-l,p)%p,p-2,p))%p;

			printf("%lld\n",g);
		}
	}
	return 0;
}


#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
#include<set>
#include<assert.h>
using namespace std;
#define FOR(i,a,b) for(int i = a; i <= b; ++i)
#define FORD(i,a,b) for(int i = a; i >= b; --i)
#define REP(i,n) FOR(i,0,(n)-1)
#define RI(i,n) FOR(i,1,n)
#define pb push_back
#define mp make_pair
#define st first
#define nd second
#define mini(a,b) a=min(a,b)
#define maxi(a,b) a=max(a,b)
bool debug;
typedef vector<int> vi;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
const int inf = 1e9 + 5;
const int nax = 1e6 + 5;

int test;
ll k,l,r,p;

ll pot(ll a, ll b, ll mod) {
	ll res = 1;
	while (b) {
		if (b&1) res = (res*a)%mod;
		a = (a*a)%mod;
		b /= 2;
	}
	return res;
}

int main(int argc, char * argv[]) {
	debug = argc > 1;
	scanf("%d",&test);
	while (test--) {
		scanf("%lld%lld%lld%lld",&k,&l,&r,&p);
		if (p == 2) {
			puts((k&1) ? "0" : "1");
		}
		else {
			ll x;
			if (k%p == 0)
				x = 0;
			else
				x = pot(k%p, pot(2%(p-1), l, p-1), p);

			ll g;
			if (x == 1)
				g = pot(2,r-l+1,p);
			else {
				if (x == 0)
					g = p-1;
				else
					g = (pot(x, pot(2%(p-1), r-l+1, p-1), p) - 1 + p)%p;

				g = (g*pot((x-1+p)%p, p-2, p))%p;
			}

			if (k&1)
				g = (g*pot(pot(2,r-l,p)%p,p-2,p))%p;

			printf("%lld\n",g);
		}
	}
	return 0;
}


#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
#include<set>
#include<assert.h>
using namespace std;
#define FOR(i,a,b) for(int i = a; i <= b; ++i)
#define FORD(i,a,b) for(int i = a; i >= b; --i)
#define REP(i,n) FOR(i,0,(n)-1)
#define RI(i,n) FOR(i,1,n)
#define pb push_back
#define mp make_pair
#define st first
#define nd second
#define mini(a,b) a=min(a,b)
#define maxi(a,b) a=max(a,b)
bool debug;
typedef vector<int> vi;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
const int inf = 1e9 + 5;
const int nax = 1e6 + 5;

int test;
ll k,l,r,p;

ll pot(ll a, ll b, ll mod) {
	ll res = 1;
	while (b) {
		if (b&1) res = (res*a)%mod;
		a = (a*a)%mod;
		b /= 2;
	}
	return res;
}

int main(int argc, char * argv[]) {
	debug = argc > 1;
	scanf("%d",&test);
	while (test--) {
		scanf("%lld%lld%lld%lld",&k,&l,&r,&p);
		if (p == 2) {
			puts((k&1) ? "0" : "1");
		}
		else {
			ll x = pot(k, pot(2, l, p-1), p);
			ll g;
			if (x == 1)
				g = pot(2,r-l+1,p);
			else {
				g = (pot(x, pot(2, r-l+2, p-1), p) - 1 + p)%p;
				g = (g*pot(x-1, p-2, p))%p;
			}

			if (k&1)
				g = (g*pot(2,r-l,p));

				printf("%lld\n",g);
		}
	}
	return 0;
}


#include <bits/stdc++.h>

#define FO(i,a,b) for (int i = (a); i < (b); i++)
#define sz(v) int(v.size())

using namespace std;

typedef long long ll;

ll add(ll a, ll b, ll p) {
    return (p+(a+b)%p)%p;
}

ll mul(ll a, ll b, ll p) {
    return (a*b)%p;
}

ll pw(ll a, ll b, ll p) {
    ll r = 1;
    while (b) {
        if (b&1) r = mul(r,a,p);
        a = mul(a,a,p);
        b /= 2;
    }
    return r;
}

ll ppw(ll a, ll b, ll c, ll p) {
    if (a == 0) return 0;
    return pw(a, pw(b,c,p-1), p);
}

int main() {
    int t; scanf("%d", &t);
    FO(Z,0,t) {
        ll x, l, r, p; scanf("%lld %lld %lld %lld", &x, &l, &r, &p);
        if (p == 2) {
            printf("%lld\n", (x+1)%2);
            continue;
        }
        bool odd = x%2;
        x = ppw(x,2,l,p); r -= l;
        ll m;
        if (x == 1) {
            m = pw(2,r+1,p);
        } else {
            m = mul(add(ppw(x,2,r+1,p),-1,p),pw(x-1,p-2,p),p);
        }
        if (odd) m = mul(m, pw(pw(2,r,p),p-2,p), p);
        printf("%lld\n", m);
    }
}


#include <bits/stdc++.h>

#define FO(i,a,b) for (int i = (a); i < (b); i++)
#define sz(v) int(v.size())

using namespace std;

typedef long long ll;

ll add(ll a, ll b, ll p) {
    return (p+(a+b)%p)%p;
}

ll mul(ll a, ll b, ll p) {
    return (a*b)%p;
}

ll pw(ll a, ll b, ll p) {
    ll r = 1;
    while (b) {
        if (b&1) r = mul(r,a,p);
        a = mul(a,a,p);
        b /= 2;
    }
    return r;
}

ll ppw(ll a, ll b, ll c, ll p) {
    if (a%p == 0) return 0;
    return pw(a, pw(b,c,p-1), p);
}

int main() {
    int t; scanf("%d", &t);
    FO(Z,0,t) {
        ll x, l, r, p; scanf("%lld %lld %lld %lld", &x, &l, &r, &p);
        if (p == 2) {
            printf("%lld\n", (x+1)%2);
            continue;
        }
        bool odd = x%2;
        x = ppw(x,2,l,p); r -= l;
        ll m;
        if (x == 0) {
            m = 1;
        } else if (x == 1) {
            m = pw(2,r+1,p);
        } else {
            m = mul(add(ppw(x,2,r+1,p),-1,p),pw(x-1,p-2,p),p);
        }
        if (odd) m = mul(m, pw(pw(2,r,p),p-2,p), p);
        printf("%lld\n", m);
    }
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <cassert>
#include <initializer_list>
#include <unordered_map>
#include <unordered_set>

using namespace std;

#ifndef ONLINE_JUDGE
#define DEBUG
#endif

#define oo 0x3F3F3F3F
#ifdef DEBUG
#define cvar(x) cerr << "<" << #x << ": " << x << ">"
#define evar(x) cvar (x) << endl
template<class T> void DISP (const char *s, T x, int n) {cerr << "[" << s << ": "; for (int i = 0; i < n; ++i) cerr << x[i] << " "; cerr << "]" << endl;}
#define disp(x,n) DISP (#x " to " #n, x, n)
#else
#define cvar(...) ({})
#define evar(...) ({})
#define disp(...) ({})
#endif
#define MKP make_pair
#define Px first
#define Py second
#define PB push_back
#define SZ(x) (int)((x).size ())
#define ALL(x) (x).begin (), (x).end ()

typedef unsigned int uint;
typedef long long int64;
typedef unsigned long long uint64;
typedef long double real;
typedef unordered_map<int, int> UMII;
typedef unordered_set<int> USI;

int64 fpm (int64 b, int64 e, int64 m) { int64 t = 1; for (; e; e >>= 1, b = b * b % m) e & 1 ? t = t * b % m : 0; return t; }
template<class T> inline bool chkmin (T &a, T b) {return a > b ? a = b, true : false;}
template<class T> inline bool chkmax (T &a, T b) {return a < b ? a = b, true : false;}
template<class T> inline T sqr (T x) {return x * x;}

template<class edge> struct Graph
{
  vector<vector<edge> > adj;
  Graph (int n) {adj.clear (); adj.resize (n + 5);}
  Graph () {adj.clear (); }
  void resize (int n) {adj.resize (n + 5); }
  void add (int s, edge e){adj[s].push_back (e);}
  void del (int s, edge e) {adj[s].erase (find (iter (adj[s]), e)); }
  int deg (int v) {return adj[v].size (); }
  vector<edge>& operator [](int t) {return adj[t];}
};

pair<int64, int64> calc (int k, int64 R, int p)
{
  cerr << "calc: " << k << " " << R << " " << p << endl;
  int w = 0;
  for (; 2 << w < p; ++w);
  if (((p - 1) & (p - 2)) || w > R)
    return MKP (0, fpm (k, fpm (2, R, p - 1), p) - 1);

  int64 a = k / p % p, b = k % p;
  for (; w--; )
    {
      int64 na = a * b * 2;
      int64 nb = b * b;
      a = (na + nb / p) % p;
      b = nb % p;
    }
  // assert (b == 1);
  // cerr << a << " " << b << endl;
  return MKP (a * fpm (2, R - w, p) % p, 0);
}

int main ()
{
#ifndef ONLINE_JUDGE
  freopen ("185D.in" , "r", stdin);
  freopen ("185D.out", "w", stdout);
#endif
  ios_base::sync_with_stdio (false);

  int tc, k, p; int64 L, R;

  cin >> tc;
  for (; tc--; )
    {
      cin >> k >> L >> R >> p;
      if (k % p == 0)
        {
          if ((k & 1) && p != 2)
            cout << fpm (2, p - 2, p) << endl;
          else
            cout << 1 << endl;
        }
      else if (k == 1)
        cout << 2 % p << endl;
      else
        {
          pair<int64, int64> ansR = calc (k, R + 1, p), ansL = calc (k, L, p);
          int64 ans;
          if (ansL.Py)
            ans = ansR.Py * fpm (ansL.Py, p - 2, p) % p;
          else
            ans = ansR.Px * fpm (ansL.Px, p - 2, p) % p;
          if ((k & 1) && p != 2)
            ans = ans * fpm (2, (R - L) % (p - 1) * (p - 2) % (p - 1), p), cerr << (R - L) % (p - 1) * (p - 2) % (p - 1) << endl;
          cout << ans << endl;
        }
    }

  return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <cassert>
#include <initializer_list>
#include <unordered_map>
#include <unordered_set>

using namespace std;

#ifndef ONLINE_JUDGE
#define DEBUG
#endif

#define oo 0x3F3F3F3F
#ifdef DEBUG
#define cvar(x) cerr << "<" << #x << ": " << x << ">"
#define evar(x) cvar (x) << endl
template<class T> void DISP (const char *s, T x, int n) {cerr << "[" << s << ": "; for (int i = 0; i < n; ++i) cerr << x[i] << " "; cerr << "]" << endl;}
#define disp(x,n) DISP (#x " to " #n, x, n)
#else
#define cvar(...) ({})
#define evar(...) ({})
#define disp(...) ({})
#endif
#define MKP make_pair
#define Px first
#define Py second
#define PB push_back
#define SZ(x) (int)((x).size ())
#define ALL(x) (x).begin (), (x).end ()

typedef unsigned int uint;
typedef long long int64;
typedef unsigned long long uint64;
typedef long double real;
typedef unordered_map<int, int> UMII;
typedef unordered_set<int> USI;

int64 fpm (int64 b, int64 e, int64 m) { int64 t = 1; for (; e; e >>= 1, b = b * b % m) e & 1 ? t = t * b % m : 0; return t; }
template<class T> inline bool chkmin (T &a, T b) {return a > b ? a = b, true : false;}
template<class T> inline bool chkmax (T &a, T b) {return a < b ? a = b, true : false;}
template<class T> inline T sqr (T x) {return x * x;}

template<class edge> struct Graph
{
  vector<vector<edge> > adj;
  Graph (int n) {adj.clear (); adj.resize (n + 5);}
  Graph () {adj.clear (); }
  void resize (int n) {adj.resize (n + 5); }
  void add (int s, edge e){adj[s].push_back (e);}
  void del (int s, edge e) {adj[s].erase (find (iter (adj[s]), e)); }
  int deg (int v) {return adj[v].size (); }
  vector<edge>& operator [](int t) {return adj[t];}
};

pair<int64, int64> calc (int k, int64 R, int p)
{
  cerr << "calc: " << k << " " << R << " " << p << endl;
  int w = 0;
  for (; 2 << w < p; ++w);
  if (((p - 1) & (p - 2)) || w > R)
    return MKP (0, fpm (k, fpm (2, R, p - 1), p) - 1);

  int64 a = k / p % p, b = k % p;
  for (; w--; )
    {
      int64 na = a * b * 2;
      int64 nb = b * b;
      a = (na + nb / p) % p;
      b = nb % p;
    }
  // assert (b == 1);
  // cerr << a << " " << b << endl;
  return MKP (a * fpm (2, R - w, p) % p, 0);
}

int main ()
{
#ifndef ONLINE_JUDGE
  freopen ("185D.in" , "r", stdin);
  freopen ("185D.out", "w", stdout);
#endif
  ios_base::sync_with_stdio (false);

  int tc, k, p; int64 L, R;

  cin >> tc;
  for (; tc--; )
    {
      cin >> k >> L >> R >> p;
      if (k % p == 0)
        cout << 1 << endl;
      else if (k == 1)
        cout << 2 << endl;
      else
        {
          pair<int64, int64> ansR = calc (k, R + 1, p), ansL = calc (k, L, p);
          if (ansL.Py)
            cout << ansR.Py * fpm (ansL.Py, p - 2, p) % p << endl;
          else
            cout << ansR.Px * fpm (ansL.Px, p - 2, p) % p << endl;
        }
    }

  return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <cassert>
#include <initializer_list>
#include <unordered_map>
#include <unordered_set>

using namespace std;

#ifndef ONLINE_JUDGE
#define DEBUG
#endif

#define oo 0x3F3F3F3F
#ifdef DEBUG
#define cvar(x) cerr << "<" << #x << ": " << x << ">"
#define evar(x) cvar (x) << endl
template<class T> void DISP (const char *s, T x, int n) {cerr << "[" << s << ": "; for (int i = 0; i < n; ++i) cerr << x[i] << " "; cerr << "]" << endl;}
#define disp(x,n) DISP (#x " to " #n, x, n)
#else
#define cvar(...) ({})
#define evar(...) ({})
#define disp(...) ({})
#endif
#define MKP make_pair
#define Px first
#define Py second
#define PB push_back
#define SZ(x) (int)((x).size ())
#define ALL(x) (x).begin (), (x).end ()

typedef unsigned int uint;
typedef long long int64;
typedef unsigned long long uint64;
typedef long double real;
typedef unordered_map<int, int> UMII;
typedef unordered_set<int> USI;

int64 fpm (int64 b, int64 e, int64 m) { int64 t = 1; for (; e; e >>= 1, b = b * b % m) e & 1 ? t = t * b % m : 0; return t; }
template<class T> inline bool chkmin (T &a, T b) {return a > b ? a = b, true : false;}
template<class T> inline bool chkmax (T &a, T b) {return a < b ? a = b, true : false;}
template<class T> inline T sqr (T x) {return x * x;}

template<class edge> struct Graph
{
  vector<vector<edge> > adj;
  Graph (int n) {adj.clear (); adj.resize (n + 5);}
  Graph () {adj.clear (); }
  void resize (int n) {adj.resize (n + 5); }
  void add (int s, edge e){adj[s].push_back (e);}
  void del (int s, edge e) {adj[s].erase (find (iter (adj[s]), e)); }
  int deg (int v) {return adj[v].size (); }
  vector<edge>& operator [](int t) {return adj[t];}
};

pair<int64, int64> calc (int k, int64 R, int p)
{
  cerr << "calc: " << k << " " << R << " " << p << endl;
  int w = 0;
  for (; 2 << w < p; ++w);
  if (((p - 1) & (p - 2)) || w > R)
    return MKP (0, fpm (k, fpm (2, R, p - 1), p) - 1);

  int64 a = k / p % p, b = k % p;
  for (; w--; )
    {
      int64 na = a * b * 2;
      int64 nb = b * b;
      a = (na + nb / p) % p;
      b = nb % p;
    }
  // assert (b == 1);
  // cerr << a << " " << b << endl;
  return MKP (a * fpm (2, R - w, p) % p, 0);
}

int main ()
{
#ifndef ONLINE_JUDGE
  freopen ("185D.in" , "r", stdin);
  freopen ("185D.out", "w", stdout);
#endif
  ios_base::sync_with_stdio (false);

  int tc, k, p; int64 L, R;

  cin >> tc;
  for (; tc--; )
    {
      cin >> k >> L >> R >> p;
      if (k % p == 0)
        cout << 1 << endl;
      else if (k == 1)
        cout << 2 % p << endl;
      else
        {
          pair<int64, int64> ansR = calc (k, R + 1, p), ansL = calc (k, L, p);
          int64 ans;
          if (ansL.Py)
            ans = ansR.Py * fpm (ansL.Py, p - 2, p) % p;
          else
            ans = ansR.Px * fpm (ansL.Px, p - 2, p) % p;
          if ((k & 1) && p != 2)
            ans = ans * fpm (2, (L - R) % (p - 2) + p - 2, p);
          cout << ans << endl;
        }
    }

  return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <cassert>
#include <initializer_list>
#include <unordered_map>
#include <unordered_set>

using namespace std;

#ifndef ONLINE_JUDGE
#define DEBUG
#endif

#define oo 0x3F3F3F3F
#ifdef DEBUG
#define cvar(x) cerr << "<" << #x << ": " << x << ">"
#define evar(x) cvar (x) << endl
template<class T> void DISP (const char *s, T x, int n) {cerr << "[" << s << ": "; for (int i = 0; i < n; ++i) cerr << x[i] << " "; cerr << "]" << endl;}
#define disp(x,n) DISP (#x " to " #n, x, n)
#else
#define cvar(...) ({})
#define evar(...) ({})
#define disp(...) ({})
#endif
#define MKP make_pair
#define Px first
#define Py second
#define PB push_back
#define SZ(x) (int)((x).size ())
#define ALL(x) (x).begin (), (x).end ()

typedef unsigned int uint;
typedef long long int64;
typedef unsigned long long uint64;
typedef long double real;
typedef unordered_map<int, int> UMII;
typedef unordered_set<int> USI;

int64 fpm (int64 b, int64 e, int64 m) { int64 t = 1; for (; e; e >>= 1, b = b * b % m) e & 1 ? t = t * b % m : 0; return t; }
template<class T> inline bool chkmin (T &a, T b) {return a > b ? a = b, true : false;}
template<class T> inline bool chkmax (T &a, T b) {return a < b ? a = b, true : false;}
template<class T> inline T sqr (T x) {return x * x;}

template<class edge> struct Graph
{
  vector<vector<edge> > adj;
  Graph (int n) {adj.clear (); adj.resize (n + 5);}
  Graph () {adj.clear (); }
  void resize (int n) {adj.resize (n + 5); }
  void add (int s, edge e){adj[s].push_back (e);}
  void del (int s, edge e) {adj[s].erase (find (iter (adj[s]), e)); }
  int deg (int v) {return adj[v].size (); }
  vector<edge>& operator [](int t) {return adj[t];}
};

pair<int64, int64> calc (int k, int64 R, int p)
{
  cerr << "calc: " << k << " " << R << " " << p << endl;
  int w = 0;
  for (; 2 << w < p; ++w);
  if (((p - 1) & (p - 2)) || w > R)
    return MKP (0, fpm (k, fpm (2, R, p - 1), p) - 1);

  int64 a = k / p % p, b = k % p;
  for (; w--; )
    {
      int64 na = a * b * 2;
      int64 nb = b * b;
      a = (na + nb / p) % p;
      b = nb % p;
    }
  // assert (b == 1);
  // cerr << a << " " << b << endl;
  return MKP (a * fpm (2, R - w, p) % p, 0);
}

int main ()
{
#ifndef ONLINE_JUDGE
  freopen ("185D.in" , "r", stdin);
  freopen ("185D.out", "w", stdout);
#endif
  ios_base::sync_with_stdio (false);

  int tc, k, p; int64 L, R;

  cin >> tc;
  for (; tc--; )
    {
      cin >> k >> L >> R >> p;
      if (k % p == 0)
        cout << 1 << endl;
      else if (k == 1)
        cout << 2 % p << endl;
      else
        {
          pair<int64, int64> ansR = calc (k, R + 1, p), ansL = calc (k, L, p);
          if (ansL.Py)
            cout << ansR.Py * fpm (ansL.Py, p - 2, p) % p << endl;
          else
            cout << ansR.Px * fpm (ansL.Px, p - 2, p) % p << endl;
        }
    }

  return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <cassert>
#include <initializer_list>
#include <unordered_map>
#include <unordered_set>

using namespace std;

#ifndef ONLINE_JUDGE
#define DEBUG
#endif

#define oo 0x3F3F3F3F
#ifdef DEBUG
#define cvar(x) cerr << "<" << #x << ": " << x << ">"
#define evar(x) cvar (x) << endl
template<class T> void DISP (const char *s, T x, int n) {cerr << "[" << s << ": "; for (int i = 0; i < n; ++i) cerr << x[i] << " "; cerr << "]" << endl;}
#define disp(x,n) DISP (#x " to " #n, x, n)
#else
#define cvar(...) ({})
#define evar(...) ({})
#define disp(...) ({})
#endif
#define MKP make_pair
#define Px first
#define Py second
#define PB push_back
#define SZ(x) (int)((x).size ())
#define ALL(x) (x).begin (), (x).end ()

typedef unsigned int uint;
typedef long long int64;
typedef unsigned long long uint64;
typedef long double real;
typedef unordered_map<int, int> UMII;
typedef unordered_set<int> USI;

int64 fpm (int64 b, int64 e, int64 m) { int64 t = 1; for (; e; e >>= 1, b = b * b % m) e & 1 ? t = t * b % m : 0; return t; }
template<class T> inline bool chkmin (T &a, T b) {return a > b ? a = b, true : false;}
template<class T> inline bool chkmax (T &a, T b) {return a < b ? a = b, true : false;}
template<class T> inline T sqr (T x) {return x * x;}

template<class edge> struct Graph
{
  vector<vector<edge> > adj;
  Graph (int n) {adj.clear (); adj.resize (n + 5);}
  Graph () {adj.clear (); }
  void resize (int n) {adj.resize (n + 5); }
  void add (int s, edge e){adj[s].push_back (e);}
  void del (int s, edge e) {adj[s].erase (find (iter (adj[s]), e)); }
  int deg (int v) {return adj[v].size (); }
  vector<edge>& operator [](int t) {return adj[t];}
};

pair<int64, int64> calc (int k, int64 R, int p)
{
  cerr << "calc: " << k << " " << R << " " << p << endl;
  int w = 0;
  for (; 2 << w < p; ++w);
  if (((p - 1) & (p - 2)) || w > R)
    return MKP (0, fpm (k, fpm (2, R, p - 1), p) - 1);

  int64 a = k / p % p, b = k % p;
  for (; w--; )
    {
      int64 na = a * b * 2;
      int64 nb = b * b;
      a = (na + nb / p) % p;
      b = nb % p;
    }
  // assert (b == 1);
  // cerr << a << " " << b << endl;
  return MKP (a * fpm (2, R - w, p) % p, 0);
}

int main ()
{
#ifndef ONLINE_JUDGE
  freopen ("185D.in" , "r", stdin);
  freopen ("185D.out", "w", stdout);
#endif
  ios_base::sync_with_stdio (false);

  int tc, k, p; int64 L, R;

  cin >> tc;
  for (; tc--; )
    {
      int64 ans;

      cin >> k >> L >> R >> p;
      if (k % p == 0)
        ans = 1;
      else if (k == 1)
        ans = fpm (2, R - L + 1, p);
      else
        {
          pair<int64, int64> ansR = calc (k, R + 1, p), ansL = calc (k, L, p);
          if (ansL.Py)
            ans = ansR.Py * fpm (ansL.Py, p - 2, p) % p;
          else
            ans = ansR.Px * fpm (ansL.Px, p - 2, p) % p;
        }
      if (k & 1)
        ans = ans * fpm (2, (R - L) % (p - 1) * (p - 2) % (p - 1), p) % p;
      cout << ans << endl;
    }

  return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <cassert>
#include <initializer_list>
#include <unordered_map>
#include <unordered_set>

using namespace std;

#ifndef ONLINE_JUDGE
#define DEBUG
#endif

#define oo 0x3F3F3F3F
#ifdef DEBUG
#define cvar(x) cerr << "<" << #x << ": " << x << ">"
#define evar(x) cvar (x) << endl
template<class T> void DISP (const char *s, T x, int n) {cerr << "[" << s << ": "; for (int i = 0; i < n; ++i) cerr << x[i] << " "; cerr << "]" << endl;}
#define disp(x,n) DISP (#x " to " #n, x, n)
#else
#define cvar(...) ({})
#define evar(...) ({})
#define disp(...) ({})
#endif
#define MKP make_pair
#define Px first
#define Py second
#define PB push_back
#define SZ(x) (int)((x).size ())
#define ALL(x) (x).begin (), (x).end ()

typedef unsigned int uint;
typedef long long int64;
typedef unsigned long long uint64;
typedef long double real;
typedef unordered_map<int, int> UMII;
typedef unordered_set<int> USI;

int64 fpm (int64 b, int64 e, int64 m) { int64 t = 1; for (; e; e >>= 1, b = b * b % m) e & 1 ? t = t * b % m : 0; return t; }
template<class T> inline bool chkmin (T &a, T b) {return a > b ? a = b, true : false;}
template<class T> inline bool chkmax (T &a, T b) {return a < b ? a = b, true : false;}
template<class T> inline T sqr (T x) {return x * x;}

template<class edge> struct Graph
{
  vector<vector<edge> > adj;
  Graph (int n) {adj.clear (); adj.resize (n + 5);}
  Graph () {adj.clear (); }
  void resize (int n) {adj.resize (n + 5); }
  void add (int s, edge e){adj[s].push_back (e);}
  void del (int s, edge e) {adj[s].erase (find (iter (adj[s]), e)); }
  int deg (int v) {return adj[v].size (); }
  vector<edge>& operator [](int t) {return adj[t];}
};

pair<int64, int64> calc (int k, int64 R, int p)
{
  cerr << "calc: " << k << " " << R << " " << p << endl;
  int w = 0;
  for (; 2 << w < p; ++w);
  if (((p - 1) & (p - 2)) || w > R)
    return MKP (0, fpm (k, fpm (2, R, p - 1), p) - 1);

  int64 a = k / p % p, b = k % p;
  for (; w--; )
    {
      int64 na = a * b * 2;
      int64 nb = b * b;
      a = (na + nb / p) % p;
      b = nb % p;
    }
  // assert (b == 1);
  cerr << a << " " << b << endl;
  return MKP (a * fpm (2, R - w, p) % p, 0);
}

int main ()
{
#ifndef ONLINE_JUDGE
  freopen ("185D.in" , "r", stdin);
  freopen ("185D.out", "w", stdout);
#endif
  ios_base::sync_with_stdio (false);

  int tc, k, p; int64 L, R;

  cin >> tc;
  for (; tc--; )
    {
      int64 ans;

      cin >> k >> L >> R >> p;
      if (k % p == 0)
        ans = 1;
      else if (k % p == 1)
        ans = fpm (2, R - L + 1, p);
      else
        {
          pair<int64, int64> ansR = calc (k, R + 1, p), ansL = calc (k, L, p);
          if (ansL.Py)
            ans = ansR.Py * fpm (ansL.Py, p - 2, p) % p;
          else
            ans = ansR.Px * fpm (ansL.Px, p - 2, p) % p;
        }
      if (k & 1)
        ans = ans * fpm (2, (R - L) % (p - 1) * (p - 2) % (p - 1), p) % p;
      cout << ans << endl;
    }

  return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <cassert>
#include <initializer_list>
#include <unordered_map>
#include <unordered_set>

using namespace std;

#ifndef ONLINE_JUDGE
#define DEBUG
#endif

#define oo 0x3F3F3F3F
#ifdef DEBUG
#define cvar(x) cerr << "<" << #x << ": " << x << ">"
#define evar(x) cvar (x) << endl
template<class T> void DISP (const char *s, T x, int n) {cerr << "[" << s << ": "; for (int i = 0; i < n; ++i) cerr << x[i] << " "; cerr << "]" << endl;}
#define disp(x,n) DISP (#x " to " #n, x, n)
#else
#define cvar(...) ({})
#define evar(...) ({})
#define disp(...) ({})
#endif
#define MKP make_pair
#define Px first
#define Py second
#define PB push_back
#define SZ(x) (int)((x).size ())
#define ALL(x) (x).begin (), (x).end ()

typedef unsigned int uint;
typedef long long int64;
typedef unsigned long long uint64;
typedef long double real;
typedef unordered_map<int, int> UMII;
typedef unordered_set<int> USI;

int64 fpm (int64 b, int64 e, int64 m) { int64 t = 1; for (; e; e >>= 1, b = b * b % m) e & 1 ? t = t * b % m : 0; return t; }
template<class T> inline bool chkmin (T &a, T b) {return a > b ? a = b, true : false;}
template<class T> inline bool chkmax (T &a, T b) {return a < b ? a = b, true : false;}
template<class T> inline T sqr (T x) {return x * x;}

template<class edge> struct Graph
{
  vector<vector<edge> > adj;
  Graph (int n) {adj.clear (); adj.resize (n + 5);}
  Graph () {adj.clear (); }
  void resize (int n) {adj.resize (n + 5); }
  void add (int s, edge e){adj[s].push_back (e);}
  void del (int s, edge e) {adj[s].erase (find (iter (adj[s]), e)); }
  int deg (int v) {return adj[v].size (); }
  vector<edge>& operator [](int t) {return adj[t];}
};

pair<int64, int64> calc (int k, int64 R, int p)
{
  cerr << "calc: " << k << " " << R << " " << p << endl;
  int w = 0;
  for (; 2 << w < p; ++w);
  if (((p - 1) & (p - 2)) || w > R)
    return MKP (0, fpm (k, fpm (2, R, p - 1), p) - 1);

  int64 a = k / p % p, b = k % p;
  for (; w--; )
    {
      int64 na = a * b * 2;
      int64 nb = b * b;
      a = (na + nb / p) % p;
      b = nb % p;
    }
  // assert (b == 1);
  // cerr << a << " " << b << endl;
  return MKP (a * fpm (2, R - w, p) % p, 0);
}

int main ()
{
#ifndef ONLINE_JUDGE
  freopen ("185D.in" , "r", stdin);
  freopen ("185D.out", "w", stdout);
#endif
  ios_base::sync_with_stdio (false);

  int tc, k, p; int64 L, R;

  cin >> tc;
  for (; tc--; )
    {
      int64 ans;

      cin >> k >> L >> R >> p;
      if (k % p == 0)
        ans = 1;
      else if (k == 1)
        ans = 2 % p;
      else
        {
          pair<int64, int64> ansR = calc (k, R + 1, p), ansL = calc (k, L, p);
          if (ansL.Py)
            ans = ansR.Py * fpm (ansL.Py, p - 2, p) % p;
          else
            ans = ansR.Px * fpm (ansL.Px, p - 2, p) % p;
        }
      if (k & 1)
        ans = ans * fpm (2, (R - L) % (p - 1) * (p - 2) % (p - 1), p) % p;
      cout << ans << endl;
    }

  return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <cassert>
#include <initializer_list>
#include <unordered_map>
#include <unordered_set>

using namespace std;

#ifndef ONLINE_JUDGE
#define DEBUG
#endif

#define oo 0x3F3F3F3F
#ifdef DEBUG
#define cvar(x) cerr << "<" << #x << ": " << x << ">"
#define evar(x) cvar (x) << endl
template<class T> void DISP (const char *s, T x, int n) {cerr << "[" << s << ": "; for (int i = 0; i < n; ++i) cerr << x[i] << " "; cerr << "]" << endl;}
#define disp(x,n) DISP (#x " to " #n, x, n)
#else
#define cvar(...) ({})
#define evar(...) ({})
#define disp(...) ({})
#endif
#define MKP make_pair
#define Px first
#define Py second
#define PB push_back
#define SZ(x) (int)((x).size ())
#define ALL(x) (x).begin (), (x).end ()

typedef unsigned int uint;
typedef long long int64;
typedef unsigned long long uint64;
typedef long double real;
typedef unordered_map<int, int> UMII;
typedef unordered_set<int> USI;

int64 fpm (int64 b, int64 e, int64 m) { int64 t = 1; for (; e; e >>= 1, b = b * b % m) e & 1 ? t = t * b % m : 0; return t; }
template<class T> inline bool chkmin (T &a, T b) {return a > b ? a = b, true : false;}
template<class T> inline bool chkmax (T &a, T b) {return a < b ? a = b, true : false;}
template<class T> inline T sqr (T x) {return x * x;}

template<class edge> struct Graph
{
  vector<vector<edge> > adj;
  Graph (int n) {adj.clear (); adj.resize (n + 5);}
  Graph () {adj.clear (); }
  void resize (int n) {adj.resize (n + 5); }
  void add (int s, edge e){adj[s].push_back (e);}
  void del (int s, edge e) {adj[s].erase (find (iter (adj[s]), e)); }
  int deg (int v) {return adj[v].size (); }
  vector<edge>& operator [](int t) {return adj[t];}
};

pair<int64, int64> calc (int k, int64 R, int p)
{
  cerr << "calc: " << k << " " << R << " " << p << endl;
  int w = 0;
  for (; 2 << w < p; ++w);
  if (((p - 1) & (p - 2)) || w > R)
    return MKP (0, fpm (k, fpm (2, R, p - 1), p) - 1);

  int64 a = k / p % p, b = k % p;
  for (; w--; )
    {
      int64 na = a * b * 2;
      int64 nb = b * b;
      a = (na + nb / p) % p;
      b = nb % p;
    }
  // assert (b == 1);
  // cerr << a << " " << b << endl;
  return MKP (a * fpm (2, R - w, p) % p, 0);
}

int main ()
{
#ifndef ONLINE_JUDGE
  freopen ("185D.in" , "r", stdin);
  freopen ("185D.out", "w", stdout);
#endif
  ios_base::sync_with_stdio (false);

  int tc, k, p; int64 L, R;

  cin >> tc;
  for (; tc--; )
    {
      cin >> k >> L >> R >> p;
      if (k % p == 0)
        {
          if ((k & 1) && p != 2)
            cout << fpm (2, (R - L) % (p - 1) * (p - 2) % (p - 1), p) << endl;
          else
            cout << 1 << endl;
        }
      else if (k == 1)
        cout << 2 % p << endl;
      else
        {
          pair<int64, int64> ansR = calc (k, R + 1, p), ansL = calc (k, L, p);
          int64 ans;
          if (ansL.Py)
            ans = ansR.Py * fpm (ansL.Py, p - 2, p) % p;
          else
            ans = ansR.Px * fpm (ansL.Px, p - 2, p) % p;
          if ((k & 1) && p != 2)
            ans = ans * fpm (2, (R - L) % (p - 1) * (p - 2) % (p - 1), p), cerr << (R - L) % (p - 1) * (p - 2) % (p - 1) << endl;
          cout << ans << endl;
        }
    }

  return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <cassert>
#include <initializer_list>
#include <unordered_map>
#include <unordered_set>

using namespace std;

#ifndef ONLINE_JUDGE
#define DEBUG
#endif

#define oo 0x3F3F3F3F
#ifdef DEBUG
#define cvar(x) cerr << "<" << #x << ": " << x << ">"
#define evar(x) cvar (x) << endl
template<class T> void DISP (const char *s, T x, int n) {cerr << "[" << s << ": "; for (int i = 0; i < n; ++i) cerr << x[i] << " "; cerr << "]" << endl;}
#define disp(x,n) DISP (#x " to " #n, x, n)
#else
#define cvar(...) ({})
#define evar(...) ({})
#define disp(...) ({})
#endif
#define MKP make_pair
#define Px first
#define Py second
#define PB push_back
#define SZ(x) (int)((x).size ())
#define ALL(x) (x).begin (), (x).end ()

typedef unsigned int uint;
typedef long long int64;
typedef unsigned long long uint64;
typedef long double real;
typedef unordered_map<int, int> UMII;
typedef unordered_set<int> USI;

int64 fpm (int64 b, int64 e, int64 m) { int64 t = 1; for (; e; e >>= 1, b = b * b % m) e & 1 ? t = t * b % m : 0; return t; }
template<class T> inline bool chkmin (T &a, T b) {return a > b ? a = b, true : false;}
template<class T> inline bool chkmax (T &a, T b) {return a < b ? a = b, true : false;}
template<class T> inline T sqr (T x) {return x * x;}

template<class edge> struct Graph
{
  vector<vector<edge> > adj;
  Graph (int n) {adj.clear (); adj.resize (n + 5);}
  Graph () {adj.clear (); }
  void resize (int n) {adj.resize (n + 5); }
  void add (int s, edge e){adj[s].push_back (e);}
  void del (int s, edge e) {adj[s].erase (find (iter (adj[s]), e)); }
  int deg (int v) {return adj[v].size (); }
  vector<edge>& operator [](int t) {return adj[t];}
};

const int maxr = 33;

pair<int64, int64> get_mod (int k, int m, int P, int r) // return <a, r> k^{2^m} = a * (p^r) + (r != 0)
{
  int64 v[maxr] = {0}, u[maxr];

  for (int i = 0; i <= r; ++i)
    v[i] = k % P, k /= P;
  for (int i = 1; i <= m; ++i)
    {
      for (int x = 0; x <= r; ++x)
        u[x] = 0;
      for (int x = 0; x <= r; ++x)
        {
          int64 s = 0;
          for (int y = 0; x + y <= r; ++y)
            s += v[x] * v[y] + u[x + y], u[x + y] = s % P, s /= P;
        }
      for (int x = 0; x <= r; ++x)
        v[x] = u[x];
    }
  assert (v[0] == 1);

  int lx = 1;
  for (; !v[lx] && lx < r; ++lx);
  return MKP (v[lx], lx);
}

pair<int64, int64> calc (int k, int64 R, int P) // return <a, r> k^{2^R} - 1 = a * p^r
{
  cerr << "calc: " << k << " " << R << " " << P << endl;
  int64 rem = fpm (k, fpm (2, R, P - 1), P);
  if (rem != 1) return MKP (rem - 1, 0);

  int64 w = 0;
  for (; 2 << w < P; ++w);
  pair<int64, int64> ret = MKP (0, 0);
  for (int r = 1; ret.Px == 0; r *= 2)
    ret = get_mod (k, min (w, R), P, r);
  return ret;
}

int main ()
{
#ifndef ONLINE_JUDGE
  freopen ("185D.in" , "r", stdin);
  freopen ("185D.out", "w", stdout);
#endif
  ios_base::sync_with_stdio (false);

  int tc, k, P; int64 L, R;

  cin >> tc;
  for (; tc--; )
    {
      int64 ans;

      cin >> k >> L >> R >> P;
      if (k % P == 0)
        ans = 1;
      else if (k % P == 1)
        ans = fpm (2, R - L + 1, P);
      else
        {
          pair<int64, int64> ansR = calc (k, R + 1, P), ansL = calc (k, L, P);
          if (ansL.Py != ansR.Py)
            ans = 0;
          else
            ans = ansR.Px * fpm (ansL.Px, P - 2, P) % P;
        }
      if (k & 1)
        ans = ans * fpm (2, (R - L) % (P - 1) * (P - 2) % (P - 1), P) % P;
      cout << ans << endl;
    }

  return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <cassert>
#include <initializer_list>
#include <unordered_map>
#include <unordered_set>

using namespace std;

#ifndef ONLINE_JUDGE
#define DEBUG
#endif

#define oo 0x3F3F3F3F
#ifdef DEBUG
#define cvar(x) cerr << "<" << #x << ": " << x << ">"
#define evar(x) cvar (x) << endl
template<class T> void DISP (const char *s, T x, int n) {cerr << "[" << s << ": "; for (int i = 0; i < n; ++i) cerr << x[i] << " "; cerr << "]" << endl;}
#define disp(x,n) DISP (#x " to " #n, x, n)
#else
#define cvar(...) ({})
#define evar(...) ({})
#define disp(...) ({})
#endif
#define MKP make_pair
#define Px first
#define Py second
#define PB push_back
#define SZ(x) (int)((x).size ())
#define ALL(x) (x).begin (), (x).end ()

typedef unsigned int uint;
typedef long long int64;
typedef unsigned long long uint64;
typedef long double real;
typedef unordered_map<int, int> UMII;
typedef unordered_set<int> USI;

int64 fpm (int64 b, int64 e, int64 m) { int64 t = 1; for (; e; e >>= 1, b = b * b % m) e & 1 ? t = t * b % m : 0; return t; }
template<class T> inline bool chkmin (T &a, T b) {return a > b ? a = b, true : false;}
template<class T> inline bool chkmax (T &a, T b) {return a < b ? a = b, true : false;}
template<class T> inline T sqr (T x) {return x * x;}

template<class edge> struct Graph
{
  vector<vector<edge> > adj;
  Graph (int n) {adj.clear (); adj.resize (n + 5);}
  Graph () {adj.clear (); }
  void resize (int n) {adj.resize (n + 5); }
  void add (int s, edge e){adj[s].push_back (e);}
  void del (int s, edge e) {adj[s].erase (find (iter (adj[s]), e)); }
  int deg (int v) {return adj[v].size (); }
  vector<edge>& operator [](int t) {return adj[t];}
};

const int maxr = 30;

pair<int64, int64> get_mod (int k, int m, int P, int r) // return <a, r> k^{2^m} = a * (p^r) + (r != 0)
{
  int64 v[maxr] = {0}, u[maxr];

  for (int i = 0; i <= r; ++i)
    v[i] = k % P, k /= P;
  for (int i = 1; i <= m; ++i)
    {
      for (int x = 0; x <= r; ++x)
        u[x] = 0;
      for (int x = 0; x <= r; ++x)
        for (int y = 0; x + y <= r; ++y)
          u[x + y] += v[x] * v[y] % P;
      for (int x = 0; x <= r; ++x)
        v[x] = u[x] % P;
    }
  assert (v[0] == 1);

  int lx = 1;
  for (; !v[lx] && lx < r; ++lx);
  return MKP (v[lx], lx);
}

pair<int64, int64> calc (int k, int64 R, int P) // return <a, r> k^{2^R} - 1 = a * p^r
{
  cerr << "calc: " << k << " " << R << " " << P << endl;
  int64 rem = fpm (k, fpm (2, R, P - 1), P);
  if (rem != 1) return MKP (rem - 1, 0);

  int64 w = 0;
  for (; 2 << w < P; ++w);
  pair<int64, int64> ret = MKP (0, 0);
  for (int r = 1; ret.Px == 0; r *= 2)
    ret = get_mod (k, min (w, R), P, r);
  return ret;
}

int main ()
{
#ifndef ONLINE_JUDGE
  freopen ("185D.in" , "r", stdin);
  freopen ("185D.out", "w", stdout);
#endif
  ios_base::sync_with_stdio (false);

  int tc, k, P; int64 L, R;

  cin >> tc;
  for (; tc--; )
    {
      int64 ans;

      cin >> k >> L >> R >> P;
      if (k % P == 0)
        ans = 1;
      else if (k % P == 1)
        ans = fpm (2, R - L + 1, P);
      else
        {
          pair<int64, int64> ansR = calc (k, R + 1, P), ansL = calc (k, L, P);
          if (ansL.Py != ansR.Py)
            ans = 0;
          else
            ans = ansR.Px * fpm (ansL.Px, P - 2, P) % P;
        }
      if (k & 1)
        ans = ans * fpm (2, (R - L) % (P - 1) * (P - 2) % (P - 1), P) % P;
      cout << ans << endl;
    }

  return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <cassert>
#include <initializer_list>
#include <unordered_map>
#include <unordered_set>

using namespace std;

#ifndef ONLINE_JUDGE
#define DEBUG
#endif

#define oo 0x3F3F3F3F
#ifdef DEBUG
#define cvar(x) cerr << "<" << #x << ": " << x << ">"
#define evar(x) cvar (x) << endl
template<class T> void DISP (const char *s, T x, int n) {cerr << "[" << s << ": "; for (int i = 0; i < n; ++i) cerr << x[i] << " "; cerr << "]" << endl;}
#define disp(x,n) DISP (#x " to " #n, x, n)
#else
#define cvar(...) ({})
#define evar(...) ({})
#define disp(...) ({})
#endif
#define MKP make_pair
#define Px first
#define Py second
#define PB push_back
#define SZ(x) (int)((x).size ())
#define ALL(x) (x).begin (), (x).end ()

typedef unsigned int uint;
typedef long long int64;
typedef unsigned long long uint64;
typedef long double real;
typedef unordered_map<int, int> UMII;
typedef unordered_set<int> USI;

int64 fpm (int64 b, int64 e, int64 m) { int64 t = 1; for (; e; e >>= 1, b = b * b % m) e & 1 ? t = t * b % m : 0; return t; }
template<class T> inline bool chkmin (T &a, T b) {return a > b ? a = b, true : false;}
template<class T> inline bool chkmax (T &a, T b) {return a < b ? a = b, true : false;}
template<class T> inline T sqr (T x) {return x * x;}

template<class edge> struct Graph
{
  vector<vector<edge> > adj;
  Graph (int n) {adj.clear (); adj.resize (n + 5);}
  Graph () {adj.clear (); }
  void resize (int n) {adj.resize (n + 5); }
  void add (int s, edge e){adj[s].push_back (e);}
  void del (int s, edge e) {adj[s].erase (find (iter (adj[s]), e)); }
  int deg (int v) {return adj[v].size (); }
  vector<edge>& operator [](int t) {return adj[t];}
};

pair<int64, int64> calc (int k, int64 R, int p)
{
  cerr << "calc: " << k << " " << R << " " << p << endl;
  int w = 0;
  for (; 2 << w < p; ++w);
  if (((p - 1) & (p - 2)) || w > R)
    return MKP (0, fpm (k, fpm (2, R, p - 1), p) - 1);

  int64 a = k / p % p, b = k % p;
  for (; w--; )
    {
      int64 na = a * b * 2;
      int64 nb = b * b;
      a = (na + nb / p) % p;
      b = nb % p;
    }
  // assert (b == 1);
  // cerr << a << " " << b << endl;
  return MKP (a * fpm (2, R - w, p) % p, 0);
}

int main ()
{
#ifndef ONLINE_JUDGE
  freopen ("185D.in" , "r", stdin);
  freopen ("185D.out", "w", stdout);
#endif
  ios_base::sync_with_stdio (false);

  int tc, k, p; int64 L, R;

  cin >> tc;
  for (; tc--; )
    {
      cin >> k >> L >> R >> p;
      if (k % p == 0)
        cout << 1 << endl;
      else if (k == 1)
        cout << fpm (2, R - L + 1, p) << endl;
      else
        {
          pair<int64, int64> ansR = calc (k, R + 1, p), ansL = calc (k, L, p);
          if (ansL.Py)
            cout << ansR.Py * fpm (ansL.Py, p - 2, p) % p << endl;
          else
            cout << ansR.Px * fpm (ansL.Px, p - 2, p) % p << endl;
        }
    }

  return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <cassert>
#include <initializer_list>
#include <unordered_map>
#include <unordered_set>

using namespace std;

#ifndef ONLINE_JUDGE
#define DEBUG
#endif

#define oo 0x3F3F3F3F
#ifdef DEBUG
#define cvar(x) cerr << "<" << #x << ": " << x << ">"
#define evar(x) cvar (x) << endl
template<class T> void DISP (const char *s, T x, int n) {cerr << "[" << s << ": "; for (int i = 0; i < n; ++i) cerr << x[i] << " "; cerr << "]" << endl;}
#define disp(x,n) DISP (#x " to " #n, x, n)
#else
#define cvar(...) ({})
#define evar(...) ({})
#define disp(...) ({})
#endif
#define MKP make_pair
#define Px first
#define Py second
#define PB push_back
#define SZ(x) (int)((x).size ())
#define ALL(x) (x).begin (), (x).end ()

typedef unsigned int uint;
typedef long long int64;
typedef unsigned long long uint64;
typedef long double real;
typedef unordered_map<int, int> UMII;
typedef unordered_set<int> USI;

int64 fpm (int64 b, int64 e, int64 m) { int64 t = 1; for (; e; e >>= 1, b = b * b % m) e & 1 ? t = t * b % m : 0; return t; }
template<class T> inline bool chkmin (T &a, T b) {return a > b ? a = b, true : false;}
template<class T> inline bool chkmax (T &a, T b) {return a < b ? a = b, true : false;}
template<class T> inline T sqr (T x) {return x * x;}

template<class edge> struct Graph
{
  vector<vector<edge> > adj;
  Graph (int n) {adj.clear (); adj.resize (n + 5);}
  Graph () {adj.clear (); }
  void resize (int n) {adj.resize (n + 5); }
  void add (int s, edge e){adj[s].push_back (e);}
  void del (int s, edge e) {adj[s].erase (find (iter (adj[s]), e)); }
  int deg (int v) {return adj[v].size (); }
  vector<edge>& operator [](int t) {return adj[t];}
};

pair<int64, int64> calc (int k, int64 R, int p)
{
  cerr << "calc: " << k << " " << R << " " << p << endl;
  int w = 0;
  for (; 2 << w < p; ++w);
  if (((p - 1) & (p - 2)) || w > R)
    return MKP (0, fpm (k, fpm (2, R, p - 1), p) - 1);

  int64 a = k / p % p, b = k % p;
  for (; w--; )
    {
      int64 na = a * b * 2;
      int64 nb = b * b;
      a = (na + nb / p) % p;
      b = nb % p;
    }
  // assert (b == 1);
  // cerr << a << " " << b << endl;
  return MKP (a * fpm (2, R - w, p) % p, 0);
}

int main ()
{
#ifndef ONLINE_JUDGE
  freopen ("185D.in" , "r", stdin);
  freopen ("185D.out", "w", stdout);
#endif
  ios_base::sync_with_stdio (false);

  int tc, k, p; int64 L, R;

  cin >> tc;
  for (; tc--; )
    {
      cin >> k >> L >> R >> p;
      if (k % p == 0)
        cout << 1 << endl;
      else if (k == 1)
        cout << 2 % p << endl;
      else
        {
          pair<int64, int64> ansR = calc (k, R + 1, p), ansL = calc (k, L, p);
          int64 ans;
          if (ansL.Py)
            ans = ansR.Py * fpm (ansL.Py, p - 2, p) % p;
          else
            ans = ansR.Px * fpm (ansL.Px, p - 2, p) % p;
          if (k & 1)
            ans = ans * fpm (2, (L - R) % (p - 2) + p - 2, p);
          cout << ans << endl;
        }
    }

  return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <cassert>
#include <initializer_list>
#include <unordered_map>
#include <unordered_set>

using namespace std;

#ifndef ONLINE_JUDGE
#define DEBUG
#endif

#define oo 0x3F3F3F3F
#ifdef DEBUG
#define cvar(x) cerr << "<" << #x << ": " << x << ">"
#define evar(x) cvar (x) << endl
template<class T> void DISP (const char *s, T x, int n) {cerr << "[" << s << ": "; for (int i = 0; i < n; ++i) cerr << x[i] << " "; cerr << "]" << endl;}
#define disp(x,n) DISP (#x " to " #n, x, n)
#else
#define cvar(...) ({})
#define evar(...) ({})
#define disp(...) ({})
#endif
#define MKP make_pair
#define Px first
#define Py second
#define PB push_back
#define SZ(x) (int)((x).size ())
#define ALL(x) (x).begin (), (x).end ()

typedef unsigned int uint;
typedef long long int64;
typedef unsigned long long uint64;
typedef long double real;
typedef unordered_map<int, int> UMII;
typedef unordered_set<int> USI;

int64 pow (int64 b, int64 e, int64 m) { int64 t = 1; for (; e; e >>= 1, b = b * b % m) e & 1 ? t = t * b % m : 0; return t; }
template<class T> inline bool chkmin (T &a, T b) {return a > b ? a = b, true : false;}
template<class T> inline bool chkmax (T &a, T b) {return a < b ? a = b, true : false;}
template<class T> inline T sqr (T x) {return x * x;}

template<class edge> struct Graph
{
  vector<vector<edge> > adj;
  Graph (int n) {adj.clear (); adj.resize (n + 5);}
  Graph () {adj.clear (); }
  void resize (int n) {adj.resize (n + 5); }
  void add (int s, edge e){adj[s].push_back (e);}
  void del (int s, edge e) {adj[s].erase (find (iter (adj[s]), e)); }
  int deg (int v) {return adj[v].size (); }
  vector<edge>& operator [](int t) {return adj[t];}
};

const int maxr = 33;

pair<int64, int64> get_mod (int k, int m, int P, int r) // return <a, r> k^{2^m} = a * (p^r) + (r != 0)
{
  int64 v[maxr] = {0}, u[maxr];

  for (int i = 0; i <= r; ++i)
    v[i] = k % P, k /= P;
  for (int i = 1; i <= m; ++i)
    {
      for (int x = 0; x <= r; ++x)
        u[x] = 0;
      for (int x = 0; x <= r; ++x)
        {
          int64 s = 0;
          for (int y = 0; x + y <= r; ++y)
            s += v[x] * v[y] + u[x + y], u[x + y] = s % P, s /= P;
        }
      for (int x = 0; x <= r; ++x)
        v[x] = u[x];
    }
  assert (v[0] == 1);

  int lx = 1;
  for (; !v[lx] && lx < r; ++lx);
  return MKP (v[lx], lx);
}

pair<int64, int64> calc (int k, int64 R, int P) // return <a, r> k^{2^R} - 1 = a * p^r
{
  cerr << "calc: " << k << " " << R << " " << P << endl;
  int64 rem = pow (k, pow (2, R, P - 1), P);
  if (rem != 1) return MKP (rem - 1, 0);

  int64 w = 0;
  for (; 2 << w < P; ++w);
  pair<int64, int64> ret = MKP (0, 0);
  for (int r = 1; ret.Px == 0; r *= 2)
    ret = get_mod (k, min (w, R), P, r);
  ret.Px = ret.Px * pow (2, R - min (w, R), P);
  return ret;
}

int main ()
{
#ifndef ONLINE_JUDGE
  freopen ("185D.in" , "r", stdin);
  freopen ("185D.out", "w", stdout);
#endif
  ios_base::sync_with_stdio (false);

  int tc, k, P; int64 L, R;

  cin >> tc;
  for (; tc--; )
    {
      int64 ans;

      cin >> k >> L >> R >> P;
      if (k % P == 0)
        ans = 1;
      else if (k % P == 1)
        ans = pow (2, R - L + 1, P);
      else
        {
          pair<int64, int64> ansR = calc (k, R + 1, P), ansL = calc (k, L, P);
          if (ansL.Py != ansR.Py)
            ans = 0;
          else
            ans = ansR.Px * pow (ansL.Px, P - 2, P) % P;
        }
      if (k & 1)
        ans = ans * pow (2, (R - L) % (P - 1) * (P - 2) % (P - 1), P) % P;
      cout << ans << endl;
    }

  return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <cassert>
#include <initializer_list>
#include <unordered_map>
#include <unordered_set>

using namespace std;

#ifndef ONLINE_JUDGE
#define DEBUG
#endif

#define oo 0x3F3F3F3F
#ifdef DEBUG
#define cvar(x) cerr << "<" << #x << ": " << x << ">"
#define evar(x) cvar (x) << endl
template<class T> void DISP (const char *s, T x, int n) {cerr << "[" << s << ": "; for (int i = 0; i < n; ++i) cerr << x[i] << " "; cerr << "]" << endl;}
#define disp(x,n) DISP (#x " to " #n, x, n)
#else
#define cvar(...) ({})
#define evar(...) ({})
#define disp(...) ({})
#endif
#define MKP make_pair
#define Px first
#define Py second
#define PB push_back
#define SZ(x) (int)((x).size ())
#define ALL(x) (x).begin (), (x).end ()

typedef unsigned int uint;
typedef long long int64;
typedef unsigned long long uint64;
typedef long double real;
typedef unordered_map<int, int> UMII;
typedef unordered_set<int> USI;

int64 fpm (int64 b, int64 e, int64 m) { int64 t = 1; for (; e; e >>= 1, b = b * b % m) e & 1 ? t = t * b % m : 0; return t; }
template<class T> inline bool chkmin (T &a, T b) {return a > b ? a = b, true : false;}
template<class T> inline bool chkmax (T &a, T b) {return a < b ? a = b, true : false;}
template<class T> inline T sqr (T x) {return x * x;}

template<class edge> struct Graph
{
  vector<vector<edge> > adj;
  Graph (int n) {adj.clear (); adj.resize (n + 5);}
  Graph () {adj.clear (); }
  void resize (int n) {adj.resize (n + 5); }
  void add (int s, edge e){adj[s].push_back (e);}
  void del (int s, edge e) {adj[s].erase (find (iter (adj[s]), e)); }
  int deg (int v) {return adj[v].size (); }
  vector<edge>& operator [](int t) {return adj[t];}
};

pair<int64, int64> calc (int k, int64 R, int p)
{
  cerr << "calc: " << k << " " << R << " " << p << endl;
  int w = 0;
  for (; 2 << w < p; ++w);
  if (((p - 1) & (p - 2)) || w > R)
    return MKP (0ll, fpm (k, fpm (2, R, p - 1), p) - 1);

  int64 a = k / p % p, b = k % p;
  for (; w--; )
    {
      int64 na = a * b * 2;
      int64 nb = b * b;
      a = (na + nb / p) % p;
      b = nb % p;
    }
  // assert (b == 1);
  // cerr << a << " " << b << endl;
  return MKP (a * fpm (2, R - w, p) % p, 0);
}

int main ()
{
#ifndef ONLINE_JUDGE
  freopen ("185D.in" , "r", stdin);
  freopen ("185D.out", "w", stdout);
#endif
  ios_base::sync_with_stdio (false);

  int tc, k, p; int64 L, R;

  cin >> tc;
  for (; tc--; )
    {
      cin >> k >> L >> R >> p;
      if (k % p == 0)
        cout << 1 << endl;
      else
        {
          pair<int64, int64> ansR = calc (k, R + 1, p), ansL = calc (k, L, p);
          if (ansL.Py)
            cout << ansR.Py * fpm (ansL.Py, p - 2, p) % p << endl;
          else
            cout << ansR.Px * fpm (ansL.Px, p - 2, p) % p << endl;
        }
    }

  return 0;
}


// 观察 + 逆元 + 前缀积
// 観察 + 逆数 + 接頭辞のプロダクト

#include<cstdio>

void Get(int &T)
{
	char C;bool F=0;
	for(;C=getchar(),C<'0'||C>'9';)if(C=='-')F=1;
	for(T=C-'0';C=getchar(),C>='0'&&C<='9';T=T*10+C-'0');
	F&&(T=-T);
}

void Get(long long &T)
{
	char C;bool F=0;
	for(;C=getchar(),C<'0'||C>'9';)if(C=='-')F=1;
	for(T=C-'0';C=getchar(),C>='0'&&C<='9';T=T*10+C-'0');
	F&&(T=-T);
}

long long L,R,K,P;

void Init()
{
	Get(K);Get(L);Get(R);Get(P);
}

long long Pow(long long X,long long Y,long long P)
{
	X%=P;
	long long Ans=1;
	for(;Y;Y>>=1)
	{
		if(Y&1)(Ans*=X)%=P;
		(X*=X)%=P;
	}
	return Ans;
}

long long Ans;

long long Product(long long N)
{
	long long Ans=(Pow(K,Pow(2,N,P-1),P)+P-1)%P;
	return Ans;
}

long long Pi(long long L,long long R)
{
	long long P1=Product(R+1);
	long long Q1=Product(L);

	if(Q1!=0)
		(P1*=Pow(Q1,P-2,P))%=P;
	else
		P1=Pow(2,R-L+1,P);

	return P1;
}

void Work()
{
	if(P==2)
	{
		Ans=((K&1)==0);
	}
	else
	{
		Ans=Pi(L,R);
		if((K&1)==1)
		{
			(Ans*=Pow(Pow(2,R-L+1,P),P-2,P))%=P;
			(Ans*=2)%=P;
		}
	}
}

void Output()
{
	printf("%I64d\n",Ans);
}

int main()
{
	int Case;
	Get(Case);
	for(int i=1;i<=Case;i++)
	{
		Init();
		Work();
		Output();
	}
	return 0;
}


// 观察 + 逆元 + 前缀积
// 観察 + 逆数 + 接頭辞のプロダクト

#include<cstdio>

void Get(int &T)
{
	char C;bool F=0;
	for(;C=getchar(),C<'0'||C>'9';)if(C=='-')F=1;
	for(T=C-'0';C=getchar(),C>='0'&&C<='9';T=T*10+C-'0');
	F&&(T=-T);
}

void Get(long long &T)
{
	char C;bool F=0;
	for(;C=getchar(),C<'0'||C>'9';)if(C=='-')F=1;
	for(T=C-'0';C=getchar(),C>='0'&&C<='9';T=T*10+C-'0');
	F&&(T=-T);
}

long long L,R,K,P;

void Init()
{
	Get(K);Get(L);Get(R);Get(P);
}

long long Pow(long long X,long long Y,long long P)
{
	X%=P;
	long long Ans=1;
	for(;Y;Y>>=1)
	{
		if(Y&1)(Ans*=X)%=P;
		(X*=X)%=P;
	}
	return Ans;
}

long long Ans;

long long Product(long long N)
{
	long long Ans=(Pow(K,Pow(2,N,P-1),P)+P-1)%P;
	return Ans;
}

long long Pi(long long L,long long R)
{
	if(K==P) return 1;

	long long P1=Product(R+1);
	long long Q1=Product(L);

	if(Q1!=0)
		(P1*=Pow(Q1,P-2,P))%=P;
	else
		P1=Pow(2,R-L+1,P);

	return P1;
}

void Work()
{
	if(P==2)
	{
		Ans=((K&1)==0);
	}
	else
	{
		Ans=Pi(L,R);
		if((K&1)==1)
		{
			(Ans*=Pow(Pow(2,R-L+1,P),P-2,P))%=P;
			(Ans*=2)%=P;
		}
	}
}

void Output()
{
	printf("%I64d\n",Ans);
}

int main()
{
	int Case;
	Get(Case);
	for(int i=1;i<=Case;i++)
	{
		Init();
		Work();
		Output();
	}
	return 0;
}