#include<iostream>
#include<stdio.h>
#include<string>
#include<algorithm>
#include<vector>
#include<map>
#include<cstdio>
using namespace std ;

#define MAXN 200007

int n ;
int k ;
int a[ MAXN ] ;

int dp[ MAXN ][ 7 ] ;
int prv[ MAXN ][ 7 ] ;
int ans[ MAXN ] ;

struct tuhla {
	int l , r ;
    int val ;
};

vector < struct tuhla > v ;
int tot ;

void paint_interval ( int l , int r , int st , int br ) {
	//printf ( "%d %d %d %d\n" , l , r , st , br ) ;
	int i ;
    for ( i = l ; i <= r ; i ++ ) {
    	if ( st > br ) {
        	ans[ i ] = ans[ i - 1 ] ;
            continue ;
        }
    	if ( ( i - l ) % 2 == 0 ) {
        	ans[ i ] = st ;
            st ++ ;
        }
        else {
        	ans[ i ] = a[ i - 1 ] ;
        }
    }
}
void paint_range ( int l , int r , int val ) {
	int i ;
    for ( i = l ; i <= r ; i ++ ) {
    	ans[ i ] = val ;
    }
}

bool getdp ( int id , int r ) {
	//printf ( "%d %d\n" , id , r ) ;
	if ( id == 0 ) {
    	int pr = v[ id ].val - 1 ;
        int i , j , t ;
        for ( i = v[ id ].l ; i >= r - 4 ; i -- ) {
        	if ( a[ i ] != 0 && a[ i ] != v[ id ].val ) { continue ; }
            if ( ( r - i + 1 ) < 2 ) { continue ; }
           	//printf ( " YYYY %d %d\n" , pr , ( i - 1 ) ) ;
        	if ( 2 * pr <= ( i - 1 ) && ( i - 1 ) <= 5 * pr ) {
            	if ( i - 1 >= 1 ) { paint_interval ( 1 , i - 1 , 1 , pr ) ; }
                paint_range ( i , r , v[ id ].val ) ;
                if ( k == 1 ) {
					for ( t = r + 1 ; t < n ; t ++ ) {
						if ( ( t - r ) % 2 == 1 ) { v[ id ].val ++ ; ans[ t ] = v[ id ].val ; }
						else { ans[ t ] = v[ id ].val ; }
					}
					if ( r != n ) { ans[ n ] = v[ id ].val ; }
					printf ( "%d\n" , ans[ n ] ) ;
					for ( t = 1 ; t <= n ; t ++ ) {
						printf ( "%d" , ans[ t ] ) ;
						if ( t == n ) { printf ( "\n" ) ; }
						else { printf ( " " ) ; }
					}
					exit ( 0 ) ;
                }
                return true ;
            }
        }
    }
    else {
    	int pr = v[ id ].val - v[ id - 1 ].val - 1 ;
        int i , j , t ;
        for ( i = v[ id ].l ; i >= r - 4 ; i -- ) {
            //printf ( "~~ %d\n" , i ) ;
        	if ( a[ i ] != 0 && a[ i ] != v[ id ].val ) { continue ; }
            if ( ( r - i + 1 ) < 2 ) { continue ; }
            for ( j = v[ id - 1 ].r ; j <= v[ id - 1 ].l + 4 ; j ++ ) {
            	//printf ( "   ### %d %d\n" , i , j ) ;
            	if ( a[ j ] != 0 && a[ j ] != v[ id - 1 ].val ) { continue ; }
                if ( j >= i ) { continue ; }
                int len = ( i - j - 1 ) ;
        		if ( ( 2 * pr <= len && len <= 5 * pr ) && getdp ( id - 1 , j ) == true ) {
            		if ( j + 1 <= i - 1 ) { paint_interval ( j + 1 , i - 1 , v[ id - 1 ].val + 1 , v[ id ].val - 1 ) ; }
                	paint_range ( i , r , v[ id ].val ) ;
                    if ( id == k - 1 ) {
                    	for ( t = r + 1 ; t < n ; t ++ ) {
                        	if ( ( t - r ) % 2 == 1 ) { v[ id ].val ++ ; ans[ t ] = v[ id ].val ; }
                            else { ans[ t ] = v[ id ].val ; }
                        }
                        if ( r != n ) { ans[ n ] = v[ id ].val ; }
                        int br = 0 ;
                        for ( t = 1 ; t <= n ; t ++ ) {
                        	if ( ans[ t ] == 0 ) { br ++ ;  }
                        }
                        if ( n == 200000 ) { printf ( "TOTAL WRONG --> %d\n" , br ) ; }
                        printf ( "%d\n" , ans[ n ] ) ;
                        for ( t = 1 ; t <= n ; t ++ ) {
                        	printf ( "%d" , ans[ t ] ) ;
	                        if ( t == n ) { printf ( "\n" ) ; }
                            else { printf ( " " ) ; }
                        }
						exit ( 0 ) ;
                    }
    	            return true ;
	            }
            }
        }
    }
    return false ;
}

void input ( ) {
	scanf ( "%d" , &n ) ;
    int i ;
    for ( i = 1 ; i <= n ; i ++ ) {
    	scanf ( "%d" , &a[ i ] ) ;
    }
}

void solve ( ) {
	int i , j , t ;
    int lst = 0 ;
    int pos = 0 ;
    vector < pair < int , int > > u ;
    for ( i = 1 ; i <= n ; i ++ ) {
    	if ( a[ i ] != 0 ) {
        	if ( lst != 0 && lst > a[ i ] ) { printf ( "-1\n" ) ; return ; }
        	if ( lst != 0 && lst == a[ i ] ) {
            	for ( j = pos ; j <= i ; j ++ ) {
                	a[ j ] = a[ i ] ;
                }
            }
            lst = a[ i ] ;
            pos = i ;
        }
    }
    if ( lst == 0 ) {
		for ( i = 1 ; i <= n ; i ++ ) {
        	if ( i % 2 == 1 ) { ans[ i ] = ( i / 2 + 1 ) ; }
            else { ans[ i ] = ans[ i - 1 ] ; }
        }
        if ( ans[ n ] != ans[ n - 1 ] ) { ans[ n ] = ans[ n - 1 ] ; }
        printf ( "%d\n" , ans[ n ] ) ;
        for ( i = 1 ; i <= n ; i ++ ) {
        	printf ( "%d" , ans[ i ] ) ;
	        if ( i == n ) { printf ( "\n" ) ; }
            else { printf ( " " ) ; }
        }
		return ;
    }
    for ( i = 1 ; i <= n ; i ++ ) {
    	if ( a[ i ] != 0 ) { u.push_back ( make_pair ( a[ i ] , i ) ) ; }
    }
    int br = 1 ;
    int mx = 0 ;
    int sz = u.size ( ) ;
    struct tuhla w ;
	for ( i = 1 ; i < sz ; i ++ ) {
    	if ( u[ i ].first == u[ i - 1 ].first ) { br ++ ; }
        else {
        	if ( mx < br ) { mx = br ; }
            w.r = u[ i - 1 ].second ;
            w.l = u[ i - br ] .second ;
            w.val = a[ u[ i - 1 ].second ] ;
            v.push_back ( w ) ;
            br = 1 ;
        }
    }
	if ( mx < br ) { mx = br ; }

	w.r = u[ i - 1 ].second ;
	w.l = u[ i - br ] .second ;
	w.val = a[ u[ i - 1 ].second ] ;
	v.push_back ( w ) ;
    if ( mx > 5 ) { printf ( "-1\n" ) ; return ; }
	k = v.size ( ) ;

	for ( i = 0 ; i < k ; i ++ ) {
    	//printf ( "--- %d %d %d\n" , v[ i ].l , v[ i ].r , v[ i ].val ) ;
        if ( a[ v[ i ].l ] != a[ v[ i ].r ] ) { printf ( "WTF??\n" ) ; }
        if ( a[ v[ i ].l ] != v[ i ].val ) { printf ( "WTF??\n" ) ; }
        if ( a[ v[ i ].r ] != v[ i ].val ) { printf ( "WTF??\n" ) ; }
    }
    if ( n == 200000 ) { printf ( "---- %d\n" , k ) ; }
    for ( i = 0 ; i < 5 ; i ++ ) {
    	if ( v[ ( k - 1 ) ].r + i == ( n - 1 ) ) { continue ; }
    	if ( v[ ( k - 1 ) ].r + i > n ) { continue ; }
    	if ( ( v[ ( k - 1 ) ].r - v[ ( k - 1 ) ].l + 1 ) + i > 5 ) { break ; }
        if ( v[ ( k - 1 ) ].r + i > n ) { continue ; }
		getdp ( k - 1 , v[ ( k - 1 ) ].r + i ) ;
    }
    printf ( "-1\n" ) ;
}

int main ( ) {
	ios::sync_with_stdio ( false ) ;
	cin.tie ( NULL ) ;
	input ( ) ;
	solve ( ) ;
	return 0 ;
}


#include<iostream>
#include<stdio.h>
#include<string>
#include<algorithm>
#include<vector>
#include<map>
#include<cstdio>
using namespace std ;

#define MAXN 200007

int n ;
int k ;
int a[ MAXN ] ;

int dp[ MAXN ][ 7 ] ;
int prv[ MAXN ][ 7 ] ;
int ans[ MAXN ] ;

bool fake[ MAXN ][ 7 ] ;

struct tuhla {
	int l , r ;
    int val ;
};

vector < struct tuhla > v ;
int tot ;

void paint_interval ( int lo , int hi , int beg , int en ) {
	//printf ( "%d %d %d %d\n" , l , r , st , br ) ;
   	/**
    if ( n == 200000 ) {
    	if ( lo <= 2485 && hi >= 2485 ) {
			printf ( "%d %d %d %d\n" , lo , hi , beg , en ) ;
        }
    }
	**/
	int i , j , t ;
	i = lo ;
    while ( 1 ) {
    	if ( i > n ) { return ; }
		for ( j = 2 ; j <= 5 ; j ++ ) {
        	int lft = ( ( hi - i + 1 ) - j ) ;
            int pr = en - beg ;
            if ( 2 * pr <= lft && lft <= 5 * pr ) {
            	break ;
            }
        }
        for ( t = 0 ; t < j ; t ++ ) {
        	ans[ i + t ] = beg ;
        }
        beg ++ ;
        i += j ;
    }
}
void paint_range ( int l , int r , int val ) {
	int i ;
    /**
    if ( n == 200000 ) {
    	if ( l <= 2485 && r >= 2485 ) {
			printf ( "%d %d %d\n" , l , r , val ) ;
        }
    }
	**/
    for ( i = l ; i <= r ; i ++ ) {
    	ans[ i ] = val ;
    }
}

bool getdp ( int id , int r ) {
	if ( fake[ id ][ r ] == true ) { return false ; }
	//printf ( "%d %d\n" , id , r ) ;
	if ( id == 0 ) {
    	int pr = v[ id ].val - 1 ;
        int i , j , t ;
        for ( i = v[ id ].l ; i >= r - 4 ; i -- ) {
        	if ( a[ i ] != 0 && a[ i ] != v[ id ].val ) { continue ; }
            if ( ( r - i + 1 ) < 2 ) { continue ; }
           	//printf ( " YYYY %d %d\n" , pr , ( i - 1 ) ) ;
        	if ( 2 * pr <= ( i - 1 ) && ( i - 1 ) <= 5 * pr ) {
            	if ( i - 1 >= 1 ) { paint_interval ( 1 , i - 1 , 1 , pr ) ; }
                paint_range ( i , r , v[ id ].val ) ;
                if ( k == 1 ) {
					for ( t = r + 1 ; t < n ; t ++ ) {
						if ( ( t - r ) % 2 == 1 ) { v[ id ].val ++ ; ans[ t ] = v[ id ].val ; }
						else { ans[ t ] = v[ id ].val ; }
					}
					if ( r != n ) { ans[ n ] = v[ id ].val ; }
					printf ( "%d\n" , ans[ n ] ) ;
					for ( t = 1 ; t <= n ; t ++ ) {
						printf ( "%d" , ans[ t ] ) ;
						if ( t == n ) { printf ( "\n" ) ; }
						else { printf ( " " ) ; }
					}
					exit ( 0 ) ;
                }
                return true ;
            }
        }
    }
    else {
    	int pr = v[ id ].val - v[ id - 1 ].val - 1 ;
        int i , j , t ;
        for ( i = v[ id ].l ; i >= r - 4 ; i -- ) {
            //printf ( "~~ %d\n" , i ) ;
        	if ( a[ i ] != 0 && a[ i ] != v[ id ].val ) { continue ; }
            if ( ( r - i + 1 ) < 2 ) { continue ; }
            for ( j = v[ id - 1 ].r ; j <= v[ id - 1 ].l + 4 ; j ++ ) {
            	//printf ( "   ### %d %d\n" , i , j ) ;
            	if ( a[ j ] != 0 && a[ j ] != v[ id - 1 ].val ) { continue ; }
                if ( j >= i ) { break ; }
                int len = ( i - j - 1 ) ;
        		if ( ( 2 * pr <= len && len <= 5 * pr ) && getdp ( id - 1 , j ) == true ) {
            		if ( j + 1 <= i - 1 ) { paint_interval ( j + 1 , i - 1 , v[ id - 1 ].val + 1 , v[ id ].val - 1 ) ; }
                	paint_range ( i , r , v[ id ].val ) ;
                    if ( id == k - 1 ) {
                    	for ( t = r + 1 ; t < n ; t ++ ) {
                        	if ( ( t - r ) % 2 == 1 ) { v[ id ].val ++ ; ans[ t ] = v[ id ].val ; }
                            else { ans[ t ] = v[ id ].val ; }
                        }
                        if ( r != n ) { ans[ n ] = v[ id ].val ; }
                        /**
                        int br = 0 ;
                        for ( t = 1 ; t <= n ; t ++ ) {
                        	if ( ans[ t ] == 0 ) { br ++ ;  }
                        }
						**/
                        //if ( n == 200000 ) { printf ( "TOTAL WRONG --> %d\n" , br ) ; }
                        printf ( "%d\n" , ans[ n ] ) ;
                        for ( t = 1 ; t <= n ; t ++ ) {
                        	printf ( "%d" , ans[ t ] ) ;
	                        if ( t == n ) { printf ( "\n" ) ; }
                            else { printf ( " " ) ; }
                        }
						exit ( 0 ) ;
                    }
    	            return true ;
	            }
            }
        }
    }
    fake[ id ][ r ] = true ;
    return false ;
}

void input ( ) {
	scanf ( "%d" , &n ) ;
    int i ;
    for ( i = 1 ; i <= n ; i ++ ) {
    	scanf ( "%d" , &a[ i ] ) ;
    }
}

void solve ( ) {
	int i , j , t ;
    int lst = 0 ;
    int pos = 0 ;
    vector < pair < int , int > > u ;
    for ( i = 1 ; i <= n ; i ++ ) {
    	if ( a[ i ] != 0 ) {
        	if ( lst != 0 && lst > a[ i ] ) { printf ( "-1\n" ) ; return ; }
        	if ( lst != 0 && lst == a[ i ] ) {
            	for ( j = pos ; j <= i ; j ++ ) {
                	a[ j ] = a[ i ] ;
                }
            }
            lst = a[ i ] ;
            pos = i ;
        }
    }
    if ( lst == 0 ) {
		for ( i = 1 ; i <= n ; i ++ ) {
        	if ( i % 2 == 1 ) { ans[ i ] = ( i / 2 + 1 ) ; }
            else { ans[ i ] = ans[ i - 1 ] ; }
        }
        if ( ans[ n ] != ans[ n - 1 ] ) { ans[ n ] = ans[ n - 1 ] ; }
        printf ( "%d\n" , ans[ n ] ) ;
        for ( i = 1 ; i <= n ; i ++ ) {
        	printf ( "%d" , ans[ i ] ) ;
	        if ( i == n ) { printf ( "\n" ) ; }
            else { printf ( " " ) ; }
        }
		return ;
    }
    for ( i = 1 ; i <= n ; i ++ ) {
    	if ( a[ i ] != 0 ) { u.push_back ( make_pair ( a[ i ] , i ) ) ; }
    }
    int br = 1 ;
    int mx = 0 ;
    int sz = u.size ( ) ;
    struct tuhla w ;
	for ( i = 1 ; i < sz ; i ++ ) {
    	if ( u[ i ].first == u[ i - 1 ].first ) { br ++ ; }
        else {
        	if ( mx < br ) { mx = br ; }
            w.r = u[ i - 1 ].second ;
            w.l = u[ i - br ] .second ;
            w.val = a[ u[ i - 1 ].second ] ;
            v.push_back ( w ) ;
            br = 1 ;
        }
    }
	if ( mx < br ) { mx = br ; }

	w.r = u[ i - 1 ].second ;
	w.l = u[ i - br ] .second ;
	w.val = a[ u[ i - 1 ].second ] ;
	v.push_back ( w ) ;
    if ( mx > 5 ) { printf ( "-1\n" ) ; return ; }
	k = v.size ( ) ;
    /**
	for ( i = 0 ; i < k ; i ++ ) {
    	//printf ( "--- %d %d %d\n" , v[ i ].l , v[ i ].r , v[ i ].val ) ;
        if ( a[ v[ i ].l ] != a[ v[ i ].r ] ) { printf ( "WTF??\n" ) ; }
        if ( a[ v[ i ].l ] != v[ i ].val ) { printf ( "WTF??\n" ) ; }
        if ( a[ v[ i ].r ] != v[ i ].val ) { printf ( "WTF??\n" ) ; }
    }
    if ( n == 200000 ) { printf ( "---- %d\n" , k ) ; }
	**/

    for ( i = 0 ; i < 5 ; i ++ ) {
    	if ( v[ ( k - 1 ) ].r + i == ( n - 1 ) ) { continue ; }
    	if ( v[ ( k - 1 ) ].r + i > n ) { continue ; }
    	if ( ( v[ ( k - 1 ) ].r - v[ ( k - 1 ) ].l + 1 ) + i > 5 ) { break ; }
        if ( v[ ( k - 1 ) ].r + i > n ) { continue ; }
		getdp ( k - 1 , v[ ( k - 1 ) ].r + i ) ;
    }
    printf ( "-1\n" ) ;
}

int main ( ) {
	ios::sync_with_stdio ( false ) ;
	cin.tie ( NULL ) ;
	input ( ) ;
	solve ( ) ;
	return 0 ;
}


#include<iostream>
#include<stdio.h>
#include<string>
#include<algorithm>
#include<vector>
#include<map>
#include<cstdio>
using namespace std ;

#define MAXN 200007

int n ;
int k ;
int a[ MAXN ] ;

int dp[ MAXN ][ 7 ] ;
int prv[ MAXN ][ 7 ] ;
int ans[ MAXN ] ;

struct tuhla {
	int l , r ;
    int val ;
};

vector < struct tuhla > v ;
int tot ;

void paint_interval ( int l , int r , int st , int br ) {
	//printf ( "%d %d %d %d\n" , l , r , st , br ) ;
	int i ;
    for ( i = l ; i <= r ; i ++ ) {
    	if ( st > br ) {
        	ans[ i ] = ans[ i - 1 ] ;
            continue ;
        }
    	if ( ( i - l ) % 2 == 0 ) {
        	ans[ i ] = st ;
            st ++ ;
        }
        else {
        	ans[ i ] = a[ i - 1 ] ;
        }
    }
}
void paint_range ( int l , int r , int val ) {
	int i ;
    for ( i = l ; i <= r ; i ++ ) {
    	ans[ i ] = val ;
    }
}

bool getdp ( int id , int r ) {
	//printf ( "%d %d\n" , id , r ) ;
	if ( id == 0 ) {
    	int pr = v[ id ].val - 1 ;
        int i , j , t ;
        for ( i = v[ id ].l ; i >= r - 4 ; i -- ) {
        	if ( a[ i ] != 0 && a[ i ] != v[ id ].val ) { continue ; }
            if ( ( r - i + 1 ) < 2 ) { continue ; }
           	//printf ( " YYYY %d %d\n" , pr , ( i - 1 ) ) ;
        	if ( 2 * pr <= ( i - 1 ) && ( i - 1 ) <= 5 * pr ) {
            	paint_interval ( 1 , i - 1 , 1 , pr ) ;
                paint_range ( i , r , v[ id ].val ) ;
                if ( k == 1 ) {
					for ( t = r + 1 ; t < n ; t ++ ) {
						if ( ( t - r ) % 2 == 1 ) { v[ id ].val ++ ; ans[ t ] = v[ id ].val ; }
						else { ans[ t ] = v[ id ].val ; }
					}
					if ( r != n ) { ans[ n ] = v[ id ].val ; }
					printf ( "%d\n" , ans[ n ] ) ;
					for ( t = 1 ; t <= n ; t ++ ) {
						printf ( "%d" , ans[ t ] ) ;
						if ( t == n ) { printf ( "\n" ) ; }
						else { printf ( " " ) ; }
					}
					exit ( 0 ) ;
                }
                return true ;
            }
        }
    }
    else {
    	int pr = v[ id ].val - v[ id - 1 ].val - 1 ;
        int i , j , t ;
        for ( i = v[ id ].l ; i >= r - 4 ; i -- ) {
            //printf ( "~~ %d\n" , i ) ;
        	if ( a[ i ] != 0 && a[ i ] != v[ id ].val ) { continue ; }
            if ( ( r - i + 1 ) < 2 ) { continue ; }
            for ( j = v[ id - 1 ].r ; j <= v[ id - 1 ].l + 4 ; j ++ ) {
            	//printf ( "   ### %d %d\n" , i , j ) ;
            	if ( a[ j ] != 0 && a[ j ] != v[ id - 1 ].val ) { continue ; }
                if ( j >= i ) { continue ; }
                int len = ( i - j - 1 ) ;
        		if ( getdp ( id - 1 , j ) == true && ( 2 * pr <= len && len <= 5 * pr ) ) {
            		paint_interval ( j + 1 , i - 1 , v[ id - 1 ].val + 1 , v[ id ].val - 1 ) ;
                	paint_range ( i , r , v[ id ].val ) ;
                    if ( id == k - 1 ) {
                    	for ( t = r + 1 ; t < n ; t ++ ) {
                        	if ( ( t - r ) % 2 == 1 ) { v[ id ].val ++ ; ans[ t ] = v[ id ].val ; }
                            else { ans[ t ] = v[ id ].val ; }
                        }
                        if ( r != n ) { ans[ n ] = v[ id ].val ; }
                        printf ( "%d\n" , ans[ n ] ) ;
                        for ( t = 1 ; t <= n ; t ++ ) {
                        	printf ( "%d" , ans[ t ] ) ;
	                        if ( t == n ) { printf ( "\n" ) ; }
                            else { printf ( " " ) ; }
                        }
						exit ( 0 ) ;
                    }
    	            return true ;
	            }
            }
        }
    }
    return false ;
}

void input ( ) {
	scanf ( "%d" , &n ) ;
    int i ;
    for ( i = 1 ; i <= n ; i ++ ) {
    	scanf ( "%d" , &a[ i ] ) ;
    }
}

void solve ( ) {
	int i , j , t ;
    int lst = 0 ;
    int pos = 0 ;
    vector < pair < int , int > > u ;
    for ( i = 1 ; i <= n ; i ++ ) {
    	if ( a[ i ] != 0 ) {
        	if ( lst != 0 && lst > a[ i ] ) { printf ( "-1\n" ) ; return ; }
        	if ( lst != 0 && lst == a[ i ] ) {
            	for ( j = pos ; j <= i ; j ++ ) {
                	a[ j ] = a[ i ] ;
                }
            }
            lst = a[ i ] ;
            pos = i ;
        }
    }
    if ( lst == 0 ) {
		for ( i = 1 ; i <= n ; i ++ ) {
        	if ( i % 2 == 1 ) { ans[ i ] = ( i / 2 + 1 ) ; }
            else { ans[ i ] = ans[ i - 1 ] ; }
        }
        if ( ans[ n ] != ans[ n - 1 ] ) { ans[ n ] = ans[ n - 1 ] ; }
        printf ( "%d\n" , ans[ n ] ) ;
        for ( i = 1 ; i <= n ; i ++ ) {
        	printf ( "%d" , ans[ i ] ) ;
	        if ( i == n ) { printf ( "\n" ) ; }
            else { printf ( " " ) ; }
        }
		return ;
    }
    for ( i = 1 ; i <= n ; i ++ ) {
    	if ( a[ i ] != 0 ) { u.push_back ( make_pair ( a[ i ] , i ) ) ; }
    }
    int br = 1 ;
    int mx = 0 ;
    int sz = u.size ( ) ;
    struct tuhla w ;
	for ( i = 1 ; i < sz ; i ++ ) {
    	if ( u[ i ].first == u[ i - 1 ].first ) { br ++ ; }
        else {
        	if ( mx < br ) { mx = br ; }
            w.r = u[ i - 1 ].second ;
            w.l = u[ i - br ] .second ;
            w.val = a[ u[ i - 1 ].second ] ;
            v.push_back ( w ) ;
            br = 1 ;
        }
    }
	if ( mx < br ) { mx = br ; }

	w.r = u[ i - 1 ].second ;
	w.l = u[ i - br ] .second ;
	w.val = a[ u[ i - 1 ].second ] ;
	v.push_back ( w ) ;
    if ( mx > 5 ) { printf ( "-1\n" ) ; return ; }
	k = v.size ( ) ;
    /**
	for ( i = 0 ; i < k ; i ++ ) {
    	printf ( "--- %d %d %d\n" , v[ i ].l , v[ i ].r , v[ i ].val ) ;
    }**/
    if ( n == 200000 ) { printf ( "---- %d\n" , k ) ; }
    for ( i = 0 ; i < 5 ; i ++ ) {
    	if ( v[ ( k - 1 ) ].r + i == ( n - 1 ) ) { continue ; }
    	if ( v[ ( k - 1 ) ].r + i > n ) { continue ; }
    	if ( ( v[ ( k - 1 ) ].r - v[ ( k - 1 ) ].l + 1 ) + i > 5 ) { break ; }
        if ( v[ ( k - 1 ) ].r + i > n ) { continue ; }
		getdp ( k - 1 , v[ ( k - 1 ) ].r + i ) ;
    }
    printf ( "-1\n" ) ;
}

int main ( ) {
	ios::sync_with_stdio ( false ) ;
	cin.tie ( NULL ) ;
	input ( ) ;
	solve ( ) ;
	return 0 ;
}


#include<iostream>
#include<stdio.h>
#include<string>
#include<algorithm>
#include<vector>
#include<map>
#include<cstdio>
using namespace std ;

#define MAXN 200007

int n ;
int k ;
int a[ MAXN ] ;

int dp[ MAXN ][ 7 ] ;
int prv[ MAXN ][ 7 ] ;
int ans[ MAXN ] ;

struct tuhla {
	int l , r ;
    int val ;
};

vector < struct tuhla > v ;
int tot ;

void paint_interval ( int lo , int hi , int beg , int en ) {
	//printf ( "%d %d %d %d\n" , l , r , st , br ) ;
   	/**
    if ( n == 200000 ) {
    	if ( lo <= 2485 && hi >= 2485 ) {
			printf ( "%d %d %d %d\n" , lo , hi , beg , en ) ;
        }
    }
	**/
	int i , j , t ;
	i = lo ;
    while ( 1 ) {
    	if ( i > n ) { return ; }
		for ( j = 2 ; j <= 5 ; j ++ ) {
        	int lft = ( ( hi - i + 1 ) - j ) ;
            int pr = en - beg ;
            if ( 2 * pr <= lft && lft <= 5 * pr ) {
            	break ;
            }
        }
        for ( t = 0 ; t < j ; t ++ ) {
        	ans[ i + t ] = beg ;
        }
        beg ++ ;
        i += j ;
    }
}
void paint_range ( int l , int r , int val ) {
	int i ;
    /**
    if ( n == 200000 ) {
    	if ( l <= 2485 && r >= 2485 ) {
			printf ( "%d %d %d\n" , l , r , val ) ;
        }
    }
	**/
    for ( i = l ; i <= r ; i ++ ) {
    	ans[ i ] = val ;
    }
}

bool getdp ( int id , int r ) {
	//printf ( "%d %d\n" , id , r ) ;
	if ( id == 0 ) {
    	int pr = v[ id ].val - 1 ;
        int i , j , t ;
        for ( i = v[ id ].l ; i >= r - 4 ; i -- ) {
        	if ( a[ i ] != 0 && a[ i ] != v[ id ].val ) { return false ; }
            if ( ( r - i + 1 ) < 2 ) { continue ; }
           	//printf ( " YYYY %d %d\n" , pr , ( i - 1 ) ) ;
        	if ( 2 * pr <= ( i - 1 ) && ( i - 1 ) <= 5 * pr ) {
            	if ( i - 1 >= 1 ) { paint_interval ( 1 , i - 1 , 1 , pr ) ; }
                paint_range ( i , r , v[ id ].val ) ;
                if ( k == 1 ) {
					for ( t = r + 1 ; t < n ; t ++ ) {
						if ( ( t - r ) % 2 == 1 ) { v[ id ].val ++ ; ans[ t ] = v[ id ].val ; }
						else { ans[ t ] = v[ id ].val ; }
					}
					if ( r != n ) { ans[ n ] = v[ id ].val ; }
					printf ( "%d\n" , ans[ n ] ) ;
					for ( t = 1 ; t <= n ; t ++ ) {
						printf ( "%d" , ans[ t ] ) ;
						if ( t == n ) { printf ( "\n" ) ; }
						else { printf ( " " ) ; }
					}
					exit ( 0 ) ;
                }
                return true ;
            }
        }
    }
    else {
    	int pr = v[ id ].val - v[ id - 1 ].val - 1 ;
        int i , j , t ;
        for ( i = v[ id ].l ; i >= r - 4 ; i -- ) {
            //printf ( "~~ %d\n" , i ) ;
        	if ( a[ i ] != 0 && a[ i ] != v[ id ].val ) { return false ; }
            if ( ( r - i + 1 ) < 2 ) { continue ; }
            for ( j = v[ id - 1 ].r ; j <= v[ id - 1 ].l + 4 ; j ++ ) {
            	//printf ( "   ### %d %d\n" , i , j ) ;
            	if ( a[ j ] != 0 && a[ j ] != v[ id - 1 ].val ) { return false ; }
                if ( j >= i ) { break ; }
                int len = ( i - j - 1 ) ;
        		if ( ( 2 * pr <= len && len <= 5 * pr ) && getdp ( id - 1 , j ) == true ) {
            		if ( j + 1 <= i - 1 ) { paint_interval ( j + 1 , i - 1 , v[ id - 1 ].val + 1 , v[ id ].val - 1 ) ; }
                	paint_range ( i , r , v[ id ].val ) ;
                    if ( id == k - 1 ) {
                    	for ( t = r + 1 ; t < n ; t ++ ) {
                        	if ( ( t - r ) % 2 == 1 ) { v[ id ].val ++ ; ans[ t ] = v[ id ].val ; }
                            else { ans[ t ] = v[ id ].val ; }
                        }
                        if ( r != n ) { ans[ n ] = v[ id ].val ; }
                        /**
                        int br = 0 ;
                        for ( t = 1 ; t <= n ; t ++ ) {
                        	if ( ans[ t ] == 0 ) { br ++ ;  }
                        }
						**/
                        //if ( n == 200000 ) { printf ( "TOTAL WRONG --> %d\n" , br ) ; }
                        printf ( "%d\n" , ans[ n ] ) ;
                        for ( t = 1 ; t <= n ; t ++ ) {
                        	printf ( "%d" , ans[ t ] ) ;
	                        if ( t == n ) { printf ( "\n" ) ; }
                            else { printf ( " " ) ; }
                        }
						exit ( 0 ) ;
                    }
    	            return true ;
	            }
            }
        }
    }
    return false ;
}

void input ( ) {
	scanf ( "%d" , &n ) ;
    int i ;
    for ( i = 1 ; i <= n ; i ++ ) {
    	scanf ( "%d" , &a[ i ] ) ;
    }
}

void solve ( ) {
	int i , j , t ;
    int lst = 0 ;
    int pos = 0 ;
    vector < pair < int , int > > u ;
    for ( i = 1 ; i <= n ; i ++ ) {
    	if ( a[ i ] != 0 ) {
        	if ( lst != 0 && lst > a[ i ] ) { printf ( "-1\n" ) ; return ; }
        	if ( lst != 0 && lst == a[ i ] ) {
            	for ( j = pos ; j <= i ; j ++ ) {
                	a[ j ] = a[ i ] ;
                }
            }
            lst = a[ i ] ;
            pos = i ;
        }
    }
    if ( lst == 0 ) {
		for ( i = 1 ; i <= n ; i ++ ) {
        	if ( i % 2 == 1 ) { ans[ i ] = ( i / 2 + 1 ) ; }
            else { ans[ i ] = ans[ i - 1 ] ; }
        }
        if ( ans[ n ] != ans[ n - 1 ] ) { ans[ n ] = ans[ n - 1 ] ; }
        printf ( "%d\n" , ans[ n ] ) ;
        for ( i = 1 ; i <= n ; i ++ ) {
        	printf ( "%d" , ans[ i ] ) ;
	        if ( i == n ) { printf ( "\n" ) ; }
            else { printf ( " " ) ; }
        }
		return ;
    }
    for ( i = 1 ; i <= n ; i ++ ) {
    	if ( a[ i ] != 0 ) { u.push_back ( make_pair ( a[ i ] , i ) ) ; }
    }
    int br = 1 ;
    int mx = 0 ;
    int sz = u.size ( ) ;
    struct tuhla w ;
	for ( i = 1 ; i < sz ; i ++ ) {
    	if ( u[ i ].first == u[ i - 1 ].first ) { br ++ ; }
        else {
        	if ( mx < br ) { mx = br ; }
            w.r = u[ i - 1 ].second ;
            w.l = u[ i - br ] .second ;
            w.val = a[ u[ i - 1 ].second ] ;
            v.push_back ( w ) ;
            br = 1 ;
        }
    }
	if ( mx < br ) { mx = br ; }

	w.r = u[ i - 1 ].second ;
	w.l = u[ i - br ] .second ;
	w.val = a[ u[ i - 1 ].second ] ;
	v.push_back ( w ) ;
    if ( mx > 5 ) { printf ( "-1\n" ) ; return ; }
	k = v.size ( ) ;
    /**
	for ( i = 0 ; i < k ; i ++ ) {
    	//printf ( "--- %d %d %d\n" , v[ i ].l , v[ i ].r , v[ i ].val ) ;
        if ( a[ v[ i ].l ] != a[ v[ i ].r ] ) { printf ( "WTF??\n" ) ; }
        if ( a[ v[ i ].l ] != v[ i ].val ) { printf ( "WTF??\n" ) ; }
        if ( a[ v[ i ].r ] != v[ i ].val ) { printf ( "WTF??\n" ) ; }
    }
    if ( n == 200000 ) { printf ( "---- %d\n" , k ) ; }
	**/

    for ( i = 0 ; i < 5 ; i ++ ) {
    	if ( v[ ( k - 1 ) ].r + i == ( n - 1 ) ) { continue ; }
    	if ( v[ ( k - 1 ) ].r + i > n ) { continue ; }
    	if ( ( v[ ( k - 1 ) ].r - v[ ( k - 1 ) ].l + 1 ) + i > 5 ) { break ; }
        if ( v[ ( k - 1 ) ].r + i > n ) { continue ; }
		getdp ( k - 1 , v[ ( k - 1 ) ].r + i ) ;
    }
    printf ( "-1\n" ) ;
}

int main ( ) {
	ios::sync_with_stdio ( false ) ;
	cin.tie ( NULL ) ;
	input ( ) ;
	solve ( ) ;
	return 0 ;
}


#include<iostream>
#include<stdio.h>
#include<string>
#include<algorithm>
#include<vector>
#include<map>
#include<cstdio>
using namespace std ;

#define MAXN 200007

int n ;
int k ;
int a[ MAXN ] ;

int dp[ MAXN ][ 7 ] ;
int prv[ MAXN ][ 7 ] ;
int ans[ MAXN ] ;

struct tuhla {
	int l , r ;
    int val ;
};

vector < struct tuhla > v ;
int tot ;

void paint_interval ( int l , int r , int st , int br ) {
	//printf ( "%d %d %d %d\n" , l , r , st , br ) ;
    if ( n == 200000 ) {
    	if ( l <= 2485 && r >= 2485 ) {
			printf ( "%d %d %d %d\n" , l , r , st , br ) ;
        }
    }
	int i ;
    for ( i = l ; i <= r ; i ++ ) {
    	if ( st > br ) {
        	ans[ i ] = ans[ i - 1 ] ;
            continue ;
        }
    	if ( ( i - l ) % 2 == 0 ) {
        	ans[ i ] = st ;
            st ++ ;
        }
        else {
        	ans[ i ] = a[ i - 1 ] ;
        }
    }
}
void paint_range ( int l , int r , int val ) {
	int i ;
    if ( n == 200000 ) {
    	if ( l <= 2485 && r >= 2485 ) {
			printf ( "%d %d %d\n" , l , r , val ) ;
        }
    }
    for ( i = l ; i <= r ; i ++ ) {
    	ans[ i ] = val ;
    }
}

bool getdp ( int id , int r ) {
	//printf ( "%d %d\n" , id , r ) ;
	if ( id == 0 ) {
    	int pr = v[ id ].val - 1 ;
        int i , j , t ;
        for ( i = v[ id ].l ; i >= r - 4 ; i -- ) {
        	if ( a[ i ] != 0 && a[ i ] != v[ id ].val ) { continue ; }
            if ( ( r - i + 1 ) < 2 ) { continue ; }
           	//printf ( " YYYY %d %d\n" , pr , ( i - 1 ) ) ;
        	if ( 2 * pr <= ( i - 1 ) && ( i - 1 ) <= 5 * pr ) {
            	if ( i - 1 >= 1 ) { paint_interval ( 1 , i - 1 , 1 , pr ) ; }
                paint_range ( i , r , v[ id ].val ) ;
                if ( k == 1 ) {
					for ( t = r + 1 ; t < n ; t ++ ) {
						if ( ( t - r ) % 2 == 1 ) { v[ id ].val ++ ; ans[ t ] = v[ id ].val ; }
						else { ans[ t ] = v[ id ].val ; }
					}
					if ( r != n ) { ans[ n ] = v[ id ].val ; }
					printf ( "%d\n" , ans[ n ] ) ;
					for ( t = 1 ; t <= n ; t ++ ) {
						printf ( "%d" , ans[ t ] ) ;
						if ( t == n ) { printf ( "\n" ) ; }
						else { printf ( " " ) ; }
					}
					exit ( 0 ) ;
                }
                return true ;
            }
        }
    }
    else {
    	int pr = v[ id ].val - v[ id - 1 ].val - 1 ;
        int i , j , t ;
        for ( i = v[ id ].l ; i >= r - 4 ; i -- ) {
            //printf ( "~~ %d\n" , i ) ;
        	if ( a[ i ] != 0 && a[ i ] != v[ id ].val ) { continue ; }
            if ( ( r - i + 1 ) < 2 ) { continue ; }
            for ( j = v[ id - 1 ].r ; j <= v[ id - 1 ].l + 4 ; j ++ ) {
            	//printf ( "   ### %d %d\n" , i , j ) ;
            	if ( a[ j ] != 0 && a[ j ] != v[ id - 1 ].val ) { continue ; }
                if ( j >= i ) { continue ; }
                int len = ( i - j - 1 ) ;
        		if ( ( 2 * pr <= len && len <= 5 * pr ) && getdp ( id - 1 , j ) == true ) {
            		if ( j + 1 <= i - 1 ) { paint_interval ( j + 1 , i - 1 , v[ id - 1 ].val + 1 , v[ id ].val - 1 ) ; }
                	paint_range ( i , r , v[ id ].val ) ;
                    if ( id == k - 1 ) {
                    	for ( t = r + 1 ; t < n ; t ++ ) {
                        	if ( ( t - r ) % 2 == 1 ) { v[ id ].val ++ ; ans[ t ] = v[ id ].val ; }
                            else { ans[ t ] = v[ id ].val ; }
                        }
                        if ( r != n ) { ans[ n ] = v[ id ].val ; }
                        int br = 0 ;
                        for ( t = 1 ; t <= n ; t ++ ) {
                        	if ( ans[ t ] == 0 ) { br ++ ;  }
                        }
                        if ( n == 200000 ) { printf ( "TOTAL WRONG --> %d\n" , br ) ; }
                        printf ( "%d\n" , ans[ n ] ) ;
                        for ( t = 1 ; t <= n ; t ++ ) {
                        	printf ( "%d" , ans[ t ] ) ;
	                        if ( t == n ) { printf ( "\n" ) ; }
                            else { printf ( " " ) ; }
                        }
						exit ( 0 ) ;
                    }
    	            return true ;
	            }
            }
        }
    }
    return false ;
}

void input ( ) {
	scanf ( "%d" , &n ) ;
    int i ;
    for ( i = 1 ; i <= n ; i ++ ) {
    	scanf ( "%d" , &a[ i ] ) ;
    }
}

void solve ( ) {
	int i , j , t ;
    int lst = 0 ;
    int pos = 0 ;
    vector < pair < int , int > > u ;
    for ( i = 1 ; i <= n ; i ++ ) {
    	if ( a[ i ] != 0 ) {
        	if ( lst != 0 && lst > a[ i ] ) { printf ( "-1\n" ) ; return ; }
        	if ( lst != 0 && lst == a[ i ] ) {
            	for ( j = pos ; j <= i ; j ++ ) {
                	a[ j ] = a[ i ] ;
                }
            }
            lst = a[ i ] ;
            pos = i ;
        }
    }
    if ( lst == 0 ) {
		for ( i = 1 ; i <= n ; i ++ ) {
        	if ( i % 2 == 1 ) { ans[ i ] = ( i / 2 + 1 ) ; }
            else { ans[ i ] = ans[ i - 1 ] ; }
        }
        if ( ans[ n ] != ans[ n - 1 ] ) { ans[ n ] = ans[ n - 1 ] ; }
        printf ( "%d\n" , ans[ n ] ) ;
        for ( i = 1 ; i <= n ; i ++ ) {
        	printf ( "%d" , ans[ i ] ) ;
	        if ( i == n ) { printf ( "\n" ) ; }
            else { printf ( " " ) ; }
        }
		return ;
    }
    for ( i = 1 ; i <= n ; i ++ ) {
    	if ( a[ i ] != 0 ) { u.push_back ( make_pair ( a[ i ] , i ) ) ; }
    }
    int br = 1 ;
    int mx = 0 ;
    int sz = u.size ( ) ;
    struct tuhla w ;
	for ( i = 1 ; i < sz ; i ++ ) {
    	if ( u[ i ].first == u[ i - 1 ].first ) { br ++ ; }
        else {
        	if ( mx < br ) { mx = br ; }
            w.r = u[ i - 1 ].second ;
            w.l = u[ i - br ] .second ;
            w.val = a[ u[ i - 1 ].second ] ;
            v.push_back ( w ) ;
            br = 1 ;
        }
    }
	if ( mx < br ) { mx = br ; }

	w.r = u[ i - 1 ].second ;
	w.l = u[ i - br ] .second ;
	w.val = a[ u[ i - 1 ].second ] ;
	v.push_back ( w ) ;
    if ( mx > 5 ) { printf ( "-1\n" ) ; return ; }
	k = v.size ( ) ;
    /**
	for ( i = 0 ; i < k ; i ++ ) {
    	//printf ( "--- %d %d %d\n" , v[ i ].l , v[ i ].r , v[ i ].val ) ;
        if ( a[ v[ i ].l ] != a[ v[ i ].r ] ) { printf ( "WTF??\n" ) ; }
        if ( a[ v[ i ].l ] != v[ i ].val ) { printf ( "WTF??\n" ) ; }
        if ( a[ v[ i ].r ] != v[ i ].val ) { printf ( "WTF??\n" ) ; }
    }
	**/
    if ( n == 200000 ) { printf ( "---- %d\n" , k ) ; }
    for ( i = 0 ; i < 5 ; i ++ ) {
    	if ( v[ ( k - 1 ) ].r + i == ( n - 1 ) ) { continue ; }
    	if ( v[ ( k - 1 ) ].r + i > n ) { continue ; }
    	if ( ( v[ ( k - 1 ) ].r - v[ ( k - 1 ) ].l + 1 ) + i > 5 ) { break ; }
        if ( v[ ( k - 1 ) ].r + i > n ) { continue ; }
		getdp ( k - 1 , v[ ( k - 1 ) ].r + i ) ;
    }
    printf ( "-1\n" ) ;
}

int main ( ) {
	ios::sync_with_stdio ( false ) ;
	cin.tie ( NULL ) ;
	input ( ) ;
	solve ( ) ;
	return 0 ;
}


#include<iostream>
#include<stdio.h>
#include<string>
#include<algorithm>
#include<vector>
#include<map>
#include<cstdio>
using namespace std ;

#define MAXN 200007

int n ;
int k ;
int a[ MAXN ] ;

int dp[ MAXN ][ 7 ] ;
int prv[ MAXN ][ 7 ] ;
int ans[ MAXN ] ;

struct tuhla {
	int l , r ;
    int val ;
};

vector < struct tuhla > v ;
int tot ;

void paint_interval ( int l , int r , int st , int br ) {
	//printf ( "%d %d %d %d\n" , l , r , st , br ) ;
	int i ;
    for ( i = l ; i <= r ; i ++ ) {
    	if ( st > br ) {
        	ans[ i ] = ans[ i - 1 ] ;
            continue ;
        }
    	if ( ( i - l ) % 2 == 0 ) {
        	ans[ i ] = st ;
            st ++ ;
        }
        else {
        	ans[ i ] = a[ i - 1 ] ;
        }
    }
}
void paint_range ( int l , int r , int val ) {
	int i ;
    for ( i = l ; i <= r ; i ++ ) {
    	ans[ i ] = val ;
    }
}

bool getdp ( int id , int r ) {
	//printf ( "%d %d\n" , id , r ) ;
	if ( id == 0 ) {
    	int pr = v[ id ].val - 1 ;
        int i , j , t ;
        for ( i = v[ id ].l ; i >= r - 4 ; i -- ) {
        	if ( a[ i ] != 0 && a[ i ] != v[ id ].val ) { continue ; }
            if ( ( r - i + 1 ) < 2 ) { continue ; }
           	//printf ( " YYYY %d %d\n" , pr , ( i - 1 ) ) ;
        	if ( 2 * pr <= ( i - 1 ) && ( i - 1 ) <= 5 * pr ) {
            	paint_interval ( 1 , i - 1 , 1 , pr ) ;
                paint_range ( i , r , v[ id ].val ) ;
                if ( k == 1 ) {
					for ( t = r + 1 ; t < n ; t ++ ) {
						if ( ( t - r ) % 2 == 1 ) { v[ id ].val ++ ; ans[ t ] = v[ id ].val ; }
						else { ans[ t ] = v[ id ].val ; }
					}
					if ( r != n ) { ans[ n ] = v[ id ].val ; }
					printf ( "%d\n" , ans[ n ] ) ;
					for ( t = 1 ; t <= n ; t ++ ) {
						printf ( "%d" , ans[ t ] ) ;
						if ( t == n ) { printf ( "\n" ) ; }
						else { printf ( " " ) ; }
					}
					exit ( 0 ) ;
                }
                return true ;
            }
        }
    }
    else {
    	int pr = v[ id ].val - v[ id - 1 ].val - 1 ;
        int i , j , t ;
        for ( i = v[ id ].l ; i >= r - 4 ; i -- ) {
            //printf ( "~~ %d\n" , i ) ;
        	if ( a[ i ] != 0 && a[ i ] != v[ id ].val ) { continue ; }
            if ( ( r - i + 1 ) < 2 ) { continue ; }
            for ( j = v[ id - 1 ].r ; j <= v[ id - 1 ].l + 4 ; j ++ ) {
            	//printf ( "   ### %d %d\n" , i , j ) ;
            	if ( a[ j ] != 0 && a[ j ] != v[ id - 1 ].val ) { continue ; }
                if ( j >= i ) { continue ; }
                int len = ( i - j - 1 ) ;
        		if ( getdp ( id - 1 , j ) == true && ( 2 * pr <= len && len <= 5 * pr ) ) {
            		paint_interval ( j + 1 , i - 1 , v[ id - 1 ].val + 1 , v[ id ].val - 1 ) ;
                	paint_range ( i , r , v[ id ].val ) ;
                    if ( id == k - 1 ) {
                    	for ( t = r + 1 ; t < n ; t ++ ) {
                        	if ( ( t - r ) % 2 == 1 ) { v[ id ].val ++ ; ans[ t ] = v[ id ].val ; }
                            else { ans[ t ] = v[ id ].val ; }
                        }
                        if ( r != n ) { ans[ n ] = v[ id ].val ; }
                        for ( t = 1 ; t <= n ; t ++ ) {
                        	if ( ans[ t ] == 0 ) { printf ( "WA --> %d\n" , t ) ; }
                        }
                        printf ( "%d\n" , ans[ n ] ) ;
                        for ( t = 1 ; t <= n ; t ++ ) {
                        	printf ( "%d" , ans[ t ] ) ;
	                        if ( t == n ) { printf ( "\n" ) ; }
                            else { printf ( " " ) ; }
                        }
						exit ( 0 ) ;
                    }
    	            return true ;
	            }
            }
        }
    }
    return false ;
}

void input ( ) {
	scanf ( "%d" , &n ) ;
    int i ;
    for ( i = 1 ; i <= n ; i ++ ) {
    	scanf ( "%d" , &a[ i ] ) ;
    }
}

void solve ( ) {
	int i , j , t ;
    int lst = 0 ;
    int pos = 0 ;
    vector < pair < int , int > > u ;
    for ( i = 1 ; i <= n ; i ++ ) {
    	if ( a[ i ] != 0 ) {
        	if ( lst != 0 && lst > a[ i ] ) { printf ( "-1\n" ) ; return ; }
        	if ( lst != 0 && lst == a[ i ] ) {
            	for ( j = pos ; j <= i ; j ++ ) {
                	a[ j ] = a[ i ] ;
                }
            }
            lst = a[ i ] ;
            pos = i ;
        }
    }
    if ( lst == 0 ) {
		for ( i = 1 ; i <= n ; i ++ ) {
        	if ( i % 2 == 1 ) { ans[ i ] = ( i / 2 + 1 ) ; }
            else { ans[ i ] = ans[ i - 1 ] ; }
        }
        if ( ans[ n ] != ans[ n - 1 ] ) { ans[ n ] = ans[ n - 1 ] ; }
        printf ( "%d\n" , ans[ n ] ) ;
        for ( i = 1 ; i <= n ; i ++ ) {
        	printf ( "%d" , ans[ i ] ) ;
	        if ( i == n ) { printf ( "\n" ) ; }
            else { printf ( " " ) ; }
        }
		return ;
    }
    for ( i = 1 ; i <= n ; i ++ ) {
    	if ( a[ i ] != 0 ) { u.push_back ( make_pair ( a[ i ] , i ) ) ; }
    }
    int br = 1 ;
    int mx = 0 ;
    int sz = u.size ( ) ;
    struct tuhla w ;
	for ( i = 1 ; i < sz ; i ++ ) {
    	if ( u[ i ].first == u[ i - 1 ].first ) { br ++ ; }
        else {
        	if ( mx < br ) { mx = br ; }
            w.r = u[ i - 1 ].second ;
            w.l = u[ i - br ] .second ;
            w.val = a[ u[ i - 1 ].second ] ;
            v.push_back ( w ) ;
            br = 1 ;
        }
    }
	if ( mx < br ) { mx = br ; }

	w.r = u[ i - 1 ].second ;
	w.l = u[ i - br ] .second ;
	w.val = a[ u[ i - 1 ].second ] ;
	v.push_back ( w ) ;
    if ( mx > 5 ) { printf ( "-1\n" ) ; return ; }
	k = v.size ( ) ;
    /**
	for ( i = 0 ; i < k ; i ++ ) {
    	printf ( "--- %d %d %d\n" , v[ i ].l , v[ i ].r , v[ i ].val ) ;
    }**/
    if ( n == 200000 ) { printf ( "---- %d\n" , k ) ; }
    for ( i = 0 ; i < 5 ; i ++ ) {
    	if ( v[ ( k - 1 ) ].r + i == ( n - 1 ) ) { continue ; }
    	if ( v[ ( k - 1 ) ].r + i > n ) { continue ; }
    	if ( ( v[ ( k - 1 ) ].r - v[ ( k - 1 ) ].l + 1 ) + i > 5 ) { break ; }
        if ( v[ ( k - 1 ) ].r + i > n ) { continue ; }
		getdp ( k - 1 , v[ ( k - 1 ) ].r + i ) ;
    }
    printf ( "-1\n" ) ;
}

int main ( ) {
	ios::sync_with_stdio ( false ) ;
	cin.tie ( NULL ) ;
	input ( ) ;
	solve ( ) ;
	return 0 ;
}


#include <bits/stdc++.h>
#define MP make_pair
#define PB push_back
#define int long long
#define st first
#define nd second
#define rd third
#define FOR(i, a, b) for(int i =(a); i <=(b); ++i)
#define RE(i, n) FOR(i, 1, n)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define REP(i, n) for(int i = 0;i <(n); ++i)
#define VAR(v, i) __typeof(i) v=(i)
#define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#ifdef LOCAL
#define debug(x) {cerr <<#x<<" = " <<x<<"\n"; }
#define debug2(x, y) {cerr <<#x<<" = " <<x<<", "<<#y <<" = " <<y <<"\n";}
#define debug3(x, y, z) {cerr <<#x<<" = " <<x<<", "<<#y <<" = " <<y <<", "<<#z<<" = "<<z<<"\n";}
#define debug4(x, y, z, t) {cerr <<#x<<" = " <<x<<", "<<#y <<" = " <<y <<", "<<#z<<" = "<<z<<", "<<#t <<" = " <<t<<"\n";}
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }}
#else
#define debug(x)
#define debug2(x, y)
#define debug3(x, y, z)
#define debug4(x,y,z,t)
#define debugv(x)
#define cerr if(0)cout
#endif
#define make(type, x) type x; cin>>x;
#define make2(type, x, y) type x, y; cin>>x>>y;
#define make3(type, x, y, z) type x, y, z; cin>>x>>y>>z;
#define make4(type, x, y, z, t) type x, y, z, t; cin>>x>>y>>z>>t;
#define next ____next
#define prev ____prev
#define left ____left
#define hash ____hash
using namespace std;
typedef long long ll;
typedef long double LD;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef vector<ll> VLL;
typedef vector<pair<int, int> > VPII;
typedef vector<pair<ll, ll> > VPLL;

template<class C> void mini(C&a4, C b4){a4=min(a4, b4); }
template<class C> void maxi(C&a4, C b4){a4=max(a4, b4); }
template<class T1, class T2>
ostream& operator<< (ostream &out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")";}
template<class A, class B, class C> struct Triple { A first; B second; C third; };
template<class T> void ResizeVec(T&, vector<int>) {}
template<class T> void ResizeVec(vector<T>& vec, vector<int> sz) {
  vec.resize(sz[0]); sz.erase(sz.begin()); if (sz.empty()) { return; }
  for (T& v : vec) { ResizeVec(v, sz); }
}
int spow(int a, int b, int M) {
  int res = 1;
  while (b) {
    if (b & 1) {
      res = res * a % M;
    }
    a = a * a % M;
    b /= 2;
  }
  return res;
}
int inv(int a, int P) {
  return spow(a, P - 2, P);
}
#undef int
int main() {
#define int long long

  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(10);
  double beg_clock = 1.0 * clock() / CLOCKS_PER_SEC;

  make(int, t);
  RE (tt, t) {
    make4(int, k, l, r, p);
    if (k == 1) {
      cout<<2 % p<<"\n";
      continue;
    }
    if (p == 2) {
      cout<<(k + 1) % 2<<"\n";
      continue;
    }
    if (k % p == 0) {
      if (k % 2 == 0) {
        cout<<1<<"\n";
      } else {
        cout<<inv(spow(2, r - l, p), p)<<"\n";
      }
      continue;
    }
    int pow2l = spow(2, l, p - 1);
    int denres = (spow(k, pow2l, p) + p - 1) % p;
    if (denres == 0) {
      cerr<<"a\n";
      if (k % 2 == 0) {
        cout<<spow(2, r - l + 1, p)<<"\n";
      } else {
        cout<<2<<"\n";
      }
    } else {
      cerr<<"b\n";
      int pow2r = spow(2, r + 1, p - 1);
      int numres = (spow(k, pow2r, p) + p - 1) % p;
      int res = numres * inv(denres, p);
      if (k % 2 == 1) {
        debug4(pow2r, numres, denres, res);
        cerr<<"c\n";
        res = res * inv(spow(2, r - l, p), p) % p;
      }
      cout<<res<<"\n";
    }
  }

  return 0;
}


#include <bits/stdc++.h>
#define MP make_pair
#define PB push_back
#define int long long
#define st first
#define nd second
#define rd third
#define FOR(i, a, b) for(int i =(a); i <=(b); ++i)
#define RE(i, n) FOR(i, 1, n)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define REP(i, n) for(int i = 0;i <(n); ++i)
#define VAR(v, i) __typeof(i) v=(i)
#define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#ifdef LOCAL
#define debug(x) {cerr <<#x<<" = " <<x<<"\n"; }
#define debug2(x, y) {cerr <<#x<<" = " <<x<<", "<<#y <<" = " <<y <<"\n";}
#define debug3(x, y, z) {cerr <<#x<<" = " <<x<<", "<<#y <<" = " <<y <<", "<<#z<<" = "<<z<<"\n";}
#define debug4(x, y, z, t) {cerr <<#x<<" = " <<x<<", "<<#y <<" = " <<y <<", "<<#z<<" = "<<z<<", "<<#t <<" = " <<t<<"\n";}
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }}
#else
#define debug(x)
#define debug2(x, y)
#define debug3(x, y, z)
#define debug4(x,y,z,t)
#define debugv(x)
#define cerr if(0)cout
#endif
#define make(type, x) type x; cin>>x;
#define make2(type, x, y) type x, y; cin>>x>>y;
#define make3(type, x, y, z) type x, y, z; cin>>x>>y>>z;
#define make4(type, x, y, z, t) type x, y, z, t; cin>>x>>y>>z>>t;
#define next ____next
#define prev ____prev
#define left ____left
#define hash ____hash
using namespace std;
typedef long long ll;
typedef long double LD;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef vector<ll> VLL;
typedef vector<pair<int, int> > VPII;
typedef vector<pair<ll, ll> > VPLL;

template<class C> void mini(C&a4, C b4){a4=min(a4, b4); }
template<class C> void maxi(C&a4, C b4){a4=max(a4, b4); }
template<class T1, class T2>
ostream& operator<< (ostream &out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")";}
template<class A, class B, class C> struct Triple { A first; B second; C third; };
template<class T> void ResizeVec(T&, vector<int>) {}
template<class T> void ResizeVec(vector<T>& vec, vector<int> sz) {
  vec.resize(sz[0]); sz.erase(sz.begin()); if (sz.empty()) { return; }
  for (T& v : vec) { ResizeVec(v, sz); }
}
int spow(int a, int b, int M) {
  int res = 1;
  while (b) {
    if (b & 1) {
      res = res * a % M;
    }
    a = a * a % M;
    b /= 2;
  }
  return res;
}
int inv(int a, int P) {
  return spow(a, P - 2, P);
}
#undef int
int main() {
#define int long long

  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(10);
  double beg_clock = 1.0 * clock() / CLOCKS_PER_SEC;

  make(int, t);
  RE (tt, t) {
    make4(int, k, l, r, p);
    if (k == 1) {
      cout<<2 % p<<"\n";
      continue;
    }
    if (p == 2) {
      cout<<(k + 1) % 2<<"\n";
      continue;
    }
    if (k % p == 0) {
      cout<<1<<"\n";
      continue;
    }
    int pow2l = spow(2, l, p - 1);
    int denres = (spow(k, pow2l, p) + p - 1) % p;
    if (denres == 0) {
      cerr<<"a\n";
      cout<<spow(2, r - l + 1, p)<<"\n";

    } else {
      cerr<<"b\n";
      int pow2r = spow(2, r + 1, p - 1);
      int numres = (spow(k, pow2r, p) + p - 1) % p;
      int res = numres * inv(denres, p);
      if (k % 2 == 1) {
        debug4(pow2r, numres, denres, res);
        cerr<<"c\n";
        res = res * inv(spow(2, r - l, p), p) % p;
      }
      cout<<res<<"\n";
    }
  }

  return 0;
}


#include <bits/stdc++.h>
#define MP make_pair
#define PB push_back
#define int long long
#define st first
#define nd second
#define rd third
#define FOR(i, a, b) for(int i =(a); i <=(b); ++i)
#define RE(i, n) FOR(i, 1, n)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define REP(i, n) for(int i = 0;i <(n); ++i)
#define VAR(v, i) __typeof(i) v=(i)
#define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#ifdef LOCAL
#define debug(x) {cerr <<#x<<" = " <<x<<"\n"; }
#define debug2(x, y) {cerr <<#x<<" = " <<x<<", "<<#y <<" = " <<y <<"\n";}
#define debug3(x, y, z) {cerr <<#x<<" = " <<x<<", "<<#y <<" = " <<y <<", "<<#z<<" = "<<z<<"\n";}
#define debug4(x, y, z, t) {cerr <<#x<<" = " <<x<<", "<<#y <<" = " <<y <<", "<<#z<<" = "<<z<<", "<<#t <<" = " <<t<<"\n";}
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }}
#else
#define debug(x)
#define debug2(x, y)
#define debug3(x, y, z)
#define debug4(x,y,z,t)
#define debugv(x)
#define cerr if(0)cout
#endif
#define make(type, x) type x; cin>>x;
#define make2(type, x, y) type x, y; cin>>x>>y;
#define make3(type, x, y, z) type x, y, z; cin>>x>>y>>z;
#define make4(type, x, y, z, t) type x, y, z, t; cin>>x>>y>>z>>t;
#define next ____next
#define prev ____prev
#define left ____left
#define hash ____hash
using namespace std;
typedef long long ll;
typedef long double LD;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef vector<ll> VLL;
typedef vector<pair<int, int> > VPII;
typedef vector<pair<ll, ll> > VPLL;

template<class C> void mini(C&a4, C b4){a4=min(a4, b4); }
template<class C> void maxi(C&a4, C b4){a4=max(a4, b4); }
template<class T1, class T2>
ostream& operator<< (ostream &out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")";}
template<class A, class B, class C> struct Triple { A first; B second; C third; };
template<class T> void ResizeVec(T&, vector<int>) {}
template<class T> void ResizeVec(vector<T>& vec, vector<int> sz) {
  vec.resize(sz[0]); sz.erase(sz.begin()); if (sz.empty()) { return; }
  for (T& v : vec) { ResizeVec(v, sz); }
}
int spow(int a, int b, int M) {
  int res = 1;
  while (b) {
    if (b & 1) {
      res = res * a % M;
    }
    a = a * a % M;
    b /= 2;
  }
  return res;
}
int inv(int a, int P) {
  return spow(a, P - 2, P);
}
#undef int
int main() {
#define int long long

  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(10);
  double beg_clock = 1.0 * clock() / CLOCKS_PER_SEC;

  make(int, t);
  RE (tt, t) {
    make4(int, k, l, r, p);
    if (k == 1) {
      cout<<2 % p<<"\n";
      continue;
    }
    if (p == 2) {
      cout<<(k + 1) % 2<<"\n";
      continue;
    }
    int pow2l = spow(2, l, p - 1);
    int denres = (spow(k, pow2l, p) + p - 1) % p;
    if (denres == 0) {
      cout<<spow(2, r - l + 1, p)<<"\n";

    } else {
      int pow2r = spow(2, r + 1, p - 1);
      int numres = (spow(k, pow2r, p) + p - 1) % p;
      int res = numres * inv(denres, p);
      if (k % 2 == 1) {
        res = res * inv(spow(2, r - l, p), p);
      }
      cout<<res<<"\n";
    }
  }

  return 0;
}


//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;
}
*/
LL qpot(LL a, LL b, LL MOD)
{
	assert(b >= 0);
	a = a % MOD;
	LL res = 1;
	while(b)
	{
		if(b & 1)
		{
			res = (res * a) % MOD;
		}
		a = (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) % p;

	//cout << "x : " << x << "\n";
	LL res = 0;
	if(x == 1)
	{
		res = qpot(2, r - l + 1, p) % p;
	}
	else
	{
		if(x == 0)
		{
			res = 1;
		}
		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;
	}
	assert(res >= 0 && res < p);
	cout << res << "\n";
	return;
}

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

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


//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;
}

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

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

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

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


//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 unsigned long long LL;

#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 qpot(LL a, LL b, LL MOD)
{
	assert(b >= 0 && a >= 0);
	LL res = 1;
	while(b)
	{
		if(b & 1)
		{
			res = (res * a) % MOD;
		}
		a = (a * a) % MOD;
		b = b >> 1;
	}
	return res;
}

void jebaj()
{
	LL k, l, r, p;
	cin >> k >> l >> r >> p;
	if(p == 2ll)
	{
		if(k & 1ll)
		{
			cout << 0 << "\n";
			return;
		}
		else
		{
			cout << 1 << "\n";
			return;
		}
	}

	LL x = qpot(k, qpot(2ll, l, p - 1ll), p) % p;

	//cout << "x : " << x << "\n";
	LL res = 0;
	if(x == 1ll)
	{
		res = qpot(2ll, r - l + 1ll, p) % p;
	}
	else
	{
		if(x == 0)
		{
			res = 1ll;
		}
		else
		{
			res = (qpot(x, qpot(2ll, r - l + 1ll, p - 1ll), p) + p - 1ll) % p;
			res = (res * qpot((x + p - 1ll) % p, p - 2ll, p)) % p;
		}
	}
	if(k & 1ll)
	{
		res = (res * qpot(qpot(2ll, r - l, p), p - 2ll, p)) % p;
	}
	assert(res >= 0 && res < p);
	cout << res << "\n";
	return;
}

int main()
{
	//IOSYNC;
	int t;
	cin >> t;

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


//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;

#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 qpot(LL a, LL b, LL MOD)
{
	assert(b >= 0 && a >= 0);
	LL res = 1;
	while(b)
	{
		if(b & 1ll)
		{
			res = (res * a) % MOD;
		}
		a = (a * a) % MOD;
		b = b >> 1ll;
	}
	assert(res >= 0 && res < MOD);
	return res;
}

void jebaj()
{
	LL k, l, r, p;
	cin >> k >> l >> r >> p;
	if(p == 2ll)
	{
		if(k & 1ll)
		{
			cout << 0 << "\n";
			return;
		}
		else
		{
			cout << 1 << "\n";
			return;
		}
	}

	LL x = qpot(k, qpot(2ll, l, p - 1ll), p) % p;

	//cout << "x : " << x << "\n";
	LL res = 0;
	if(x == 1ll)
	{
		res = qpot(2ll, r - l + 1ll, p) % p;
	}
	else
	{
		if(x == 0)
		{
			res = 1ll;
		}
		else
		{
			res = (qpot(x, qpot(2ll, r - l + 1ll, p - 1ll), p) + p - 1ll) % p;
			res = (res * qpot((x + p - 1ll) % p, p - 2ll, p)) % p;
		}
	}
	if(k & 1ll)
	{
		res = (res * qpot(qpot(2ll, r - l, p), p - 2ll, p)) % p;
	}
	assert(res >= 0 && res < p);
	cout << res << "\n";
	return;
}

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

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


//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;
}

LL qpot(LL a, LL b, LL MOD)
{
	LL res = 1;
	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;
	LL x = qpot(k, qpot(2, l, p - 1), p);
	if(p == 2)
	{
		if(k % 2)
		{
			cout << "0\n";
			return;
		}
		else
		{
			cout << "1\n";
			return;
		}
	}
	if(x == 1)
	{
		LL res = qpot(2, r - l + 1, p);
		cout << res << "\n";
		return;
	}
	LL res = qpot(x, qpot(2, r - l + 1, p - 1), p) - 1;
	res = (res * qpot(x - 1, p - 2, p)) % p;
	if(k % 2)
	{
		res = (res * qpot(qpot(2, r - l, p), p - 2, p)) % p;
	}
	cout << res << "\n";
	return;
}

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

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


//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 unsigned long long LL;

#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 qpot(LL a, LL b, LL MOD)
{
	assert(b >= 0 && a >= 0);
	LL res = 1;
	while(b)
	{
		if(b & 1)
		{
			res = (res * a) % MOD;
		}
		a = (a * a) % MOD;
		b = b >> 1;
	}
	return res;
}

void jebaj()
{
	LL k, l, r, p;
	cin >> k >> l >> r >> p;
	if(p == 2ll)
	{
		if(k & 1ll)
		{
			cout << 0 << "\n";
			return;
		}
		else
		{
			cout << 1 << "\n";
			return;
		}
	}

	LL x = qpot(k, qpot(2ll, l, p - 1ll), p) % p;

	//cout << "x : " << x << "\n";
	LL res = 0;
	if(x == 1ll)
	{
		res = qpot(2ll, r - l + 1ll, p) % p;
	}
	else
	{
		if(x == 0)
		{
			res = 1ll;
		}
		else
		{
			res = (qpot(x, qpot(2ll, r - l + 1ll, p - 1ll), p) + p - 1ll) % p;
			res = (res * qpot((x + p - 1ll) % p, p - 2ll, p)) % p;
		}
	}
	if(k & 1ll)
	{
		res = (res * qpot(qpot(2ll, r - l, p), p - 2ll, p)) % p;
	}
	assert(res >= 0 && res < p);
	cout << res << "\n";
	return;
}

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

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


//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 unsigned long long LL;

#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 qpot(LL a, LL b, LL MOD)
{
	assert(b >= 0 && a >= 0);
	LL res = 1;
	while(b)
	{
		if(b & 1)
		{
			res = (res * a) % MOD;
		}
		a = (a * a) % MOD;
		b = b >> 1;
	}
	return res;
}

void jebaj()
{
	LL k, l, r, p;
	cin >> k >> l >> r >> p;
	if(p == 2ll)
	{
		if(k & 1ll)
		{
			cout << 0 << endl;
			return;
		}
		else
		{
			cout << 1 << endl;
			return;
		}
	}

	LL x = qpot(k, qpot(2ll, l, p - 1ll), p) % p;

	//cout << "x : " << x << "\n";
	LL res = 0;
	if(x == 1ll)
	{
		res = qpot(2ll, r - l + 1ll, p) % p;
	}
	else
	{
		if(x == 0)
		{
			res = 1ll;
		}
		else
		{
			res = (qpot(x, qpot(2ll, r - l + 1ll, p - 1ll), p) + p - 1ll) % p;
			res = (res * qpot((x + p - 1ll) % p, p - 2ll, p)) % p;
		}
	}
	if(k & 1ll)
	{
		res = (res * qpot(qpot(2ll, r - l, p), p - 2ll, p)) % p;
	}
	assert(res >= 0 && res < p);
	cout << res << endl;
	return;
}

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

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


//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;
	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);
	if(x == 0)
	{
		cout << "1\n";
		return;
	}
	if(x == 1)
	{
		LL res = qpot(2, r - l + 1, p);
		cout << res << "\n";
		return;
	}
	LL 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)
	{
		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();
	}
}


//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;

#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 qpot(LL a, LL b, LL MOD)
{
	assert(b >= 0 && a >= 0);
	LL res = 1;
	while(b)
	{
		if(b & 1)
		{
			res = (res * a) % MOD;
		}
		a = (a * a) % MOD;
		b = b >> 1;
	}
	assert(res >= 0 && res < MOD);
	return res;
}

void jebaj()
{
	LL k, l, r, p;
	cin >> k >> l >> r >> p;
	if(p == 2ll)
	{
		if(k & 1ll)
		{
			cout << 0 << "\n";
			return;
		}
		else
		{
			cout << 1 << "\n";
			return;
		}
	}

	LL x = qpot(k, qpot(2ll, l, p - 1ll), p) % p;

	//cout << "x : " << x << "\n";
	LL res = 0;
	if(x == 1ll)
	{
		res = qpot(2ll, r - l + 1ll, p) % p;
	}
	else
	{
		if(x == 0)
		{
			res = 1ll;
		}
		else
		{
			res = (qpot(x, qpot(2ll, r - l + 1ll, p - 1ll), p) + p - 1ll) % p;
			res = (res * qpot((x + p - 1ll) % p, p - 2ll, p)) % p;
		}
	}
	if(k & 1ll)
	{
		res = (res * qpot(qpot(2ll, r - l, p), p - 2ll, p)) % p;
	}
	assert(res >= 0 && res < p);
	cout << res << "\n";
	return;
}

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

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


//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;
	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;
	LL x = qpot(k, qpot(2, l, p - 1), p);
	if(p == 2)
	{
		if(k % 2)
		{
			cout << "0\n";
			return;
		}
		else
		{
			cout << "1\n";
			return;
		}
	}
	if(x == 1)
	{
		LL res = qpot(2, r - l + 1, p);
		cout << res << "\n";
		return;
	}
	LL 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)
	{
		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();
	}
}


//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 unsigned long long LL;

#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 qpot(LL a, LL b, LL MOD)
{
	assert(b >= 0 && a >= 0);
	LL res = 1;
	while(b)
	{
		if(b & 1)
		{
			res = (res * a) % MOD;
		}
		a = (a * a) % MOD;
		b = b >> 1;
	}
	return res;
}

void jebaj()
{
	LL k, l, r, p;
	cin >> k >> l >> r >> p;
	if(p == 2ll)
	{
		if(k & 1ll)
		{
			cout << 0 << endl;
			return;
		}
		else
		{
			cout << 1 << endl;
			return;
		}
	}

	LL x = qpot(k, qpot(2ll, l, p - 1ll), p) % p;

	//cout << "x : " << x << "\n";
	LL res = 0;
	if(x == 1ll)
	{
		res = qpot(2ll, r - l + 1ll, p) % p;
	}
	else
	{
		if(x == 0)
		{
			res = 1ll;
		}
		else
		{
			res = (qpot(x, qpot(2ll, r - l + 1ll, p - 1ll), p) + p - 1ll) % p;
			res = (res * qpot((x + p - 1ll) % p, p - 2ll, p)) % p;
		}
	}
	if(k & 1ll)
	{
		res = (res * qpot(qpot(2ll, r - l, p), p - 2ll, p)) % p;
	}
	assert(res >= 0 && res < p);
	cout << res << endl;
	return;
}

int main()
{
	//IOSYNC;
	int t;
	cin >> t;

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


//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;

#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 qpot(LL a, LL b, LL MOD)
{
	assert(b >= 0 && a >= 0);
	LL res = 1;
	while(b > 0)
	{
		if(b % 2ll == 1ll)
		{
			res = (res * a) % MOD;
		}
		a = (a * a) % MOD;
		b = b / 2ll;
	}
	assert(res >= 0 && res < MOD);
	return res;
}

void jebaj()
{
	LL k, l, r, p;
	cin >> k >> l >> r >> p;
	if(p == 2ll)
	{
		if(k & 1ll)
		{
			cout << 0 << "\n";
			return;
		}
		else
		{
			cout << 1 << "\n";
			return;
		}
	}

	LL x = qpot(k, qpot(2ll, l, p - 1ll), p) % p;

	//cout << "x : " << x << "\n";
	LL res = 0;
	if(x == 1ll)
	{
		res = qpot(2ll, (r - l + 1ll) % (p - 1ll), p) % p;
	}
	else
	{
		if(x == 0)
		{
			res = 1ll;
		}
		else
		{
			res = (qpot(x, qpot(2ll, r - l + 1ll, p - 1ll), p) + p - 1ll) % p;
			res = (res * qpot((x + p - 1ll) % p, p - 2ll, p)) % p;
		}
	}
	if(k & 1ll)
	{
		res = (res * qpot(qpot(2ll, (r - l) % (p - 1ll), p), p - 2ll, p)) % p;
	}
	assert(res >= 0 && res < p);
	cout << res << "\n";
	return;
}

int main()
{
	IOSYNC;
	LL t;
	cin >> t;

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


//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;
}
*/
LL qpot(LL a, LL b, LL MOD)
{
	assert(b >= 0);
	a = a % MOD;
	LL res = 1;
	while(b)
	{
		if(b & 1)
		{
			res = (res * a) % MOD;
		}
		a = (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) % p;

	//cout << "x : " << x << "\n";
	LL res = 0;
	if(x == 1)
	{
		res = qpot(2, r - l + 1, p) % p;
	}
	else
	{
		if(x == 0)
		{
			res = 1;
		}
		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();
	}
}


//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;

#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 qpot(LL a, LL b, LL MOD)
{
	assert(b >= 0 && a >= 0);
	LL res = 1;
	while(b > 0)
	{
		if(b % 2ll == 1ll)
		{
			res = (res * a) % MOD;
		}
		a = (a * a) % MOD;
		b = b / 2ll;
	}
	assert(res >= 0 && res < MOD);
	return res;
}

void jebaj()
{
	LL k, l, r, p;
	cin >> k >> l >> r >> p;
	r = r - l;
	if(p == 2ll)
	{
		if(k & 1ll)
		{
			cout << 0 << "\n";
			return;
		}
		else
		{
			cout << 1 << "\n";
			return;
		}
	}

	LL x = qpot(k, qpot(2ll, l, p - 1ll), p) % p;
	l = 0;
	//cout << "x : " << x << "\n";
	LL res = 0;
	if(x == 1ll)
	{
		res = qpot(2ll, (r + 1ll) % (p - 1ll), p) % p;
	}
	else
	{
		if(x == 0)
		{
			res = 1ll;
		}
		else
		{
			res = (qpot(x, qpot(2ll, r + 1ll, p - 1ll), p) + p - 1ll) % p;
			res = (res * qpot((x + p - 1ll) % p, p - 2ll, p)) % p;
		}
	}
	if(k & 1ll)
	{
		res = (res * qpot(qpot(2ll, r % (p - 1ll), p), p - 2ll, p)) % p;
	}
	assert(res >= 0 && res < p);
	cout << res << "\n";
	return;
}

int main()
{
	IOSYNC;
	LL t;
	cin >> t;

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


//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;
	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;
	LL x = qpot(k, qpot(2, l, p - 1), p);
	if(p == 2)
	{
		if(k % 2)
		{
			cout << "0\n";
			return;
		}
		else
		{
			cout << "1\n";
			return;
		}
	}
	if(x == 1)
	{
		LL res = qpot(2, r - l + 1, p);
		cout << res << "\n";
		return;
	}
	LL res = qpot(x, qpot(2, r - l + 1, p - 1), p) - 1;
	res = (res * qpot(x - 1, p - 2, p)) % p;
	if(k % 2)
	{
		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();
	}
}


//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)
{
	a = a % MOD;
	LL res = 1;
	if(b == 0) return 1;
	if(a == 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);

	//cout << "x : " << x << "\n";
	LL res = 0;
	if(x == 1)
	{
		res = qpot(2, r - l + 1, p);
	}
	else
	{
		if(x == 0)
		{
			res = 1;
		}
		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();
	}
}


//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;
}
*/
LL qpot(LL a, LL b, LL MOD)
{
	a = a % MOD;
	LL res = 1;
	if(a == 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);

	//cout << "x : " << x << "\n";
	LL res = 0;
	if(x == 1)
	{
		res = qpot(2, r - l + 1, p);
	}
	else
	{
		if(x == 0)
		{
			res = 1;
		}
		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();
	}
}


//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;

#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 qpot(LL a, LL b, LL MOD)
{
	assert(b >= 0 && a >= 0);
	LL res = 1;
	while(b > 0)
	{
		if(b % 2ll == 1ll)
		{
			res = (res * a) % MOD;
		}
		a = (a * a) % MOD;
		b = b / 2ll;
	}
	assert(res >= 0 && res < MOD);
	return res;
}

void jebaj()
{
	LL k, l, r, p;
	cin >> k >> l >> r >> p;
	r = r - l;
	if(p == 2ll)
	{
		if(k & 1ll)
		{
			cout << 0 << "\n";
			return;
		}
		else
		{
			cout << 1 << "\n";
			return;
		}
	}

	LL x = qpot(k, qpot(2ll, l, p - 1ll) + p - 1, p) % p;
	l = 0;
	//cout << "x : " << x << "\n";
	LL res = 0;
	if(x == 1ll)
	{
		res = qpot(2ll, (r + 1ll) % (p - 1ll), p) % p;
	}
	else
	{
		if(x == 0)
		{
			res = 1ll;
		}
		else
		{
			res = (qpot(x, qpot(2ll, r + 1ll, p - 1ll), p) + p - 1ll) % p;
			res = (res * qpot((x + p - 1ll) % p, p - 2ll, p)) % p;
		}
	}
	if(k & 1ll)
	{
		//cout << res << "\n";
		res = (res * qpot(qpot(2ll, r % (p - 1ll), p), p - 2ll, p)) % p;
		//cout << res << "\n";
	}
	assert(res >= 0 && res < p);
	cout << res << "\n";
	return;
}

int main()
{
	IOSYNC;
	LL t;
	cin >> t;

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


//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;

#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 qpot(LL a, LL b, LL MOD)
{
	assert(b >= 0 && a >= 0);
	LL res = 1;
	while(b)
	{
		if(b % 2ll)
		{
			res = (res * a) % MOD;
		}
		a = (a * a) % MOD;
		b = b / 2ll;
	}
	assert(res >= 0 && res < MOD);
	return res;
}

void jebaj()
{
	LL k, l, r, p;
	cin >> k >> l >> r >> p;
	if(p == 2ll)
	{
		if(k & 1ll)
		{
			cout << 0 << "\n";
			return;
		}
		else
		{
			cout << 1 << "\n";
			return;
		}
	}

	LL x = qpot(k, qpot(2ll, l, p - 1ll), p) % p;

	//cout << "x : " << x << "\n";
	LL res = 0;
	if(x == 1ll)
	{
		res = qpot(2ll, r - l + 1ll, p) % p;
	}
	else
	{
		if(x == 0)
		{
			res = 1ll;
		}
		else
		{
			res = (qpot(x, qpot(2ll, r - l + 1ll, p - 1ll), p) + p - 1ll) % p;
			res = (res * qpot((x + p - 1ll) % p, p - 2ll, p)) % p;
		}
	}
	if(k & 1ll)
	{
		res = (res * qpot(qpot(2ll, r - l, p), p - 2ll, p)) % p;
	}
	assert(res >= 0 && res < p);
	cout << res << "\n";
	return;
}

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

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


//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);
	if(x == 0)
	{
		cout << "1\n";
		return;
	}
	if(x == 1)
	{
		LL res = qpot(2, r - l + 1, p);
		cout << res << "\n";
		return;
	}
	LL 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();
	}
}


//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 unsigned long long LL;

#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;
}
*/
LL qpot(LL a, LL b, LL MOD)
{
	assert(b >= 0 && a >= 0);
	LL res = 1;
	while(b)
	{
		if(b & 1)
		{
			res = (res * a) % MOD;
		}
		a = (a * a) % MOD;
		b = b >> 1;
	}
	return res;
}

void jebaj()
{
	LL k, l, r, p;
	cin >> k >> l >> r >> p;
	if(p == 2ll)
	{
		if(k & 1)
		{
			cout << "0\n";
			return;
		}
		else
		{
			cout << "1\n";
			return;
		}
	}

	LL x = qpot(k, qpot(2ll, l, p - 1ll), p) % p;

	//cout << "x : " << x << "\n";
	LL res = 0;
	if(x == 1ll)
	{
		res = qpot(2ll, r - l + 1ll, p) % p;
	}
	else
	{
		if(x == 0)
		{
			res = 1ll;
		}
		else
		{
			res = (qpot(x, qpot(2ll, r - l + 1ll, p - 1ll), p) + p - 1ll) % p;
			res = (res * qpot((x + p - 1ll) % p, p - 2ll, p)) % p;
		}
	}
	if(k & 1ll)
	{
		res = (res * qpot(qpot(2ll, r - l, p), p - 2ll, p)) % p;
	}
	assert(res >= 0 && res < p);
	cout << res << endl;
	return;
}

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

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


//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
	{
		if(x == 0)
		{
			res = 1;
		}
		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();
	}
}


//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 unsigned long long LL;

#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;
}
*/
LL qpot(LL a, LL b, LL MOD)
{
	assert(b >= 0 && a >= 0);
	LL res = 1;
	while(b)
	{
		if(b & 1)
		{
			res = (res * a) % MOD;
		}
		a = (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) % p;

	//cout << "x : " << x << "\n";
	LL res = 0;
	if(x == 1)
	{
		res = qpot(2, r - l + 1, p) % p;
	}
	else
	{
		if(x == 0)
		{
			res = 1;
		}
		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;
	}
	assert(res >= 0 && res < p);
	cout << res << "\n";
	return;
}

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

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


//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;

#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 qpot(LL a, LL b, LL MOD)
{
	assert(b >= 0 && a >= 0);
	LL res = 1;
	while(b)
	{
		if(b % 2ll)
		{
			res = (res * a) % MOD;
		}
		a = (a * a) % MOD;
		b = b / 2ll;
	}
	assert(res >= 0 && res < MOD);
	return res;
}

void jebaj()
{
	LL k, l, r, p;
	cin >> k >> l >> r >> p;
	if(p == 2ll)
	{
		if(k & 1ll)
		{
			cout << 0 << "\n";
			return;
		}
		else
		{
			cout << 1 << "\n";
			return;
		}
	}

	LL x = qpot(k, qpot(2ll, l, p - 1ll), p) % p;

	//cout << "x : " << x << "\n";
	LL res = 0;
	if(x == 1ll)
	{
		res = qpot(2ll, (r - l + 1ll) % (p - 1), p) % p;
	}
	else
	{
		if(x == 0)
		{
			res = 1ll;
		}
		else
		{
			res = (qpot(x, qpot(2ll, r - l + 1ll, p - 1ll), p) + p - 1ll) % p;
			res = (res * qpot((x + p - 1ll) % p, p - 2ll, p)) % p;
		}
	}
	if(k & 1ll)
	{
		res = (res * qpot(qpot(2ll, (r - l) % (p - 1), p), p - 2ll, p)) % p;
	}
	assert(res >= 0 && res < p);
	cout << res << "\n";
	return;
}

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

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


//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;
}
*/
LL qpot(LL a, LL b, LL MOD)
{
	a = a % MOD;
	LL res = 1;
	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);

	//cout << "x : " << x << "\n";
	LL res = 0;
	if(x == 1)
	{
		res = qpot(2, r - l + 1, p);
	}
	else
	{
		if(x == 0)
		{
			res = 1;
		}
		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();
	}
}


//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;
}
*/
LL qpot(LL a, LL b, LL MOD)
{
	a = a % MOD;
	LL res = 1;
	if(b == 0) return 1;
	if(a == 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);

	//cout << "x : " << x << "\n";
	LL res = 0;
	if(x == 1)
	{
		res = qpot(2, r - l + 1, p);
	}
	else
	{
		if(x == 0)
		{
			res = 1;
		}
		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();
	}
}