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

const unsigned long long MAXN = 1e6 + 8 ;
const unsigned long long MAXM = 1e5 + 8 ;
const unsigned long long MOD = 1e9 + 7 ; 

unsigned long long T , n , m , v , c[MAXM] , d[MAXM] , k[MAXN] ;
bool f = true ;

int main()
{
	freopen( "assign.in" , "r" , stdin ) ;
	freopen( "assign.out" , "w" , stdout ) ;
	
	cin >> T ;
	
	while ( T-- )
	{
		f = true ;
		
		cin >> n >> m >> v ;
		memset( k , 0 , sizeof(k) ) ;
		
		for ( unsigned long long i = 1 ; i <= m ; i++ ) 
		{
			cin >> c[i] >> d[i] ;
			
			if ( n < MAXN )
			{
				if ( k[c[i]] > 0 && d[i] != k[c[i]] ) 
				{
					cout << 0 << endl ;
					f = 0 ;
					break ;
				}
				
				k[c[i]] = d[i] ;
			}
		}
		
		if ( !f ) continue ;
		
		if ( n >= MAXN ) 
		{
			cout << 0 << endl ;
			continue ;
		}
		
		unsigned long long cnt = 1 ;
		
		for ( unsigned long long i = 1 ; i <= n - 1 ; i++ )
		{
			if ( k[i] == 0 && k[ i + 1 ] == 0 ) cnt = ( cnt * v % MOD * v % MOD ) ;
			if ( k[i] == 0 && k[ i + 1 ] > 0 ) cnt = ( cnt * v % MOD * v % MOD ) ;
			if ( k[i] > 0 && k[ i + 1 ] == 0 ) cnt = ( cnt * v % MOD * v % MOD ) ;
			if ( k[i] > 0 && k[ i + 1 ] > 0 ) cnt = ( cnt * ( v % MOD * v % MOD - v % MOD + 1 ) % MOD ) ;
		}
		
		cout << cnt << endl ; 
	}
	
	fclose( stdin ) ;
	fclose( stdout ) ;
	
	return 0 ;
} 
