#include <iostream>
#include <cstring>
#include <bitset>
#include <cstdio>
#include <cmath>
using namespace std;
int read()
{
	int x = 0, f = 1;
	char c = getchar();
	while(!isdigit(c))
	{
		if(c == '-')
		{
			f = -1;
		}
		c = getchar();
	}
	while(isdigit(c))
	{
		x = x * 10 + c - 48;
		c = getchar();
	}
	return x * f;
}
const int maxn = 1e6 + 10;
int k[maxn], h[maxn];
const int mod = 1e9 + 7;
int pow(int a, int b)
{
	int ans = 1;
	for (; b; b >>= 1)
	{
		if(b & 1)
		{
			ans = 1ll * ans * a % mod;
		}
		a = 1ll * a * a % mod;
	}
	return ans;
}
int main()
{
	int T = read();
	while(T--)
	{
		int n = read(), p = read();
		if(p == 1)
		{
			for (int i = 1; i <= n; ++i)
			{
				int x = read();
			}
			printf("%d\n", n % 2);
		}
		else
		{
			int cmax = 0;
			for (int i = 1; i <= n; ++i)
			{
				k[i] = read();
				h[k[i]] ^= 1;
			}
			for (int i = 1; i <= n; ++i)
			{
				if(h[k[i]])
				{
					cmax = std::max(cmax, k[i]);
				}
			}
			long long ans1 = 0, ans2;
			for (int i = 0; i < cmax; ++i)
			{
				if(h[i])
				{
					ans1 += pow(p, i);
					ans1 %= mod;
				}
			}
			ans2 = pow(p, cmax);
			printf("%d\n", (int)abs(ans1 - ans2) % mod);
		}
	}
	return 0;
}


#pragma GCC optimize(3)
#include <iostream>
#include <cstring>
#include <bitset>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
int read()
{
	int x = 0, f = 1;
	char c = getchar();
	while(!isdigit(c))
	{
		if(c == '-')
		{
			f = -1;
		}
		c = getchar();
	}
	while(isdigit(c))
	{
		x = x * 10 + c - 48;
		c = getchar();
	}
	return x * f;
}
const int maxn = 1e6 + 10;
int k[maxn], h[maxn];
const int mod = 1e9 + 7;
int pow(int a, int b)
{
	int ans = 1;
	for (; b; b >>= 1)
	{
		if(b & 1)
		{
			ans = 1ll * ans * a % mod;
		}
		a = 1ll * a * a % mod;
	}
	return ans;
}
int main()
{
//freopen("1.in", "r", stdin);
	int T = read();
	while(T--)
	{
		int n = read(), p = read();
		for (int i = 1; i <= n; ++i)
		{
			k[i] = read();
		}
		if(p == 1)
		{
			printf("%d\n", n % 2);
		}
		else
		{
			int cmax = -1;
			sort(k + 1, k + n + 1);
			for (int i = 1; i <= n; ++i)
			{
				h[k[i]]++;
			}
			for (int i = n; i; --i)
			{
				if(cmax == -1)
				{
					if(h[k[i]] % 2 == 1)
					{
						cmax = k[i];
						h[cmax] = 1;
						break;
					}
					else
					{
						h[k[i]] = 0;
					}
				}
			}
			for (int i = 0; i <= cmax; ++i)
			{
				h[i + 1] += h[i] / p;
				h[i] %= p;
			}
			while(h[cmax + 1])
			{
				++cmax;
			}
			if(cmax == -1)
			{
				puts("0");
			}
			else
			{
				long long ans1 = 0, ans2 = 0;
				for (int i = cmax; i >= 0; --i)
				{
					if(h[i] % 2 == 1)
					{
						ans1 = pow(p, i);
						for (int j = 0; j < i; ++j)
						{
							ans2 += h[j] * pow(p, j);
							ans2 %= mod;
						}
						break;
					}
				}
			printf("%d\n", (int)abs(ans1 - ans2) % mod);
			for (int i = 1; i <= n; ++i)
			{
				k[i] = 0;
			}
			for (int i = 0; i <= cmax; ++i)
			{
				h[i] = 0;
			}
			}

		}
	}
	return 0;
}


#pragma GCC optimize(3)
#include <iostream>
#include <cstring>
#include <bitset>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
int read()
{
	int x = 0, f = 1;
	char c = getchar();
	while(!isdigit(c))
	{
		if(c == '-')
		{
			f = -1;
		}
		c = getchar();
	}
	while(isdigit(c))
	{
		x = x * 10 + c - 48;
		c = getchar();
	}
	return x * f;
}
const int maxn = 1e6 + 10;
int k[maxn], h[maxn];
const int mod1 = 1e9 + 7, mod2 = 1e9 + 9;
int pow(int a, int b, int mod)
{
	int ans = 1;
	for (; b; b >>= 1)
	{
		if(b & 1)
		{
			ans = 1ll * ans * a % mod;
		}
		a = 1ll * a * a % mod;
	}
	return ans;
}
int main()
{
//	freopen("1.in", "r", stdin);
	int T = read();
	while(T--)
	{
		int n = read(), p = read();
		for (int i = 1; i <= n; ++i)
		{
			k[i] = read();
		}
		sort(k + 1, k + n + 1);
		long long ans1 = 0, ans2 = 0;
		for (int i = n; i; --i)
		{
			if(!ans1 && !ans2)
			{
				ans1 = (ans1 + pow(p, k[i], mod1)) % mod1;
				ans2 = (ans2 + pow(p, k[i], mod2)) % mod2;
			}
			else
			{
				ans1 = (ans1 - pow(p, k[i], mod1) + mod1) % mod1;
				ans2 = (ans2 - pow(p, k[i], mod2) + mod2) % mod2;
			}
		 }
		printf("%lld\n", ans1);
		for (int i = 1; i <= n; ++i)
		{
			k[i] = 0;
		}
	}
	return 0;
}


#include <iostream>
#include <cstring>
#include <bitset>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
int read()
{
	int x = 0, f = 1;
	char c = getchar();
	while(!isdigit(c))
	{
		if(c == '-')
		{
			f = -1;
		}
		c = getchar();
	}
	while(isdigit(c))
	{
		x = x * 10 + c - 48;
		c = getchar();
	}
	return x * f;
}
const int maxn = 1e6 + 10;
int k[maxn], h[maxn];
const int mod = 1e9 + 7;
int pow(int a, int b)
{
	int ans = 1;
	for (; b; b >>= 1)
	{
		if(b & 1)
		{
			ans = 1ll * ans * a % mod;
		}
		a = 1ll * a * a % mod;
	}
	return ans;
}
int main()
{
//	freopen("1.in", "r", stdin);
	int T = read();
	while(T--)
	{
		int n = read(), p = read();
		for (int i = 1; i <= n; ++i)
		{
				k[i] = read();
		}
		if(p == 1)
		{
			printf("%d\n", n % 2);
		}
		else
		{
			sort(k + 1, k + n + 1);
			int cnt = 0, cmax = -1, i;
			for (i = n; i; --i)
			{
				if(cmax == -1)
				{
					cmax = k[i];
				}
				if(k[i] == cmax)
				{
					++cnt;
				}
				else
				{
					if(cnt % 2 == 0)
					{
						cmax = -1;
						cnt = 0;
					}
					else
					{
						break;
					}
				}
			}
			long long ans1 = pow(p, cmax), ans2 = 0;
			for (int j = i; j; --j)
			{
				ans2 += pow(p, k[j]);
				ans2 %= mod;
			}
			printf("%d\n", (int)abs(ans1 - ans2) % mod);
		}
		for (int i = 1; i <= n; ++i)
		{
			k[i] = 0;
		}
	}
	return 0;
}


#pragma GCC optimize(3)
#include <iostream>
#include <cstring>
#include <bitset>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
int read()
{
	int x = 0, f = 1;
	char c = getchar();
	while(!isdigit(c))
	{
		if(c == '-')
		{
			f = -1;
		}
		c = getchar();
	}
	while(isdigit(c))
	{
		x = x * 10 + c - 48;
		c = getchar();
	}
	return x * f;
}
const int maxn = 1e6 + 10;
int k[maxn], h[maxn];
const int mod1 = 1e9 + 7, mod2 = 1e9 + 3;
int pow(int a, int b, int mod)
{
	int ans = 1;
	for (; b; b >>= 1)
	{
		if(b & 1)
		{
			ans = 1ll * ans * a % mod;
		}
		a = 1ll * a * a % mod;
	}
	return ans;
}
int main()
{
//	freopen("1.in", "r", stdin);
	int T = read();
	while(T--)
	{
		int n = read(), p = read();
		for (int i = 1; i <= n; ++i)
		{
			k[i] = read();
		}
		sort(k + 1, k + n + 1);
		long long ans1 = 0, ans2 = 0;
		for (int i = n; i; --i)
		{
			if(!ans1 && !ans2)
			{
				ans1 = (ans1 + pow(p, k[i], mod1)) % mod1;
				ans2 = (ans2 + pow(p, k[i], mod2)) % mod2;
			}
			else
			{
				ans1 = (ans1 - pow(p, k[i], mod1) + mod1) % mod1;
				ans2 = (ans2 - pow(p, k[i], mod2) + mod2) % mod2;
			}
		 }
		printf("%lld\n", ans1);
		for (int i = 1; i <= n; ++i)
		{
			k[i] = 0;
		}
	}
	return 0;
}


#include<bits/stdc++.h>
#define re register
#define ll long long
#define inc(i,j,k) for(re int i=j;i<=k;i++)
#define dec(i,j,k) for(re int i=j;i>=k;i--)
#define edge(u) for(re int i=head[u];i;i=e[i].nxt)
#define pb push_back
#define mp make_pair
#define In inline
#define fi first
#define se second
#define db double
#define ui unsigned int
#define ull unsigned long long
#define IT set<int>::iterator
using namespace std;
const int N=2e6+5;
const int mod=1e9+7;
In int read(){
	int x=0;
	char ch=getchar();
	while(ch<'0'||ch>'9') ch=getchar();
	while('0'<=ch&&ch<='9') x=x*10+ch-'0',ch=getchar();
	return x;
}
ll qp(ll x,ll k){
	ll res=1;
	while(k){
		if(k&1) res=res*x%mod;
		k>>=1,x=x*x%mod;
	}
	return res;
}
int cmp(int a,int b){return a>b;}
int t,n,p,id[N],cnt[N],k[N];
set<int> s;
ll ans;
void up(int x){
	s.erase(x),cnt[x+1]++;
	if(!s.count(x+1)) s.insert(x+1);
	if(cnt[x+1]==0) s.erase(x+1);
	if(cnt[x+1]==p) up(x+1);
}
void dn(int x){
	s.erase(x),cnt[x+1]--;
	if(!s.count(x+1)) s.insert(x+1);
	if(cnt[x+1]==0) s.erase(x+1);
	if(cnt[x+1]==-p) dn(x+1);
}
int main(){
	t=read();
	while(t--){
		n=read(),p=read();
		inc(i,1,n) k[i]=read();
		if(p==1){printf("%d\n",n/2);continue;}
		sort(k+1,k+1+n,cmp);
		ans=0;s.clear();
		inc(i,1,n){
			bool pd;
			if(!s.size()) pd=0;
			else{
				IT it=s.end();it--;
				if(cnt[*it]>0) pd=1;
				else pd=0;
			}
			if(!pd){
				cnt[k[i]]++;
				if(!s.count(k[i])) s.insert(k[i]);
				if(!cnt[k[i]]) s.erase(k[i]);
				if(cnt[k[i]]==p) up(k[i]);
				id[i]=1;
			}
			else{
				cnt[k[i]]--;
				if(!s.count(k[i])) s.insert(k[i]);
				if(!cnt[k[i]]) s.erase(k[i]);
				if(cnt[k[i]]==-p) dn(k[i]);
				id[i]=2;
			}
		}
		inc(i,1,n){
			if(id[i]==1) (ans+=qp(p,k[i]))%=mod;
			else (ans+=mod-qp(p,k[i]))%=mod;
		}
		printf("%lld\n",ans);
		inc(i,1,n) cnt[k[i]]=0;
	}
}


#include<bits/stdc++.h>
#define re register
#define ll long long
#define inc(i,j,k) for(re int i=j;i<=k;i++)
#define dec(i,j,k) for(re int i=j;i>=k;i--)
#define edge(u) for(re int i=head[u];i;i=e[i].nxt)
#define pb push_back
#define mp make_pair
#define In inline
#define fi first
#define se second
#define db double
#define ui unsigned int
#define ull unsigned long long
#define IT set<int>::iterator
using namespace std;
const int N=2e6+5;
const int mod=1e9+7;
In int read(){
	int x=0;
	char ch=getchar();
	while(ch<'0'||ch>'9') ch=getchar();
	while('0'<=ch&&ch<='9') x=x*10+ch-'0',ch=getchar();
	return x;
}
ll qp(ll x,ll k){
	ll res=1;
	while(k){
		if(k&1) res=res*x%mod;
		k>>=1,x=x*x%mod;
	}
	return res;
}
int cmp(int a,int b){return a>b;}
int t,n,p,id[N],cnt[N],k[N];
set<int> s;
ll ans;
void up(int x){
	s.erase(x),cnt[x+1]++;
	if(!s.count(x+1)) s.insert(x+1);
	if(cnt[x+1]==0) s.erase(x+1);
	if(cnt[x+1]==p) up(x+1);
}
void dn(int x){
	s.erase(x),cnt[x+1]--;
	if(!s.count(x+1)) s.insert(x+1);
	if(cnt[x+1]==0) s.erase(x+1);
	if(cnt[x+1]==-p) dn(x+1);
}
int main(){
	t=read();
	while(t--){
		n=read(),p=read();
		inc(i,1,n) k[i]=read();
		if(p==1){printf("%d\n",n%2);continue;}
		sort(k+1,k+1+n,cmp);
		ans=0;s.clear();
		inc(i,1,n){
			bool pd;
			if(!s.size()) pd=0;
			else{
				IT it=s.end();it--;
				if(cnt[*it]>0) pd=1;
				else pd=0;
			}
			if(!pd){
				cnt[k[i]]++;
				if(!s.count(k[i])) s.insert(k[i]);
				if(!cnt[k[i]]) s.erase(k[i]);
				if(cnt[k[i]]==p) up(k[i]);
				id[i]=1;
			}
			else{
				cnt[k[i]]--;
				if(!s.count(k[i])) s.insert(k[i]);
				if(!cnt[k[i]]) s.erase(k[i]);
				if(cnt[k[i]]==-p) dn(k[i]);
				id[i]=2;
			}
		}
		inc(i,1,n){
			if(id[i]==1) (ans+=qp(p,k[i]))%=mod;
			else (ans+=mod-qp(p,k[i]))%=mod;
		}
		printf("%lld\n",ans);
		for(IT it=s.begin();it!=s.end();it++) cnt[*it]=0;
	}
}


#include<bits/stdc++.h>
#define re register
#define ll long long
#define inc(i,j,k) for(re int i=j;i<=k;i++)
#define dec(i,j,k) for(re int i=j;i>=k;i--)
#define edge(u) for(re int i=head[u];i;i=e[i].nxt)
#define pb push_back
#define mp make_pair
#define In inline
#define fi first
#define se second
#define db double
#define ui unsigned int
#define ull unsigned long long
#define IT set<int>::iterator
using namespace std;
const int N=2e6+5;
const int mod=1e9+7;
In int read(){
	int x=0;
	char ch=getchar();
	while(ch<'0'||ch>'9') ch=getchar();
	while('0'<=ch&&ch<='9') x=x*10+ch-'0',ch=getchar();
	return x;
}
ll qp(ll x,ll k){
	ll res=1;
	while(k){
		if(k&1) res=res*x%mod;
		k>>=1,x=x*x%mod;
	}
	return res;
}
int cmp(int a,int b){return a>b;}
int t,n,p,id[N],cnt[N],k[N];
set<int> s;
ll ans;
void up(int x){
	cnt[x]=0,s.erase(x),cnt[x+1]++;
	if(!s.count(x+1)) s.insert(x+1);
	if(cnt[x+1]==0) s.erase(x+1);
	if(cnt[x+1]==p) up(x+1);
}
void dn(int x){
	cnt[x]=0,s.erase(x),cnt[x+1]--;
	if(!s.count(x+1)) s.insert(x+1);
	if(cnt[x+1]==0) s.erase(x+1);
	if(cnt[x+1]==-p) dn(x+1);
}
int main(){
	t=read();
	while(t--){
		n=read(),p=read();
		inc(i,1,n) k[i]=read();
		if(p==1){printf("%d\n",n%2);continue;}
		sort(k+1,k+1+n,cmp);
		ans=0;s.clear();
		inc(i,1,n){
			bool pd;
			if(!s.size()) pd=0;
			else{
				IT it=s.end();it--;
				if(cnt[*it]>0) pd=1;
				else pd=0;
			}
			if(!pd){
				cnt[k[i]]++;
				if(!s.count(k[i])) s.insert(k[i]);
				if(!cnt[k[i]]) s.erase(k[i]);
				if(cnt[k[i]]==p) up(k[i]);
				id[i]=1;
			}
			else{
				cnt[k[i]]--;
				if(!s.count(k[i])) s.insert(k[i]);
				if(!cnt[k[i]]) s.erase(k[i]);
				if(cnt[k[i]]==-p) dn(k[i]);
				id[i]=2;
			}
		}
		inc(i,1,n){
			if(id[i]==1) (ans+=qp(p,k[i]))%=mod;
			else (ans+=mod-qp(p,k[i]))%=mod;
		}
		printf("%lld\n",ans);
		for(IT it=s.begin();it!=s.end();it++) cnt[*it]=0;
	}
}


#include<bits/stdc++.h>
#define re register
#define ll long long
#define inc(i,j,k) for(re int i=j;i<=k;i++)
#define dec(i,j,k) for(re int i=j;i>=k;i--)
#define edge(u) for(re int i=head[u];i;i=e[i].nxt)
#define pb push_back
#define mp make_pair
#define In inline
#define fi first
#define se second
#define db double
#define ui unsigned int
#define ull unsigned long long
#define IT set<int>::iterator
using namespace std;
const int N=2e6+5;
const int mod=1e9+7;
In int read(){
	int x=0;
	char ch=getchar();
	while(ch<'0'||ch>'9') ch=getchar();
	while('0'<=ch&&ch<='9') x=x*10+ch-'0',ch=getchar();
	return x;
}
ll qp(ll x,ll k){
	ll res=1;
	while(k){
		if(k&1) res=res*x%mod;
		k>>=1,x=x*x%mod;
	}
	return res;
}
int cmp(int a,int b){return a>b;}
int t,n,p,id[N],cnt[N],k[N];
set<int> s;
ll ans;
void up(int x){
	s.erase(x),cnt[x+1]++;
	if(!s.count(x+1)) s.insert(x+1);
	if(cnt[x+1]==0) s.erase(x+1);
	if(cnt[x+1]==p) up(x+1);
}
void dn(int x){
	s.erase(x),cnt[x+1]--;
	if(!s.count(x+1)) s.insert(x+1);
	if(cnt[x+1]==0) s.erase(x+1);
	if(cnt[x+1]==-p) dn(x+1);
}
int main(){
	t=read();
	while(t--){
		n=read(),p=read();
		inc(i,1,n) k[i]=read();
		if(p==1){printf("%d\n",n/2);continue;}
		sort(k+1,k+1+n,cmp);
		ans=0;s.clear();
		inc(i,1,n){
			bool pd;
			if(!s.size()) pd=0;
			else{
				IT it=s.end();it--;
				if(cnt[*it]>0) pd=1;
				else pd=0;
			}
			if(!pd){
				cnt[k[i]]++;
				if(!s.count(k[i])) s.insert(k[i]);
				if(!cnt[k[i]]) s.erase(k[i]);
				if(cnt[k[i]]==p) up(k[i]);
				id[i]=1;
			}
			else{
				cnt[k[i]]--;
				if(!s.count(k[i])) s.insert(k[i]);
				if(!cnt[k[i]]) s.erase(k[i]);
				if(cnt[k[i]]==-p) dn(k[i]);
				id[i]=2;
			}
		}
		inc(i,1,n){
			if(id[i]==1) (ans+=qp(p,k[i]))%=mod;
			else (ans+=mod-qp(p,k[i]))%=mod;
		}
		printf("%lld\n",ans);
		for(IT it=s.begin();it!=s.end();it++) cnt[*it]=0;
	}
}


#include<bits/stdc++.h>
#define re register
#define ll long long
#define inc(i,j,k) for(re int i=j;i<=k;i++)
#define dec(i,j,k) for(re int i=j;i>=k;i--)
#define edge(u) for(re int i=head[u];i;i=e[i].nxt)
#define pb push_back
#define mp make_pair
#define In inline
#define fi first
#define se second
#define db double
#define ui unsigned int
#define ull unsigned long long
#define IT set<int>::iterator
using namespace std;
const int N=1e6+5;
const int mod=1e9+7;
In int read(){
	int x=0;
	char ch=getchar();
	while(ch<'0'||ch>'9') ch=getchar();
	while('0'<=ch&&ch<='9') x=x*10+ch-'0',ch=getchar();
	return x;
}
ll qp(ll x,ll k){
	ll res=1;
	while(k){
		if(k&1) res=res*x%mod;
		k>>=1,x=x*x%mod;
	}
	return res;
}
int cmp(int a,int b){return a>b;}
int t,n,p,id[N],cnt[N],k[N];
set<int> s;
ll ans;
void up(int x){
	cnt[x+1]++;
	s.erase(x);
	if(cnt[x+1]==p) up(x+1);
}
void dn(int x){
	cnt[x+1]--;
	s.erase(x);
	if(cnt[x+1]==-p) dn(x+1);
}
int main(){
	t=read();
	while(t--){
		n=read(),p=read();
		inc(i,1,n) k[i]=read();
		if(p==1){printf("%d\n",n/2);continue;}
		sort(k+1,k+1+n,cmp);
		ans=0;s.clear();
		inc(i,1,n){
			bool pd;
			if(!s.size()) pd=0;
			else{
				IT it=s.end();it--;
				if(cnt[*it]>0) pd=1;
				else pd=0;
			}
			if(!pd){
				cnt[k[i]]++;
				if(!s.count(k[i])) s.insert(k[i]);
				if(!cnt[k[i]]) s.erase(k[i]);
				if(cnt[k[i]]==p) up(k[i]);
				id[i]=1;
			}
			else{
				cnt[k[i]]--;
				if(!s.count(k[i])) s.insert(k[i]);
				if(!cnt[k[i]]) s.erase(k[i]);
				if(cnt[k[i]]==-p) dn(k[i]);
				id[i]=2;
			}
		}
		inc(i,1,n){
			if(id[i]==1) (ans+=qp(p,k[i]))%=mod;
			else (ans+=mod-qp(p,k[i]))%=mod;
		}
		printf("%lld\n",ans);
		inc(i,1,n) cnt[k[i]]=0;
	}
}


//     自己选择的路，跪着也要走完。朋友们，虽然这个世界日益浮躁起来，只
// 要能够为了当时纯粹的梦想和感动坚持努力下去，不管其它人怎么样，我们也
// 能够保持自己的本色走下去。                               ——陈立杰
/*************************************
 * @contest:      Codeforces Round #647 (Div. 1).
 * @user_name:    hkxadpall.
 * @time:         2020-06-04.
 * @language:     C++.
 * @upload_place: Codeforces.
*************************************/

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

typedef signed char          int8;
typedef unsigned char       uint8;
typedef short                int16;
typedef unsigned short      uint16;
typedef int                  int32;
typedef unsigned            uint32;
typedef long long            int64;
typedef unsigned long long  uint64;

template <typename Int>
inline Int read()
{
    Int flag = 1;
    char c = getchar();
    while ((!isdigit(c)) && c != '-' && c != EOF) c = getchar();
    if (c == '-') flag = -1, c = getchar();
    Int init = c & 15;
    while (isdigit(c = getchar())) init = (init << 3) + (init << 1) + (c & 15);
    return init * flag;
}

template <typename Int>
inline Int read(char &c)
{
    Int flag = 1;
    c = getchar();
    while ((!isdigit(c)) && c != '-' && c != EOF) c = getchar();
    if (c == '-') flag = -1, c = getchar();
    Int init = c & 15;
    while (isdigit(c = getchar())) init = (init << 3) + (init << 1) + (c & 15);
    return init * flag;
}

template <typename Int>
inline void write(Int x)
{
    if (x < 0) putchar('-'), x = ~x + 1;
    if (x > 9) write(x / 10);
    putchar((x % 10) | 48);
}

template <typename Int>
inline void write(Int x, char nextch)
{
    write(x);
    putchar(nextch);
}

const int P = 1e9 + 7, N=1e6+7;

#define MEM_ARR(arr, siz, v) { for (unsigned i = 0; i < siz + 5; i++) arr[i] = v; }

inline int Module(int w) { return w >= P ? w - P : w; }

int mul(int x) { return x; }
template<typename... Type>
int mul(int x, Type... Args) { return (int64)x * mul(Args...) % P; }

int add(int x) { return x; }
template<typename... Type>
int add(int x, Type... Args) { return Module(x + add(Args...)); }

template<typename T>
void memarr(size_t siz, int val, T Arg) { MEM_ARR(Arg, siz, val); }
template<typename T, typename... T_>
void memarr(size_t siz, int val, T FirstArg, T_... Args) {
    MEM_ARR(FirstArg, siz, val);
    memarr(siz, val, Args...);
}

// #define int int64

int fpow(int a, int n){
    int ret(1);
    while (n) {
        if (n & 1) ret = mul(ret, a);
        a = mul(a, a);
        n >>=1;
    }return ret;
}

bool fpow(int a, int n, int &ret){
    ret = 1;
    while (n) {
        if (n & 1) {
            ret = ret * a;
            if ((int64)ret * a > N) return 0;
        }
        if (n != 1) {
            if ((int64)a * a > N) return 0;
        }
        a = a * a;
        n >>=1;
    }return 1;
}

int T, n, p;
int k[N];
int dy[N], yd[N], cnt = 0;
int ct[N], sp[N];

signed main() {
    T = read<int>();
    k[0] = -1;
    while (T--) {
        n = read<int>();
        p = read<int>();
        for (int i = 1; i <= n; i++) {
            k[i] = read<int>();
        }
        if (p == 1) {
            if (n & 1) puts("1");
            else puts("0");
            continue;
        }
        sort(k + 1, k + n + 1);
        cnt = 0;
        for (int i = 1; i <= n; i++) {
            if (k[i] != k[i - 1]) {
                yd[dy[k[i]] = ++cnt] = k[i];
                ct[cnt] = 0;
            }
            ct[dy[k[i]]]++;
        }
        for (int i = 1; i < cnt; i++) {
            int ps;
            if (fpow(p, yd[i + 1] - yd[i], ps)) {
                int ex = ct[i] / ps;
                if (!ex) continue;
                // if ((ct[i + 1] + ex) & 1) {
                //     ct[i + 1] += ex - 1;
                //     ct[i] -= (ex - 1) * ps;
                // } else {
                //     ct[i + 1] += ex;
                //     ct[i] -= ex * ps;
                // }
                ct[i + 1] += ex;
                sp[i + 1] = ex;
                ct[i] -= ex * ps;
            } else sp[i + 1] = 0;
        }
        for (int i = cnt; i >= 1; i--) {
            if (ct[i] & 1) {
                if (sp[i]) {
                    ct[i - 1] += p;
                    continue;
                }
                int ans = fpow(p, yd[i]);
                // printf("temproray = %d\n", ans);
                while (--i) {
                    ans = add(ans, P, -mul(fpow(p, yd[i]), ct[i]));
                    // ans = min(ans, Module(P - ans));
                }
                write(ans, 10);
                break;
            } else if (i == 1) {
                puts("0");
                break;
            }
        }
    }
    return 0;
}


//     自己选择的路，跪着也要走完。朋友们，虽然这个世界日益浮躁起来，只
// 要能够为了当时纯粹的梦想和感动坚持努力下去，不管其它人怎么样，我们也
// 能够保持自己的本色走下去。                               ——陈立杰
/*************************************
 * @contest:      Codeforces Round #647 (Div. 1).
 * @user_name:    hkxadpall.
 * @time:         2020-06-04.
 * @language:     C++.
 * @upload_place: Codeforces.
*************************************/

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

typedef signed char          int8;
typedef unsigned char       uint8;
typedef short                int16;
typedef unsigned short      uint16;
typedef int                  int32;
typedef unsigned            uint32;
typedef long long            int64;
typedef unsigned long long  uint64;

template <typename Int>
inline Int read()
{
    Int flag = 1;
    char c = getchar();
    while ((!isdigit(c)) && c != '-' && c != EOF) c = getchar();
    if (c == '-') flag = -1, c = getchar();
    Int init = c & 15;
    while (isdigit(c = getchar())) init = (init << 3) + (init << 1) + (c & 15);
    return init * flag;
}

template <typename Int>
inline Int read(char &c)
{
    Int flag = 1;
    c = getchar();
    while ((!isdigit(c)) && c != '-' && c != EOF) c = getchar();
    if (c == '-') flag = -1, c = getchar();
    Int init = c & 15;
    while (isdigit(c = getchar())) init = (init << 3) + (init << 1) + (c & 15);
    return init * flag;
}

template <typename Int>
inline void write(Int x)
{
    if (x < 0) putchar('-'), x = ~x + 1;
    if (x > 9) write(x / 10);
    putchar((x % 10) | 48);
}

template <typename Int>
inline void write(Int x, char nextch)
{
    write(x);
    putchar(nextch);
}

const int P = 1e9 + 7, N=1e6+7;

#define MEM_ARR(arr, siz, v) { for (unsigned i = 0; i < siz + 5; i++) arr[i] = v; }

inline int Module(int w) { return w >= P ? w - P : w; }

int mul(int x) { return x; }
template<typename... Type>
int mul(int x, Type... Args) { return (int64)x * mul(Args...) % P; }

int add(int x) { return x; }
template<typename... Type>
int add(int x, Type... Args) { return Module(x + add(Args...)); }

template<typename T>
void memarr(size_t siz, int val, T Arg) { MEM_ARR(Arg, siz, val); }
template<typename T, typename... T_>
void memarr(size_t siz, int val, T FirstArg, T_... Args) {
    MEM_ARR(FirstArg, siz, val);
    memarr(siz, val, Args...);
}

// #define int int64

int fpow(int a, int n){
    int ret(1);
    while (n) {
        if (n & 1) ret = mul(ret, a);
        a = mul(a, a);
        n >>=1;
    }return ret;
}

bool fpow(int a, int n, int &ret){
    ret = 1;
    while (n) {
        if (n & 1) {
            ret = ret * a;
            if ((int64)ret * a > N) return 0;
        }
        if (n != 1) {
            if ((int64)a * a > N) return 0;
        }
        a = a * a;
        n >>=1;
    }return 1;
}

int T, n, p;
int k[N];
int dy[N], yd[N], cnt = 0;
int ct[N], sp[N];

signed main() {
    T = read<int>();
    k[0] = -1;
    while (T--) {
        n = read<int>();
        p = read<int>();
        for (int i = 1; i <= n; i++) {
            k[i] = read<int>();
        }
        if (p == 1) {
            if (n & 1) puts("1");
            else puts("0");
            continue;
        }
        sort(k + 1, k + n + 1);
        cnt = 0;
        for (int i = 1; i <= n; i++) {
            if (k[i] != k[i - 1]) {
                yd[dy[k[i]] = ++cnt] = k[i];
                ct[cnt] = 0;
            }
            ct[dy[k[i]]]++;
        }
        for (int i = 1; i < cnt; i++) {
            int ps;
            if (fpow(p, yd[i + 1] - yd[i], ps)) {
                int ex = ct[i] / ps;
                if (!ex) {
                    sp[i + 1] = 0;
                    continue;
                }
                // if ((ct[i + 1] + ex) & 1) {
                //     ct[i + 1] += ex - 1;
                //     ct[i] -= (ex - 1) * ps;
                // } else {
                //     ct[i + 1] += ex;
                //     ct[i] -= ex * ps;
                // }
                ct[i + 1] += ex;
                sp[i + 1] = ex;
                ct[i] -= ex * ps;
            } else sp[i + 1] = 0;
        }
        for (int i = cnt; i >= 1; i--) {
            if (ct[i] & 1) {
                if (sp[i]) {
                    ct[i - 1] += p;
                    continue;
                }
                int ans = fpow(p, yd[i]);
                // printf("temproray = %d\n", ans);
                while (--i) {
                    ans = add(ans, P, -mul(fpow(p, yd[i]), ct[i]));
                    // ans = min(ans, Module(P - ans));
                }
                write(ans, 10);
                break;
            } else if (i == 1) {
                puts("0");
                break;
            }
        }
    }
    return 0;
}


//     自己选择的路，跪着也要走完。朋友们，虽然这个世界日益浮躁起来，只
// 要能够为了当时纯粹的梦想和感动坚持努力下去，不管其它人怎么样，我们也
// 能够保持自己的本色走下去。                               ——陈立杰
/*************************************
 * @contest:      Codeforces Round #647 (Div. 1).
 * @user_name:    hkxadpall.
 * @time:         2020-06-04.
 * @language:     C++.
 * @upload_place: Codeforces.
*************************************/

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

typedef signed char          int8;
typedef unsigned char       uint8;
typedef short                int16;
typedef unsigned short      uint16;
typedef int                  int32;
typedef unsigned            uint32;
typedef long long            int64;
typedef unsigned long long  uint64;

template <typename Int>
inline Int read()
{
    Int flag = 1;
    char c = getchar();
    while ((!isdigit(c)) && c != '-' && c != EOF) c = getchar();
    if (c == '-') flag = -1, c = getchar();
    Int init = c & 15;
    while (isdigit(c = getchar())) init = (init << 3) + (init << 1) + (c & 15);
    return init * flag;
}

template <typename Int>
inline Int read(char &c)
{
    Int flag = 1;
    c = getchar();
    while ((!isdigit(c)) && c != '-' && c != EOF) c = getchar();
    if (c == '-') flag = -1, c = getchar();
    Int init = c & 15;
    while (isdigit(c = getchar())) init = (init << 3) + (init << 1) + (c & 15);
    return init * flag;
}

template <typename Int>
inline void write(Int x)
{
    if (x < 0) putchar('-'), x = ~x + 1;
    if (x > 9) write(x / 10);
    putchar((x % 10) | 48);
}

template <typename Int>
inline void write(Int x, char nextch)
{
    write(x);
    putchar(nextch);
}

const int P = 1e9 + 7, N=1e6+7;

#define MEM_ARR(arr, siz, v) { for (unsigned i = 0; i < siz + 5; i++) arr[i] = v; }

inline int Module(int w) { return w >= P ? w - P : w; }

int mul(int x) { return x; }
template<typename... Type>
int mul(int x, Type... Args) { return (int64)x * mul(Args...) % P; }

int add(int x) { return x; }
template<typename... Type>
int add(int x, Type... Args) { return Module(x + add(Args...)); }

template<typename T>
void memarr(size_t siz, int val, T Arg) { MEM_ARR(Arg, siz, val); }
template<typename T, typename... T_>
void memarr(size_t siz, int val, T FirstArg, T_... Args) {
    MEM_ARR(FirstArg, siz, val);
    memarr(siz, val, Args...);
}

// #define int int64

int fpow(int a, int n){
    int ret(1);
    while (n) {
        if (n & 1) ret = mul(ret, a);
        a = mul(a, a);
        n >>=1;
    }return ret;
}

bool fpow(int a, int n, int &ret){
    ret = 1;
    while (n) {
        if (n & 1) {
            ret = ret * a;
            if ((int64)ret * a > N) return 0;
        }
        if (n != 1) {
            if ((int64)a * a > N) return 0;
        }
        a = a * a;
        n >>=1;
    }return 1;
}

int T, n, p;
int k[N];
int dy[N], yd[N], cnt = 0;
int ct[N];

signed main() {
    T = read<int>();
    k[0] = -1;
    while (T--) {
        n = read<int>();
        p = read<int>();
        for (int i = 1; i <= n; i++) {
            k[i] = read<int>();
        }
        if (p == 1) {
            if (n & 1) puts("1");
            else puts("0");
            continue;
        }
        sort(k + 1, k + n + 1);
        cnt = 0;
        for (int i = 1; i <= n; i++) {
            if (k[i] != k[i - 1]) {
                yd[dy[k[i]] = ++cnt] = k[i];
                ct[cnt] = 0;
            }
            ct[dy[k[i]]]++;
        }
        for (int i = 1; i < cnt; i++) {
            int ps;
            if (fpow(p, yd[i + 1] - yd[i], ps)) {
                int ex = ct[i] / ps;
                if (!ex) continue;
                if ((ct[i + 1] + ex) & 1) {
                    ct[i + 1] += ex - 1;
                    ct[i] -= (ex - 1) * ps;
                } else {
                    ct[i + 1] += ex;
                    ct[i] -= ex * ps;
                }
            }
        }
        for (int i = cnt; i >= 1; i--) {
            if (ct[i] & 1) {
                int ans = fpow(p, yd[i]);
                // printf("temproray = %d\n", ans);
                while (--i) {
                    ans = add(ans, P, -mul(fpow(p, yd[i]), ct[i]));
                    // ans = min(ans, Module(P - ans));
                }
                write(ans, 10);
                break;
            } else if (i == 1) {
                puts("0");
                break;
            }
        }
    }
    return 0;
}


#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
// using namespace __gnu_pbds;
#define int ll
#define rep(i, a, b) for (int (i) = (a); (i) < (b); ++(i))
#define per(i, a, b) for (int (i) = (a); (i) >= (b); --(i))
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define ff first
#define ss second
#define sz(x) ((int) x.size())
#define rd(type, ...) type __VA_ARGS__; sc(__VA_ARGS__)
#define rda(a, n) rep(i, 0, n) cin >> a[i];
#define rdaa(a, n, m) rep(i, 0, n) rep(j, 0, m) cin >> a[i][j];
#define rdv(type, v, n) vector<type> v(n); rep(__ii, 0, n) cin >> v[__ii]
#define rdm(type, v, h, w) vector<vector<type>> v(h, vector<type>(w)); rep(__ii, 0, h) rep(__jj, 0, w) cin >> v[__ii][__jj]
#define rdv2(type, n, ...)std::vector<type> __VA_ARGS__;MACRO_rdv2_Init(n, __VA_ARGS__); for(int w_=0; w_<n; ++w_){MACRO_rdv2_Scan(w_, __VA_ARGS__);}
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "] : ", debug_out(__VA_ARGS__)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define err2(x) {wr(x); return;}
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// typedef tree<int, null_type, less, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
// order_of_key (k) : Number of items strictly smaller than k .
// find_by_order(k) : K-th element in a set (counting from zero).
void rset();
void init_test();
void solve();
signed main() {ios::sync_with_stdio(false); cin.tie(0);
  cout << fixed; cout.precision(20); init_test(); return 0;}
template<typename T> void chmin(T &a, T b) {if (a > b) a = b;}
template<typename T> void chmax(T &a, T b) {if (a < b) a = b;}
template<typename T> void MACRO_rdv2_Init(int n, T& t) { t.resize(n); }
template<typename First, typename... Rest> void MACRO_rdv2_Init(int n, First& first, Rest& ...rest) { first.resize(n); MACRO_rdv2_Init(n, rest...); }
template<typename T> void MACRO_rdv2_Scan(int p, T& t) { std::cin >> t[p]; }
template<typename First, typename... Rest> void MACRO_rdv2_Scan(int p, First& first, Rest& ...rest) { std::cin >> first[p]; MACRO_rdv2_Scan(p, rest...); }
template<typename T> void wrv(const vector<T> &v) {rep(__ii, 0, sz(v)) {if (__ii) cout << ' '; cout << v[__ii];} cout << '\n';}
template<typename T> void wrm(const vector<vector<T>> &v) {rep(__ii, 0, sz(v)) {rep(__jj, 0, v[__ii].size()) {if (__jj) cout << ' '; cout << v[__ii][__jj];} cout << '\n';}}
template<typename T> void sc(T& x) {cin >> x;}
template<typename Head, typename... Tail> void sc(Head& head, Tail&... tail) {cin >> head; sc(tail...);}
template<typename T> void wr(const T& x) {cout << x << '\n';}
template<typename Head, typename... Tail> void wr(const Head& head, const Tail&... tail) {cout << head << ' '; wr(tail...);}
template<typename T> void wrf(const T& x) {cout << x << endl;}
template<typename Head, typename... Tail> void wrf(const Head& head, const Tail&... tail) {cout << head << ' '; wrf(tail...);}
template<typename T> void debug_out(const T& x) {cerr << x << '\n';}
template<typename Head, typename... Tail> void debug_out(const Head& head, const Tail&... tail) {cerr << head << ' '; debug_out(tail...);}
template<typename... T> void err(const T&... cod) {wr(cod...); exit(0);}

const int mod = 1e9 + 7;

void add_self(int &x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
}

void sub_self(int &x, int y) {
  x -= y;
  if (x < 0)
    x += mod;
}

int add(int x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
  return x;
}

int sub(int x, int y) {
  x -= y;
  if (x < 0)
    x += mod;
  return x;
}

int mult(int x, int y) {
  return (1LL * x * y) % mod;
}

int powmod(long long a, long long b) {
  long long res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res % mod;
}

int inv(int x) {
  return powmod(x, mod - 2);
}

int divide(int x, int y) {
  return mult(x, inv(y));
}

void solve(int tot, int qq) {
  rd(int, n, p);
  rdv(int, a, n);
  if (p == 1) err2(n % 2);
  sort(all(a));
  map<int, int> s1, s2;
  while (!a.empty()) {
    int z = a.back();
    a.pop_back();
    if (s2.empty()) {
      ++s2[z];
    } else {
      ++s1[z];
      while (s1[z] == p) {
        s1.erase(z);
        ++z;
      }
      if (z == s2.begin()->ff) {
        s1.clear();
        s2.clear();
      }
    }
  }
  int ans = 0;
  for (auto x : s2) add_self(ans, mult(x.ss, powmod(p, x.ff)));
  for (auto x : s1) sub_self(ans, mult(x.ss, powmod(p, x.ff)));
  if (tot > 4 && qq == 28) {
    wr(n, p);
    wrv(b);
  }
  if (tot == 4) wr(ans);
}

void init_test() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  int qq = 1;
  cin >> qq;
  rep(i, 1, qq + 1) solve(qq, i);
}


#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
// using namespace __gnu_pbds;
#define int ll
#define rep(i, a, b) for (int (i) = (a); (i) < (b); ++(i))
#define per(i, a, b) for (int (i) = (a); (i) >= (b); --(i))
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define ff first
#define ss second
#define sz(x) ((int) x.size())
#define rd(type, ...) type __VA_ARGS__; sc(__VA_ARGS__)
#define rda(a, n) rep(i, 0, n) cin >> a[i];
#define rdaa(a, n, m) rep(i, 0, n) rep(j, 0, m) cin >> a[i][j];
#define rdv(type, v, n) vector<type> v(n); rep(__ii, 0, n) cin >> v[__ii]
#define rdm(type, v, h, w) vector<vector<type>> v(h, vector<type>(w)); rep(__ii, 0, h) rep(__jj, 0, w) cin >> v[__ii][__jj]
#define rdv2(type, n, ...)std::vector<type> __VA_ARGS__;MACRO_rdv2_Init(n, __VA_ARGS__); for(int w_=0; w_<n; ++w_){MACRO_rdv2_Scan(w_, __VA_ARGS__);}
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "] : ", debug_out(__VA_ARGS__)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define err2(x) {wr(x); return;}
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// typedef tree<int, null_type, less, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
// order_of_key (k) : Number of items strictly smaller than k .
// find_by_order(k) : K-th element in a set (counting from zero).
void rset();
void init_test();
void solve();
signed main() {ios::sync_with_stdio(false); cin.tie(0);
  cout << fixed; cout.precision(20); init_test(); return 0;}
template<typename T> void chmin(T &a, T b) {if (a > b) a = b;}
template<typename T> void chmax(T &a, T b) {if (a < b) a = b;}
template<typename T> void MACRO_rdv2_Init(int n, T& t) { t.resize(n); }
template<typename First, typename... Rest> void MACRO_rdv2_Init(int n, First& first, Rest& ...rest) { first.resize(n); MACRO_rdv2_Init(n, rest...); }
template<typename T> void MACRO_rdv2_Scan(int p, T& t) { std::cin >> t[p]; }
template<typename First, typename... Rest> void MACRO_rdv2_Scan(int p, First& first, Rest& ...rest) { std::cin >> first[p]; MACRO_rdv2_Scan(p, rest...); }
template<typename T> void wrv(const vector<T> &v) {rep(__ii, 0, sz(v)) {if (__ii) cout << ' '; cout << v[__ii];} cout << '\n';}
template<typename T> void wrm(const vector<vector<T>> &v) {rep(__ii, 0, sz(v)) {rep(__jj, 0, v[__ii].size()) {if (__jj) cout << ' '; cout << v[__ii][__jj];} cout << '\n';}}
template<typename T> void sc(T& x) {cin >> x;}
template<typename Head, typename... Tail> void sc(Head& head, Tail&... tail) {cin >> head; sc(tail...);}
template<typename T> void wr(const T& x) {cout << x << '\n';}
template<typename Head, typename... Tail> void wr(const Head& head, const Tail&... tail) {cout << head << ' '; wr(tail...);}
template<typename T> void wrf(const T& x) {cout << x << endl;}
template<typename Head, typename... Tail> void wrf(const Head& head, const Tail&... tail) {cout << head << ' '; wrf(tail...);}
template<typename T> void debug_out(const T& x) {cerr << x << '\n';}
template<typename Head, typename... Tail> void debug_out(const Head& head, const Tail&... tail) {cerr << head << ' '; debug_out(tail...);}
template<typename... T> void err(const T&... cod) {wr(cod...); exit(0);}

const int mod = 1e9 + 7;

void add_self(int &x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
}

void sub_self(int &x, int y) {
  x -= y;
  if (x < 0)
    x += mod;
}

int add(int x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
  return x;
}

int sub(int x, int y) {
  x -= y;
  if (x < 0)
    x += mod;
  return x;
}

int mult(int x, int y) {
  return (1LL * x * y) % mod;
}

int powmod(long long a, long long b) {
  long long res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res % mod;
}

int inv(int x) {
  return powmod(x, mod - 2);
}

int divide(int x, int y) {
  return mult(x, inv(y));
}

void solve(int tot, int qq) {
  rd(int, n, p);
  rdv(int, a, n);
  if (p == 1) err2(n % 2);
  sort(all(a));
  vi b = a;
  map<int, int> s1, s2;
  while (!a.empty()) {
    int z = a.back();
    a.pop_back();
    if (s2.empty()) {
      ++s2[z];
    } else {
      ++s1[z];
      while (s1[z] == p) {
        s1.erase(z);
        ++z;
      }
      if (z == s2.begin()->ff) {
        s1.clear();
        s2.clear();
      }
    }
  }
  int ans = 0;
  for (auto x : s2) add_self(ans, mult(x.ss, powmod(p, x.ff)));
  for (auto x : s1) sub_self(ans, mult(x.ss, powmod(p, x.ff)));
  if (tot == 10000 && qq == 28) {
    wr(n, p);
    wrv(b);
  }
  if (tot == 4) wr(ans);
}

void init_test() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  int qq = 1;
  cin >> qq;
  rep(i, 1, qq + 1) solve(qq, i);
}


#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
// using namespace __gnu_pbds;
#define int ll
#define rep(i, a, b) for (int (i) = (a); (i) < (b); ++(i))
#define per(i, a, b) for (int (i) = (a); (i) >= (b); --(i))
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define ff first
#define ss second
#define sz(x) ((int) x.size())
#define rd(type, ...) type __VA_ARGS__; sc(__VA_ARGS__)
#define rda(a, n) rep(i, 0, n) cin >> a[i];
#define rdaa(a, n, m) rep(i, 0, n) rep(j, 0, m) cin >> a[i][j];
#define rdv(type, v, n) vector<type> v(n); rep(__ii, 0, n) cin >> v[__ii]
#define rdm(type, v, h, w) vector<vector<type>> v(h, vector<type>(w)); rep(__ii, 0, h) rep(__jj, 0, w) cin >> v[__ii][__jj]
#define rdv2(type, n, ...)std::vector<type> __VA_ARGS__;MACRO_rdv2_Init(n, __VA_ARGS__); for(int w_=0; w_<n; ++w_){MACRO_rdv2_Scan(w_, __VA_ARGS__);}
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "] : ", debug_out(__VA_ARGS__)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define err2(x) {wr(x); return;}
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// typedef tree<int, null_type, less, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
// order_of_key (k) : Number of items strictly smaller than k .
// find_by_order(k) : K-th element in a set (counting from zero).
void rset();
void init_test();
void solve();
signed main() {ios::sync_with_stdio(false); cin.tie(0);
  cout << fixed; cout.precision(20); init_test(); return 0;}
template<typename T> void chmin(T &a, T b) {if (a > b) a = b;}
template<typename T> void chmax(T &a, T b) {if (a < b) a = b;}
template<typename T> void MACRO_rdv2_Init(int n, T& t) { t.resize(n); }
template<typename First, typename... Rest> void MACRO_rdv2_Init(int n, First& first, Rest& ...rest) { first.resize(n); MACRO_rdv2_Init(n, rest...); }
template<typename T> void MACRO_rdv2_Scan(int p, T& t) { std::cin >> t[p]; }
template<typename First, typename... Rest> void MACRO_rdv2_Scan(int p, First& first, Rest& ...rest) { std::cin >> first[p]; MACRO_rdv2_Scan(p, rest...); }
template<typename T> void wrv(const vector<T> &v) {rep(__ii, 0, sz(v)) {if (__ii) cout << ' '; cout << v[__ii];} cout << '\n';}
template<typename T> void wrm(const vector<vector<T>> &v) {rep(__ii, 0, sz(v)) {rep(__jj, 0, v[__ii].size()) {if (__jj) cout << ' '; cout << v[__ii][__jj];} cout << '\n';}}
template<typename T> void sc(T& x) {cin >> x;}
template<typename Head, typename... Tail> void sc(Head& head, Tail&... tail) {cin >> head; sc(tail...);}
template<typename T> void wr(const T& x) {cout << x << '\n';}
template<typename Head, typename... Tail> void wr(const Head& head, const Tail&... tail) {cout << head << ' '; wr(tail...);}
template<typename T> void wrf(const T& x) {cout << x << endl;}
template<typename Head, typename... Tail> void wrf(const Head& head, const Tail&... tail) {cout << head << ' '; wrf(tail...);}
template<typename T> void debug_out(const T& x) {cerr << x << '\n';}
template<typename Head, typename... Tail> void debug_out(const Head& head, const Tail&... tail) {cerr << head << ' '; debug_out(tail...);}
template<typename... T> void err(const T&... cod) {wr(cod...); exit(0);}

const int mod = 1e9 + 7;

void add_self(int &x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
}

void sub_self(int &x, int y) {
  x -= y;
  if (x < 0)
    x += mod;
}

int add(int x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
  return x;
}

int sub(int x, int y) {
  x -= y;
  if (x < 0)
    x += mod;
  return x;
}

int mult(int x, int y) {
  return (1LL * x * y) % mod;
}

int powmod(long long a, long long b) {
  long long res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res % mod;
}

int inv(int x) {
  return powmod(x, mod - 2);
}

int divide(int x, int y) {
  return mult(x, inv(y));
}

void solve() {
  rd(int, n, p);
  rdv(int, a, n);
  if (p == 1) err2(n % 2);
  sort(all(a));
  map<int, int> s1, s2;
  while (!a.empty()) {
    int z = a.back();
    a.pop_back();
    if (s2.empty()) {
      ++s2[z];
    } else {
      ++s1[z];
      while (s1[z] == p) {
        s1.erase(z);
        ++z;
      }
      if (z == s2.begin()->ff) {
        s1.clear();
        s2.clear();
      }
    }
  }
  int ans = 0;
  for (auto x : s2) add_self(ans, mult(x.ss, powmod(p, x.ff)));
  for (auto x : s1) sub_self(ans, mult(x.ss, powmod(p, x.ff)));
  // for (auto x : s2) dbg(x.ff);
  // for (auto y : s1) dbg(y.ff);
  wr(ans);
}

void init_test() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  int qq = 1;
  cin >> qq;
  while (qq--) solve();
}


#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
// using namespace __gnu_pbds;
#define int ll
#define rep(i, a, b) for (int (i) = (a); (i) < (b); ++(i))
#define per(i, a, b) for (int (i) = (a); (i) >= (b); --(i))
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define ff first
#define ss second
#define sz(x) ((int) x.size())
#define rd(type, ...) type __VA_ARGS__; sc(__VA_ARGS__)
#define rda(a, n) rep(i, 0, n) cin >> a[i];
#define rdaa(a, n, m) rep(i, 0, n) rep(j, 0, m) cin >> a[i][j];
#define rdv(type, v, n) vector<type> v(n); rep(__ii, 0, n) cin >> v[__ii]
#define rdm(type, v, h, w) vector<vector<type>> v(h, vector<type>(w)); rep(__ii, 0, h) rep(__jj, 0, w) cin >> v[__ii][__jj]
#define rdv2(type, n, ...)std::vector<type> __VA_ARGS__;MACRO_rdv2_Init(n, __VA_ARGS__); for(int w_=0; w_<n; ++w_){MACRO_rdv2_Scan(w_, __VA_ARGS__);}
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "] : ", debug_out(__VA_ARGS__)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define err2(x) {wr(x); return;}
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// typedef tree<int, null_type, less, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
// order_of_key (k) : Number of items strictly smaller than k .
// find_by_order(k) : K-th element in a set (counting from zero).
void rset();
void init_test();
void solve();
signed main() {ios::sync_with_stdio(false); cin.tie(0);
  cout << fixed; cout.precision(20); init_test(); return 0;}
template<typename T> void chmin(T &a, T b) {if (a > b) a = b;}
template<typename T> void chmax(T &a, T b) {if (a < b) a = b;}
template<typename T> void MACRO_rdv2_Init(int n, T& t) { t.resize(n); }
template<typename First, typename... Rest> void MACRO_rdv2_Init(int n, First& first, Rest& ...rest) { first.resize(n); MACRO_rdv2_Init(n, rest...); }
template<typename T> void MACRO_rdv2_Scan(int p, T& t) { std::cin >> t[p]; }
template<typename First, typename... Rest> void MACRO_rdv2_Scan(int p, First& first, Rest& ...rest) { std::cin >> first[p]; MACRO_rdv2_Scan(p, rest...); }
template<typename T> void wrv(const vector<T> &v) {rep(__ii, 0, sz(v)) {if (__ii) cout << ' '; cout << v[__ii];} cout << '\n';}
template<typename T> void wrm(const vector<vector<T>> &v) {rep(__ii, 0, sz(v)) {rep(__jj, 0, v[__ii].size()) {if (__jj) cout << ' '; cout << v[__ii][__jj];} cout << '\n';}}
template<typename T> void sc(T& x) {cin >> x;}
template<typename Head, typename... Tail> void sc(Head& head, Tail&... tail) {cin >> head; sc(tail...);}
template<typename T> void wr(const T& x) {cout << x << '\n';}
template<typename Head, typename... Tail> void wr(const Head& head, const Tail&... tail) {cout << head << ' '; wr(tail...);}
template<typename T> void wrf(const T& x) {cout << x << endl;}
template<typename Head, typename... Tail> void wrf(const Head& head, const Tail&... tail) {cout << head << ' '; wrf(tail...);}
template<typename T> void debug_out(const T& x) {cerr << x << '\n';}
template<typename Head, typename... Tail> void debug_out(const Head& head, const Tail&... tail) {cerr << head << ' '; debug_out(tail...);}
template<typename... T> void err(const T&... cod) {wr(cod...); exit(0);}

const int mod = 1e9 + 7;

void add_self(int &x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
}

void sub_self(int &x, int y) {
  x -= y;
  if (x < 0)
    x += mod;
}

int add(int x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
  return x;
}

int sub(int x, int y) {
  x -= y;
  if (x < 0)
    x += mod;
  return x;
}

int mult(int x, int y) {
  return (1LL * x * y) % mod;
}

int powmod(long long a, long long b) {
  long long res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res % mod;
}

int inv(int x) {
  return powmod(x, mod - 2);
}

int divide(int x, int y) {
  return mult(x, inv(y));
}

void solve(int qq) {
  rd(int, n, p);
  rdv(int, a, n);
  if (p == 1) err2(n % 2);
  sort(all(a));
  map<int, int> s1, s2;
  while (!a.empty()) {
    int z = a.back();
    a.pop_back();
    if (s2.empty()) {
      ++s2[z];
    } else {
      ++s1[z];
      while (s1[z] == p) {
        s1.erase(z);
        ++z;
      }
      if (z == s2.begin()->ff) {
        s1.clear();
        s2.clear();
      }
    }
  }
  int ans = 0;
  for (auto x : s2) add_self(ans, mult(x.ss, powmod(p, x.ff)));
  for (auto x : s1) sub_self(ans, mult(x.ss, powmod(p, x.ff)));
  if (n == 10000 && qq == 28) {
    wr(n);
    wrv(a);
  }
  if (n == 4) wr(ans);
}

void init_test() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  int qq = 1;
  cin >> qq;
  rep(i, 1, qq + 1) solve(qq);
}


#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
// using namespace __gnu_pbds;
#define int ll
#define rep(i, a, b) for (int (i) = (a); (i) < (b); ++(i))
#define per(i, a, b) for (int (i) = (a); (i) >= (b); --(i))
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define ff first
#define ss second
#define sz(x) ((int) x.size())
#define rd(type, ...) type __VA_ARGS__; sc(__VA_ARGS__)
#define rda(a, n) rep(i, 0, n) cin >> a[i];
#define rdaa(a, n, m) rep(i, 0, n) rep(j, 0, m) cin >> a[i][j];
#define rdv(type, v, n) vector<type> v(n); rep(__ii, 0, n) cin >> v[__ii]
#define rdm(type, v, h, w) vector<vector<type>> v(h, vector<type>(w)); rep(__ii, 0, h) rep(__jj, 0, w) cin >> v[__ii][__jj]
#define rdv2(type, n, ...)std::vector<type> __VA_ARGS__;MACRO_rdv2_Init(n, __VA_ARGS__); for(int w_=0; w_<n; ++w_){MACRO_rdv2_Scan(w_, __VA_ARGS__);}
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "] : ", debug_out(__VA_ARGS__)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define err2(x) {wr(x); return;}
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// typedef tree<int, null_type, less, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
// order_of_key (k) : Number of items strictly smaller than k .
// find_by_order(k) : K-th element in a set (counting from zero).
void rset();
void init_test();
void solve();
signed main() {ios::sync_with_stdio(false); cin.tie(0);
  cout << fixed; cout.precision(20); init_test(); return 0;}
template<typename T> void chmin(T &a, T b) {if (a > b) a = b;}
template<typename T> void chmax(T &a, T b) {if (a < b) a = b;}
template<typename T> void MACRO_rdv2_Init(int n, T& t) { t.resize(n); }
template<typename First, typename... Rest> void MACRO_rdv2_Init(int n, First& first, Rest& ...rest) { first.resize(n); MACRO_rdv2_Init(n, rest...); }
template<typename T> void MACRO_rdv2_Scan(int p, T& t) { std::cin >> t[p]; }
template<typename First, typename... Rest> void MACRO_rdv2_Scan(int p, First& first, Rest& ...rest) { std::cin >> first[p]; MACRO_rdv2_Scan(p, rest...); }
template<typename T> void wrv(const vector<T> &v) {rep(__ii, 0, sz(v)) {if (__ii) cout << ' '; cout << v[__ii];} cout << '\n';}
template<typename T> void wrm(const vector<vector<T>> &v) {rep(__ii, 0, sz(v)) {rep(__jj, 0, v[__ii].size()) {if (__jj) cout << ' '; cout << v[__ii][__jj];} cout << '\n';}}
template<typename T> void sc(T& x) {cin >> x;}
template<typename Head, typename... Tail> void sc(Head& head, Tail&... tail) {cin >> head; sc(tail...);}
template<typename T> void wr(const T& x) {cout << x << '\n';}
template<typename Head, typename... Tail> void wr(const Head& head, const Tail&... tail) {cout << head << ' '; wr(tail...);}
template<typename T> void wrf(const T& x) {cout << x << endl;}
template<typename Head, typename... Tail> void wrf(const Head& head, const Tail&... tail) {cout << head << ' '; wrf(tail...);}
template<typename T> void debug_out(const T& x) {cerr << x << '\n';}
template<typename Head, typename... Tail> void debug_out(const Head& head, const Tail&... tail) {cerr << head << ' '; debug_out(tail...);}
template<typename... T> void err(const T&... cod) {wr(cod...); exit(0);}

const int mod = 1e9 + 7;

void add_self(int &x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
}

void sub_self(int &x, int y) {
  x -= y;
  if (x < 0)
    x += mod;
}

int add(int x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
  return x;
}

int sub(int x, int y) {
  x -= y;
  if (x < 0)
    x += mod;
  return x;
}

int mult(int x, int y) {
  return (1LL * x * y) % mod;
}

int powmod(long long a, long long b) {
  long long res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res % mod;
}

int inv(int x) {
  return powmod(x, mod - 2);
}

int divide(int x, int y) {
  return mult(x, inv(y));
}

void solve(int tot, int qq) {
  rd(int, n, p);
  rdv(int, a, n);
  if (p == 1) err2(n % 2);
  sort(all(a));
  map<int, int> s1, s2;
  while (!a.empty()) {
    int z = a.back();
    a.pop_back();
    if (s2.empty()) {
      ++s2[z];
    } else {
      ++s1[z];
      while (s1[z] == p) {
        s1.erase(z);
        ++z;
      }
      if (z == s2.begin()->ff) {
        s1.clear();
        s2.clear();
      }
    }
  }
  int ans = 0;
  for (auto x : s2) add_self(ans, mult(x.ss, powmod(p, x.ff)));
  for (auto x : s1) sub_self(ans, mult(x.ss, powmod(p, x.ff)));
  if (tot > 4 && qq == 28) {
    wr(n, p);
    wrv(a);
  }
  if (tot == 4) wr(ans);
}

void init_test() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  int qq = 1;
  cin >> qq;
  rep(i, 1, qq + 1) solve(qq, i);
}


#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
// using namespace __gnu_pbds;
#define int ll
#define rep(i, a, b) for (int (i) = (a); (i) < (b); ++(i))
#define per(i, a, b) for (int (i) = (a); (i) >= (b); --(i))
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define ff first
#define ss second
#define sz(x) ((int) x.size())
#define rd(type, ...) type __VA_ARGS__; sc(__VA_ARGS__)
#define rda(a, n) rep(i, 0, n) cin >> a[i];
#define rdaa(a, n, m) rep(i, 0, n) rep(j, 0, m) cin >> a[i][j];
#define rdv(type, v, n) vector<type> v(n); rep(__ii, 0, n) cin >> v[__ii]
#define rdm(type, v, h, w) vector<vector<type>> v(h, vector<type>(w)); rep(__ii, 0, h) rep(__jj, 0, w) cin >> v[__ii][__jj]
#define rdv2(type, n, ...)std::vector<type> __VA_ARGS__;MACRO_rdv2_Init(n, __VA_ARGS__); for(int w_=0; w_<n; ++w_){MACRO_rdv2_Scan(w_, __VA_ARGS__);}
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "] : ", debug_out(__VA_ARGS__)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define err2(x) {wr(x); return;}
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// typedef tree<int, null_type, less, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
// order_of_key (k) : Number of items strictly smaller than k .
// find_by_order(k) : K-th element in a set (counting from zero).
void rset();
void init_test();
void solve();
signed main() {ios::sync_with_stdio(false); cin.tie(0);
  cout << fixed; cout.precision(20); init_test(); return 0;}
template<typename T> void chmin(T &a, T b) {if (a > b) a = b;}
template<typename T> void chmax(T &a, T b) {if (a < b) a = b;}
template<typename T> void MACRO_rdv2_Init(int n, T& t) { t.resize(n); }
template<typename First, typename... Rest> void MACRO_rdv2_Init(int n, First& first, Rest& ...rest) { first.resize(n); MACRO_rdv2_Init(n, rest...); }
template<typename T> void MACRO_rdv2_Scan(int p, T& t) { std::cin >> t[p]; }
template<typename First, typename... Rest> void MACRO_rdv2_Scan(int p, First& first, Rest& ...rest) { std::cin >> first[p]; MACRO_rdv2_Scan(p, rest...); }
template<typename T> void wrv(const vector<T> &v) {rep(__ii, 0, sz(v)) {if (__ii) cout << ' '; cout << v[__ii];} cout << '\n';}
template<typename T> void wrm(const vector<vector<T>> &v) {rep(__ii, 0, sz(v)) {rep(__jj, 0, v[__ii].size()) {if (__jj) cout << ' '; cout << v[__ii][__jj];} cout << '\n';}}
template<typename T> void sc(T& x) {cin >> x;}
template<typename Head, typename... Tail> void sc(Head& head, Tail&... tail) {cin >> head; sc(tail...);}
template<typename T> void wr(const T& x) {cout << x << '\n';}
template<typename Head, typename... Tail> void wr(const Head& head, const Tail&... tail) {cout << head << ' '; wr(tail...);}
template<typename T> void wrf(const T& x) {cout << x << endl;}
template<typename Head, typename... Tail> void wrf(const Head& head, const Tail&... tail) {cout << head << ' '; wrf(tail...);}
template<typename T> void debug_out(const T& x) {cerr << x << '\n';}
template<typename Head, typename... Tail> void debug_out(const Head& head, const Tail&... tail) {cerr << head << ' '; debug_out(tail...);}
template<typename... T> void err(const T&... cod) {wr(cod...); exit(0);}

const int mod = 1e9 + 7;

void add_self(int &x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
}

void sub_self(int &x, int y) {
  x -= y;
  if (x < 0)
    x += mod;
}

int add(int x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
  return x;
}

int sub(int x, int y) {
  x -= y;
  if (x < 0)
    x += mod;
  return x;
}

int mult(int x, int y) {
  return (1LL * x * y) % mod;
}

int powmod(long long a, long long b) {
  long long res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res % mod;
}

int inv(int x) {
  return powmod(x, mod - 2);
}

int divide(int x, int y) {
  return mult(x, inv(y));
}

void solve(int tot, int qq) {
  rd(int, n, p);
  rdv(int, a, n);
  if (p == 1) err2(n % 2);
  sort(all(a));
  vi b = a;
  map<int, int> s1, s2;
  while (!a.empty()) {
    int z = a.back();
    a.pop_back();
    if (s2.empty()) {
      ++s2[z];
    } else {
      ++s1[z];
      while (s1[z] == p) {
        s1.erase(z);
        ++z;
      }
      if (z == s2.begin()->ff) {
        s1.clear();
        s2.clear();
      }
    }
  }
  int ans = 0;
  for (auto x : s2) add_self(ans, mult(x.ss, powmod(p, x.ff)));
  for (auto x : s1) sub_self(ans, mult(x.ss, powmod(p, x.ff)));
  if (tot > 4 && qq == 28) {
    wr(n, p);
    wrv(b);
  }
  if (tot == 4) wr(ans);
}

void init_test() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  int qq = 1;
  cin >> qq;
  rep(i, 1, qq + 1) solve(qq, i);
}


#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
// using namespace __gnu_pbds;
#define int ll
#define rep(i, a, b) for (int (i) = (a); (i) < (b); ++(i))
#define per(i, a, b) for (int (i) = (a); (i) >= (b); --(i))
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define ff first
#define ss second
#define sz(x) ((int) x.size())
#define rd(type, ...) type __VA_ARGS__; sc(__VA_ARGS__)
#define rda(a, n) rep(i, 0, n) cin >> a[i];
#define rdaa(a, n, m) rep(i, 0, n) rep(j, 0, m) cin >> a[i][j];
#define rdv(type, v, n) vector<type> v(n); rep(__ii, 0, n) cin >> v[__ii]
#define rdm(type, v, h, w) vector<vector<type>> v(h, vector<type>(w)); rep(__ii, 0, h) rep(__jj, 0, w) cin >> v[__ii][__jj]
#define rdv2(type, n, ...)std::vector<type> __VA_ARGS__;MACRO_rdv2_Init(n, __VA_ARGS__); for(int w_=0; w_<n; ++w_){MACRO_rdv2_Scan(w_, __VA_ARGS__);}
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "] : ", debug_out(__VA_ARGS__)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define err2(x) {wr(x); return;}
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// typedef tree<int, null_type, less, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
// order_of_key (k) : Number of items strictly smaller than k .
// find_by_order(k) : K-th element in a set (counting from zero).
void rset();
void init_test();
void solve();
signed main() {ios::sync_with_stdio(false); cin.tie(0);
  cout << fixed; cout.precision(20); init_test(); return 0;}
template<typename T> void chmin(T &a, T b) {if (a > b) a = b;}
template<typename T> void chmax(T &a, T b) {if (a < b) a = b;}
template<typename T> void MACRO_rdv2_Init(int n, T& t) { t.resize(n); }
template<typename First, typename... Rest> void MACRO_rdv2_Init(int n, First& first, Rest& ...rest) { first.resize(n); MACRO_rdv2_Init(n, rest...); }
template<typename T> void MACRO_rdv2_Scan(int p, T& t) { std::cin >> t[p]; }
template<typename First, typename... Rest> void MACRO_rdv2_Scan(int p, First& first, Rest& ...rest) { std::cin >> first[p]; MACRO_rdv2_Scan(p, rest...); }
template<typename T> void wrv(const vector<T> &v) {rep(__ii, 0, sz(v)) {if (__ii) cout << ' '; cout << v[__ii];} cout << '\n';}
template<typename T> void wrm(const vector<vector<T>> &v) {rep(__ii, 0, sz(v)) {rep(__jj, 0, v[__ii].size()) {if (__jj) cout << ' '; cout << v[__ii][__jj];} cout << '\n';}}
template<typename T> void sc(T& x) {cin >> x;}
template<typename Head, typename... Tail> void sc(Head& head, Tail&... tail) {cin >> head; sc(tail...);}
template<typename T> void wr(const T& x) {cout << x << '\n';}
template<typename Head, typename... Tail> void wr(const Head& head, const Tail&... tail) {cout << head << ' '; wr(tail...);}
template<typename T> void wrf(const T& x) {cout << x << endl;}
template<typename Head, typename... Tail> void wrf(const Head& head, const Tail&... tail) {cout << head << ' '; wrf(tail...);}
template<typename T> void debug_out(const T& x) {cerr << x << '\n';}
template<typename Head, typename... Tail> void debug_out(const Head& head, const Tail&... tail) {cerr << head << ' '; debug_out(tail...);}
template<typename... T> void err(const T&... cod) {wr(cod...); exit(0);}

const int mod = 1e9 + 7;

void add_self(int &x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
}

void sub_self(int &x, int y) {
  x -= y;
  if (x < 0)
    x += mod;
}

int add(int x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
  return x;
}

int sub(int x, int y) {
  x -= y;
  if (x < 0)
    x += mod;
  return x;
}

int mult(int x, int y) {
  return (1LL * x * y) % mod;
}

int powmod(long long a, long long b) {
  long long res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res % mod;
}

int inv(int x) {
  return powmod(x, mod - 2);
}

int divide(int x, int y) {
  return mult(x, inv(y));
}

void solve(int tot, int qq) {
  rd(int, n, p);
  rdv(int, a, n);
  if (p == 1) err2(n % 2);
  sort(all(a));
  vi b = a;
  map<int, int> s1, s2;
  while (!a.empty()) {
    int z = a.back();
    a.pop_back();
    if (s2.empty()) {
      ++s2[z];
    } else {
      ++s1[z];
      while (s1[z] == p) {
        s1.erase(z);
        ++z;
        ++s1[z];
      }
      if (z == s2.begin()->ff) {
        s1.clear();
        s2.clear();
      }
    }
  }
  int ans = 0;
  for (auto x : s2) add_self(ans, mult(x.ss, powmod(p, x.ff)));
  for (auto x : s1) sub_self(ans, mult(x.ss, powmod(p, x.ff)));
  // for (auto x : s2) dbg(x.ff, x.ss);
  // for (auto y : s1) dbg(y.ff, y.ss);
  wr(ans);
}

void init_test() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  int qq = 1;
  cin >> qq;
  rep(i, 1, qq + 1) solve(qq, i);
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;

const int N=1e6+10,mod=1e9+7;

ll n,p,t,a[N];

ll qmi(ll a,ll b){
	ll res=1;
	while(b){
		if(b&1ll) res=res*a%mod;
		b>>=1; a=a*a%mod;
	}
	return res;
}
P dif(P a,P b){
	ll res;
	ll mi=b.first;
	res=qmi(p,a.first-b.first)*a.second-b.second;
	if(res>1e6) return {-1,-1};
	return {b.first,res};
}
void work(){
	cin>>n>>p;
	for(ll i=0;i<n;i++) scanf("%lld",&a[i]);
	sort(a,a+n,greater<ll>());
	if(p==1){
		cout<<(n%2)<<endl; return ;
	}
	ll ans=0;
	for(ll i=0;i<n;i++){
		if(ans==0) (ans+=qmi(p,a[i]))%mod;
		else ans=((ans-qmi(p,a[i]))%mod+mod)%mod;
//cout<<ans<<endl;
	}
	cout<<ans<<endl;
	return ;
}

int main(){
	//work();
	for(cin>>t;t--;){
		work();
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;

const int N=1e6+10,mod=1e9+7,mod1=123456789;

ll n,p,t,a[N];

ll qmi(ll a,ll b,ll mod=mod){
	ll res=1;
	while(b){
		if(b&1ll) res=res*a%mod;
		b>>=1; a=a*a%mod;
	}
	return res;
}
P dif(P a,P b){
	ll res;
	ll mi=b.first;
	res=qmi(p,a.first-b.first)*a.second-b.second;
	if(res>1e6) return {-1,-1};
	return {b.first,res};
}
void work(){
	cin>>n>>p;
	for(ll i=0;i<n;i++) scanf("%lld",&a[i]);
	sort(a,a+n,greater<ll>());
	if(p==1){
		cout<<(n%2)<<endl; return ;
	}
	ll ans=0,ans1=0;
	for(ll i=0;i<n;i++){
		if(ans==0&&ans1==0) (ans+=qmi(p,a[i],mod))%mod, (ans1+=qmi(p,a[i],mod1))%mod1;
		else ans=((ans-qmi(p,a[i],mod))%mod+mod)%mod, ans1=(ans1-qmi(p,a[i],mod1)+mod1)%mod1;
	}
	cout<<ans<<endl;
	return ;
}

int main(){
	//work();
	for(cin>>t;t--;){
		work();
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;

const int N=1e6+10,mod=1e9+7;

ll n,p,t,a[N];

ll qmi(ll a,ll b){
	ll res=1;
	while(b){
		if(b&1) res=res*a%mod;
		b>>=1; a=a*a%mod;
	}
	return res;
}
P dif(P a,P b){
	ll res;
	res=qmi(p,a.first-b.first)*a.second-b.second;
	return {b.first,res};
}
void work(){
	cin>>n>>p;
	for(ll i=0;i<n;i++) scanf("%lld",&a[i]);
	sort(a,a+n,greater<ll>());
	if(p==1){
		cout<<(n%2?1:0)<<endl; return ;
	}
	P sum={0,0};
	ll mdif=ceil(log(1e6)/log(1.0*p));
	ll anoi=n;//幂差超最大限度后another集合直接加
	for(ll i=0;i<n;i++){
		if(sum.second==0) sum={a[i],1};
		else {
			if(sum.first-a[i]>mdif){
				anoi=i; break;
			}else{
				sum=dif(sum,{a[i],1});
			}
		}
	}
	ll ano=0;
	for(ll i=anoi;i<n;i++){
		ano=(ano+qmi(p,a[i]))%mod;
	}
	ll ans=qmi(p,sum.first)*sum.second%mod-ano;
	ans=(ans%mod+mod)%mod;
	cout<<ans<<endl;
	return ;
}

int main(){
	//work();
	for(cin>>t;t--;){
		work();
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;

const int N=1e6+10,mod=1e9+7;

ll n,p,t,a[N];

ll qmi(ll a,ll b){
	ll res=1;
	while(b){
		if(b&1ll) res=res*a%mod;
		b>>=1; a=a*a%mod;
	}
	return res;
}
P dif(P a,P b){
	ll res;
	res=qmi(p,a.first-b.first)*a.second-b.second;
	return {b.first,res};
}
void work(){
	cin>>n>>p;
	for(ll i=0;i<n;i++) scanf("%lld",&a[i]);
	sort(a,a+n,greater<ll>());
	if(p==1){
		cout<<(n%2?1:0)<<endl; return ;
	}
	P sum={0,0};
	ll mdif=(log(1e6)/log(1.0*p));
	ll anoi=n;//幂差超最大限度后another集合直接加
	for(ll i=0;i<n;i++){
		if(sum.second==0) sum={a[i],1};
		else {
			if(sum.first-a[i]>mdif){
				anoi=i; break;
			}else{
				sum=dif(sum,{a[i],1});
			}
		}
	}
	ll ano=0;
	for(ll i=anoi;i<n;i++){
		ano=(ano+qmi(p,a[i]))%mod;
	}
	ll ans=qmi(p,sum.first)*sum.second%mod-ano;
	ans=(ans%mod+mod)%mod;
	cout<<ans<<endl;
	return ;
}

int main(){
	//work();
	for(cin>>t;t--;){
		work();
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;

const int N=1e6+10,mod=1e9+7;

ll n,p,t,a[N];

ll qmi(ll a,ll b){
	ll res=1;
	while(b){
		if(b&1ll) res=res*a%mod;
		b>>=1; a=a*a%mod;
	}
	return res;
}
P dif(P a,P b){
	ll res;
	ll mi=b.first;
	res=qmi(p,a.first-b.first)*a.second-b.second;
	if(res>1e6) return {-1,-1};
	return {b.first,res};
}
void work(){
	cin>>n>>p;
	for(ll i=0;i<n;i++) scanf("%lld",&a[i]);
	sort(a,a+n,greater<ll>());
	if(p==1){
		cout<<(n%2?1:0)<<endl; return ;
	}
	P sum={0,0};
	ll mdif=(log(1e6)/log(1.0*p));
	ll anoi=n;//幂差超最大限度后another集合直接加
	for(ll i=0;i<n;i++){
		if(sum.second==0) sum={a[i],1};
		else {
			P res=dif(sum,{a[i],1});
			if(res.first==-1){
				anoi=i; break;
			}else sum=res;
		}
	}
	ll ano=0;
	for(ll i=anoi;i<n;i++){
		ano=(ano+qmi(p,a[i]))%mod;
	}
	ll ans=qmi(p,sum.first)*sum.second%mod-ano;
	ans=(ans%mod+mod)%mod;
	cout<<ans<<endl;
	return ;
}

int main(){
	//work();
	for(cin>>t;t--;){
		work();
	}
	return 0;
}


#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
#define LL long long
#define go(G, x, i, v) \
  for (int i = G.hd[x], v = G.to[i]; i; v = G.to[i = G.nx[i]])

char read_str[1 << 25], *CH = read_str;
#define getchar() (*CH++)
LL read() {
  char ch = getchar();
  int fl = 0;
  LL x = 0;
  while (!isdigit(ch)) {
    if (ch == '-') fl = 1;
    ch = getchar();
  }
  while (isdigit(ch)) x = (x << 1) + (x << 3) + ch - '0', ch = getchar();
  return fl ? -x : x;
}
void rstr(char *s) {
  char *x = s;
  char ch = getchar();
  while (ch == ' ' || ch == '\n' || ch == '\r') ch = getchar();
  while (ch != ' ' && ch != '\n' && ch != '\r') {
    *x++ = ch;
    ch = getchar();
  }
}

const int Max_n = 1e6 + 5, mod = 1e9 + 7;
int T;
int n, p, ans, k[Max_n];
int top, stk[Max_n << 1], a[Max_n << 1];

namespace Input {
void main() {
  n = read(), p = read(), top = ans = 0;
  for (int i = 1; i <= n; i++) k[i] = read();
}
}  // namespace Input

namespace Init {
void main() {
  sort(k + 1, k + n + 1);
}
}  // namespace Init

namespace Solve {
int ksm(int a, int b) {
  int res = 1;
  for (; b; b >>= 1, a = (LL)a * a % mod)
    if (b & 1) res = (LL)res * a % mod;
  return res;
}
void add(int x) {
  int now = x;
  while (a[now] == p) a[now++] = 0;
  a[now]++, stk[++top] = now;
}
void main() {
  if (p == 1) {
    printf("%d\n", (n & 1));
    return;
  }
  int fl = 0;
  for (int i = n; i; i--) {
    if (!fl) {
      fl = k[i], ans = ksm(p, k[i]);
    } else {
      add(k[i]), stk[++top] = k[i], (ans += mod - ksm(p, k[i])) %= mod;
      if (a[fl]) a[fl] = 0, fl = top = 0;
    }
  }
  for (int i = 1; i <= top; i++) a[stk[i]] = 0;
  printf("%d\n", ans);
}
}  // namespace Solve

int main() {
#ifndef ONLINE_JUDGE
  freopen("B.in", "r", stdin);
  freopen("B.out", "w", stdout);
#endif
  fread(read_str, 1, 1 << 25, stdin);
  T = read();
  while (T--) {
    Input::main();
    Init::main();
    Solve::main();
  }
}


#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
#define LL long long
#define go(G, x, i, v) \
  for (int i = G.hd[x], v = G.to[i]; i; v = G.to[i = G.nx[i]])

char read_str[1 << 25], *CH = read_str;
#define getchar() (*CH++)
LL read() {
  char ch = getchar();
  int fl = 0;
  LL x = 0;
  while (!isdigit(ch)) {
    if (ch == '-') fl = 1;
    ch = getchar();
  }
  while (isdigit(ch)) x = (x << 1) + (x << 3) + ch - '0', ch = getchar();
  return fl ? -x : x;
}
void rstr(char *s) {
  char *x = s;
  char ch = getchar();
  while (ch == ' ' || ch == '\n' || ch == '\r') ch = getchar();
  while (ch != ' ' && ch != '\n' && ch != '\r') {
    *x++ = ch;
    ch = getchar();
  }
}

const int Max_n = 1e6 + 5, mod = 1e9 + 7;
int T;
int n, p, ans, k[Max_n];
int top, stk[Max_n << 1], a[Max_n << 1];

namespace Input {
void main() {
  n = read(), p = read(), top = ans = 0;
  for (int i = 1; i <= n; i++) k[i] = read();
}
}  // namespace Input

namespace Init {
void main() {
  sort(k + 1, k + n + 1);
}
}  // namespace Init

namespace Solve {
int ksm(int a, int b) {
  int res = 1;
  for (; b; b >>= 1, a = (LL)a * a % mod)
    if (b & 1) res = (LL)res * a % mod;
  return res;
}
void add(int x) {
  int now = x;
  while (a[now] == p - 1) a[now++] = 0;
  a[now]++, stk[++top] = now;
}
void main() {
  if (p == 1) {
    printf("%d\n", (n & 1));
    return;
  }
  int fl = 0;
  for (int i = n; i; i--) {
    if (!fl) {
      fl = k[i], ans = ksm(p, k[i]);
    } else {
      add(k[i]), stk[++top] = k[i], (ans += mod - ksm(p, k[i])) %= mod;
      if (a[fl]) a[fl] = 0, fl = top = 0;
    }
  }
  for (int i = 1; i <= top; i++) a[stk[i]] = 0;
  printf("%d\n", ans);
}
}  // namespace Solve

int main() {
#ifndef ONLINE_JUDGE
  freopen("B.in", "r", stdin);
  freopen("B.out", "w", stdout);
#endif
  fread(read_str, 1, 1 << 25, stdin);
  T = read();
  while (T--) {
    Input::main();
    Init::main();
    Solve::main();
  }
}


#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
#define LL long long
#define go(G, x, i, v) \
  for (int i = G.hd[x], v = G.to[i]; i; v = G.to[i = G.nx[i]])

char read_str[1 << 25], *CH = read_str;
#define getchar() (*CH++)
LL read() {
  char ch = getchar();
  int fl = 0;
  LL x = 0;
  while (!isdigit(ch)) {
    if (ch == '-') fl = 1;
    ch = getchar();
  }
  while (isdigit(ch)) x = (x << 1) + (x << 3) + ch - '0', ch = getchar();
  return fl ? -x : x;
}
void rstr(char *s) {
  char *x = s;
  char ch = getchar();
  while (ch == ' ' || ch == '\n' || ch == '\r') ch = getchar();
  while (ch != ' ' && ch != '\n' && ch != '\r') {
    *x++ = ch;
    ch = getchar();
  }
}

const int Max_n = 1e6 + 5, mod = 1e9 + 7;
int T;
int n, p, ans, k[Max_n];
int top, stk[Max_n], a[Max_n];

namespace Input {
void main() {
  n = read(), p = read(), top = ans = 0;
  for (int i = 1; i <= n; i++) k[i] = read();
}
}  // namespace Input

namespace Init {
void main() {
  sort(k + 1, k + n + 1);
}
}  // namespace Init

namespace Solve {
int ksm(int a, int b) {
  int res = 1;
  for (; b; b >>= 1, a = (LL)a * a % mod)
    if (b & 1) res = (LL)res * a % mod;
  return res;
}
void add(int x) {
  int now = x;
  while (a[now] == p - 1) a[now++] = 0;
  a[now]++, stk[++top] = now;
}
void main() {
  if (p == 1) {
    printf("%d\n", (n & 1));
    return;
  }
  int fl = 0;
  for (int i = n; i; i--) {
    if (!fl) {
      fl = k[i], ans = ksm(p, k[i]);
    } else {
      add(k[i]), stk[++top] = k[i], (ans += mod - ksm(p, k[i])) %= mod;
      if (a[fl]) a[fl] = 0, fl = top = 0;
    }
  }
  for (int i = 1; i <= top; i++) a[stk[i]] = 0;
  printf("%d\n", ans);
}
}  // namespace Solve

int main() {
#ifndef ONLINE_JUDGE
  freopen("B.in", "r", stdin);
  freopen("B.out", "w", stdout);
#endif
  fread(read_str, 1, 1 << 25, stdin);
  T = read();
  while (T--) {
    Input::main();
    Init::main();
    Solve::main();
  }
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
#define LL long long
#define go(G, x, i, v) \
  for (int i = G.hd[x], v = G.to[i]; i; v = G.to[i = G.nx[i]])

char read_str[1 << 25], *CH = read_str;
#define getchar() (*CH++)
LL read() {
  char ch = getchar();
  int fl = 0;
  LL x = 0;
  while (!isdigit(ch)) {
    if (ch == '-') fl = 1;
    ch = getchar();
  }
  while (isdigit(ch)) x = (x << 1) + (x << 3) + ch - '0', ch = getchar();
  return fl ? -x : x;
}
void rstr(char *s) {
  char *x = s;
  char ch = getchar();
  while (ch == ' ' || ch == '\n' || ch == '\r') ch = getchar();
  while (ch != ' ' && ch != '\n' && ch != '\r') {
    *x++ = ch;
    ch = getchar();
  }
}

const int Max_n = 1e6 + 7, mod = 1e9 + 7;
int n, p, a[Max_n];

int ksm(int a, int b) {
  int res = 1;
  for (; b; b >>= 1, a = (LL)a * a % mod)
    if (b & 1) res = (LL)res * a % mod;
  return res;
}
void solve() {
  n = read(), p = read();
  for (int i = 1; i <= n; i++) a[i] = read();
  if (p == 1) {
    printf("%d\n", (n & 1));
    return;
  }
  sort(a + 1, a + n + 1);
  int ans = ksm(p, a[n]);
  LL c = 1;
  int k = a[n], w = log(n) / log(p) + 1;
  for (int i = n - 1; i; i--) {
    if (k - a[i] > w) {
      if (c > 0)
        c = n;
      else
        if (c < 0) c = -n;
      k = a[i];
    } else {
      for (; k > a[i]; k--, c *= p)
        if (c > n)
          c = n;
        else
          if (c < -n) c = -n;
    }
    if (c <= 0)
      (ans += ksm(p, a[i])) %= mod, c++;
    else
      (ans += mod - ksm(p, a[i])) %= mod, c--;
  }
  printf("%d\n", ans);
}

int main() {
  freopen("B.in", "r", stdin);
  freopen("B.ans", "w", stdout);
  fread(read_str, 1, 1 << 25, stdin);
  int T = read();
  while (T--) solve();
}


#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
#define LL long long
#define go(G, x, i, v) \
  for (int i = G.hd[x], v = G.to[i]; i; v = G.to[i = G.nx[i]])

char read_str[1 << 25], *CH = read_str;
#define getchar() (*CH++)
LL read() {
  char ch = getchar();
  int fl = 0;
  LL x = 0;
  while (!isdigit(ch)) {
    if (ch == '-') fl = 1;
    ch = getchar();
  }
  while (isdigit(ch)) x = (x << 1) + (x << 3) + ch - '0', ch = getchar();
  return fl ? -x : x;
}
void rstr(char *s) {
  char *x = s;
  char ch = getchar();
  while (ch == ' ' || ch == '\n' || ch == '\r') ch = getchar();
  while (ch != ' ' && ch != '\n' && ch != '\r') {
    *x++ = ch;
    ch = getchar();
  }
}

const int Max_n = 1e6 + 5, mod = 1e9 + 7;
int T;
int n, p, ans, k[Max_n];
int top, stk[Max_n], a[Max_n];

namespace Input {
void main() {
  n = read(), p = read(), top = ans = 0;
  for (int i = 1; i <= n; i++) k[i] = read();
}
}  // namespace Input

namespace Init {
void main() {
  sort(k + 1, k + n + 1);
}
}  // namespace Init

namespace Solve {
int ksm(int a, int b) {
  int res = 1;
  for (; b; b >>= 1, a = (LL)a * a % mod)
    if (b & 1) res = (LL)res * a % mod;
  return res;
}
void add(int x) {
  int now = x;
  while (a[now] == p) a[now++] = 0;
  a[now] = 1, stk[++top] = now;
}
void main() {
  if (p == 1) {
    printf("%d\n", (n & 1));
    return;
  }
  int fl = 0;
  for (int i = n; i; i--) {
    if (!fl) {
      fl = k[i], ans = ksm(p, k[i]);
    } else {
      add(k[i]), stk[++top] = k[i], (ans += mod - ksm(p, k[i])) %= mod;
      if (a[fl]) a[fl] = 0, fl = top = 0;
    }
  }
  printf("%d\n", ans);
}
}  // namespace Solve

int main() {
#ifndef ONLINE_JUDGE
  freopen("B.in", "r", stdin);
  freopen("B.out", "w", stdout);
#endif
  fread(read_str, 1, 1 << 25, stdin);
  T = read();
  while (T--) {
    Input::main();
    Init::main();
    Solve::main();
  }
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
#define LL long long
#define go(G, x, i, v) \
  for (int i = G.hd[x], v = G.to[i]; i; v = G.to[i = G.nx[i]])

char read_str[1 << 25], *CH = read_str;
#define getchar() (*CH++)
LL read() {
  char ch = getchar();
  int fl = 0;
  LL x = 0;
  while (!isdigit(ch)) {
    if (ch == '-') fl = 1;
    ch = getchar();
  }
  while (isdigit(ch)) x = (x << 1) + (x << 3) + ch - '0', ch = getchar();
  return fl ? -x : x;
}
void rstr(char *s) {
  char *x = s;
  char ch = getchar();
  while (ch == ' ' || ch == '\n' || ch == '\r') ch = getchar();
  while (ch != ' ' && ch != '\n' && ch != '\r') {
    *x++ = ch;
    ch = getchar();
  }
}

const int Max_n = 1e6 + 7, mod = 1e9 + 7;
int n, p, a[Max_n];

int ksm(int a, int b) {
  int res = 1;
  for (; b; b >>= 1, a = (LL)a * a % mod)
    if (b & 1) res = (LL)res * a % mod;
  return res;
}
void solve() {
  n = read(), p = read();
  for (int i = 1; i <= n; i++) a[i] = read();
  if (p == 1) {
    printf("%d\n", (n & 1));
    return;
  }
  sort(a + 1, a + n + 1);
  int ans = ksm(p, a[n]);
  LL c = 1;
  int k = a[n], w = log(n) / log(p) + 1;
  for (int i = n - 1; i; i--) {
    if (k - a[i] > w) {
      if (c > 0)
        c = n;
      else
        if (c < 0) c = -n;
      k = a[i];
    } else {
      for (; k > a[i]; k--, c *= p)
        if (c > n)
          c = n;
        else
          if (c < -n) c = -n;
    }
    if (c <= 0)
      (ans += ksm(p, a[i])) %= mod, c++;
    else
      (ans += mod - ksm(p, a[i])) %= mod, c--;
  }
  printf("%d\n", ans);
}

int main() {
  fread(read_str, 1, 1 << 25, stdin);
  int T = read();
  while (T--) solve();
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define F first
#define S second
ll q, n, k, a[1000001], r, p[32], mp;
vector<pair<ll, ll>>v;
pair<ll, ll>v1;
bool b;
ll qpow(ll x){
    if(x==0)return 1;
    ll temp=qpow(x/2);
    temp*=temp;
    temp%=1000000007;
    return (temp*((x%2)*k+((x+1)%2)))%1000000007;
}
pair<ll, ll> f(ll x, ll c){
    if(x==v.size())return {v.size(), 0};
    if(v[x-1].F-v[x].F<=mp){
        ll c1=p[v[x-1].F-v[x].F];
        if(v[x].S>=c*c1)return {x, c*c1-v[x].S};
        return f(x+1, c*c1-v[x].S);
    }
    return {v.size(), 0};
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> q;
    while(q--){
        cin >> n >> k;
        for(int i=0; i<n; i++){
            cin >> a[i];
        }
        if(k==1){
            cout << n%2 << "\n";
            continue ;
        }
        p[0]=1;
        mp=0;
        for(int i=1; p[i-1]*k<=n; i++){
            p[i]=p[i-1]*k;
            mp=i;
        }
        v.clear();
        sort(a, a+n);
        reverse(a, a+n);
        v.push_back({a[0], 1});
        for(int i=1; i<n; i++){
            if(v[v.size()-1].F!=a[i])v.push_back({a[i], 0});
            v[v.size()-1].S++;
        }
        r=0;
        b=0;
        for(int i=0; i<v.size()&&!b; i++){
            if(!r){
                if(v[i].S%2!=0)r=qpow(v[i].F);
            }
            else{
                v1=f(i, 1);
                if(v1.F==v.size()){
                    b=1;
                    for(int t=i; t<v.size(); t++){
                        r=(r-(v[t].S*qpow(v[t].F))%1000000007+1000000007)%1000000007;
                    }
                }
                else{
                    r=0;
                    v[v1.F].S=v1.S;
                    i=v1.F;
                }
            }
        }
        cout << r << "\n";
    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define F first
#define S second
ll q, n, k, a[1000001], r, p[32], mp;
vector<pair<ll, ll>>v;
pair<ll, ll>v1;
bool b;
ll qpow(ll x){
    if(x==0)return 1;
    ll temp=qpow(x/2);
    temp*=temp;
    temp%=1000000007;
    return (temp*((x%2)*k+((x+1)%2)))%1000000007;
}
pair<ll, ll> f(ll x, ll c){
    if(x==v.size())return {v.size(), 0};
    if(v[x-1].F-v[x].F<=mp){
        ll c1=p[v[x-1].F-v[x].F];
        if(v[x].S>=c*c1)return {x, c*c1-v[x].S};
        return f(x+1, c*c1-v[x].S);
    }
    return {v.size(), 0};
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> q;
    while(q--){
        cin >> n >> k;
        for(int i=0; i<n; i++){
            cin >> a[i];
        }
        if(k==1){
            cout << n%2 << "\n";
            continue ;
        }
        p[0]=1;
        mp=0;
        for(int i=1; p[i-1]*k<=n; i++){
            p[i]=p[i-1]*k;
            mp=i;
        }
        v.clear();
        sort(a, a+n);
        reverse(a, a+n);
        v.push_back({a[0], 1});
        for(int i=1; i<n; i++){
            if(v[v.size()-1].F!=a[i])v.push_back({a[i], 0});
            v[v.size()-1].S++;
        }
        r=0;
        b=0;
        for(int i=0; i<v.size()&&!b; i++){
            if(!r){
                if(v[i].S%2!=0)r=qpow(v[i].F);
            }
            else{
                v1=f(i, 1);
                if(v1.F==v.size()){
                    b=1;
                    for(int t=i; t<v.size(); t++){
                        r=(r-(v[t].S*qpow(v[t].F))%1000000007+1000000007)%1000000007;
                    }
                }
                else{
                    r=0;
                    v[v1.F].S=v1.S;
                    i=v1.F-1;
                }
            }
        }
        cout << r << "\n";
    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define F first
#define S second
ll q, n, k, a[1000001], r, c;
vector<pair<ll, ll>>v;
ll qpow(ll x){
    if(x==0)return 1;
    ll temp=qpow(x/2);
    temp*=temp;
    return (temp*((x%2)*k+((x+1)%2)))%1000000007;
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> q;
    while(q--){
        cin >> n >> k;
        for(int i=0; i<n; i++){
            cin >> a[i];
        }
        if(k==1){
            cout << n%2 << "\n";
            continue ;
        }
        v.clear();
        sort(a, a+n);
        reverse(a, a+n);
        v.push_back({a[0], 1});
        for(int i=1; i<n; i++){
            if(v[v.size()-1].F!=a[i])v.push_back({a[i], 0});
            v[v.size()-1].S++;
        }
        r=0;
        for(int i=0; i<v.size(); i++){
            if(!r){
                if(v[i].S%2!=0)r=qpow(v[i].F);
            }
            else{
                c=qpow(v[i].F);
                while(r&&v[i].S){
                    r=(r-c+1000000007)%1000000007;
                    v[i].S--;
                }
                if(r==0){
                    if(v[i].S%2!=0)r=qpow(v[i].F);
                }
            }
        }
        cout << r << "\n";
    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define F first
#define S second
ll q, n, k, a[1000001], r, p[32], mp;
vector<pair<ll, ll>>v;
pair<ll, ll>v1;
bool b;
ll qpow(ll x){
    if(x==0)return 1;
    ll temp=qpow(x/2);
    temp*=temp;
    temp%=1000000007;
    return (temp*((x%2)*k+((x+1)%2)))%1000000007;
}
pair<ll, ll> f(ll x, ll c){
    if(x==v.size())return {v.size(), 0};
    if(v[x-1].F-v[x].F<=mp){
        ll c1=p[v[x-1].F-v[x].F];
        if(v[x].S>=c*c1)return {x, v[x].S-c*c1};
        return f(x+1, c*c1-v[x].S);
    }
    return {v.size(), 0};
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> q;
    while(q--){
        cin >> n >> k;
        for(int i=0; i<n; i++){
            cin >> a[i];
        }
        if(k==1){
            cout << n%2 << "\n";
            continue ;
        }
        p[0]=1;
        mp=0;
        for(int i=1; p[i-1]*k<=n; i++){
            p[i]=p[i-1]*k;
            mp=i;
        }
        v.clear();
        sort(a, a+n);
        reverse(a, a+n);
        v.push_back({a[0], 1});
        for(int i=1; i<n; i++){
            if(v[v.size()-1].F!=a[i])v.push_back({a[i], 0});
            v[v.size()-1].S++;
        }
        r=0;
        b=0;
        for(int i=0; i<v.size()&&!b; i++){
            if(!r){
                if(v[i].S%2!=0)r=qpow(v[i].F);
            }
            else{
                v1=f(i, 1);
                if(v1.F==v.size()){
                    b=1;
                    for(int t=i; t<v.size(); t++){
                        r=(r-(v[t].S*qpow(v[t].F))%1000000007+1000000007)%1000000007;
                    }
                }
                else{
                    r=0;
                    v[v1.F].S=v1.S;
                    i=v1.F-1;
                }
            }
        }
        cout << r << "\n";
    }
}


#include<bits/stdc++.h>
using namespace std;
#define maxn 1000005
#define maxm 2000005
#define inf 0x3f3f3f3f
#define int long long
#define mod 1000000007
#define local
void file(string s){freopen((s+".in").c_str(),"r",stdin);freopen((s+".out").c_str(),"w",stdout);}
template <typename Tp>void read(Tp &x){
	x=0;int fh=1;char c=getchar();
	while(c>'9'||c<'0'){if(c=='-'){fh=-1;}c=getchar();}
	while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}x*=fh;
}
int ksm(int b,int p){int ret=1;while(p){if(p&1)ret=ret*b%mod;b=b*b%mod;p>>=1;}return ret;}
int n,m;
int a[maxn],sum;
signed main(){
	int T;
	read(T);
	while(T--){
		read(n);read(m);
		sum=0;
		for(int i=1;i<=n;i++)read(a[i]);
		if(m==1){
			puts((n&1)?"1":"0");
			continue;
		}
		sort(a+1,a+n+1);
		int cursm=1;sum++;
		for(int i=n-1;i;i--){
			int dlt=a[i+1]-a[i];
			sum=sum*ksm(m,dlt)%mod;
			if(cursm){
				for(int j=1;cursm<=n&&j<=dlt;j++){
					cursm*=m;
				}
			}
			if(cursm>0){
				cursm--;sum=(sum+(mod-1))%mod;
			}
			else{
				cursm++;sum++;sum%=mod;
			}
		}
		sum=sum*ksm(m,a[1])%mod;
		printf("%lld\n",sum);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define maxn 1000005
#define maxm 2000005
#define inf 0x3f3f3f3f
#define int long long
#define mod 1000000007
#define local
void file(string s){freopen((s+".in").c_str(),"r",stdin);freopen((s+".out").c_str(),"w",stdout);}
template <typename Tp>void read(Tp &x){
	x=0;int fh=1;char c=getchar();
	while(c>'9'||c<'0'){if(c=='-'){fh=-1;}c=getchar();}
	while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}x*=fh;
}
int ksm(int b,int p){int ret=1;while(p){if(p&1)ret=ret*b%mod;b=b*b%mod;p>>=1;}return ret;}
int n,m;
int a[maxn],sum;
signed main(){
	int T;
	read(T);
	while(T--){
		read(n);read(m);
		sum=0;
		for(int i=1;i<=n;i++)read(a[i]);
		if(m==1){
			puts((n&1)?"1":"0");
			continue;
		}
		sort(a+1,a+n+1);
		int cursm=1;sum++;
		for(int i=n-1;i;i--){
			int dlt=a[i+1]-a[i];
			sum=sum*ksm(m,dlt)%mod;
			if(cursm){
				for(int j=1;cursm<=n&&j<=dlt;j++){
					cursm*=m;
				}
			}
			if(cursm>0){
				cursm--;sum--;
			}
			else{
				cursm++;sum++;
			}
		}
		sum=sum*ksm(m,a[1])%mod;
		printf("%lld\n",sum);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define maxn 1000005
#define maxm 2000005
#define inf 0x3f3f3f3f
#define int long long
#define mod 1000000007
#define local
void file(string s){freopen((s+".in").c_str(),"r",stdin);freopen((s+".out").c_str(),"w",stdout);}
template <typename Tp>void read(Tp &x){
	x=0;int fh=1;char c=getchar();
	while(c>'9'||c<'0'){if(c=='-'){fh=-1;}c=getchar();}
	while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}x*=fh;
}
int ksm(int b,int p){int ret=1;while(p){if(p&1)ret=ret*b%mod;b=b*b%mod;p>>=1;}return ret;}
int n,m;
int a[maxn],sum;
signed main(){
	int T;
	read(T);
	while(T--){
		read(n);read(m);
		sum=0;
		for(int i=1;i<=n;i++)read(a[i]);
		if(m==1){
			puts((n&1)?"1":"0");
			continue;
		}
		sort(a+1,a+n+1);
		int cursm=1;sum++;
		for(int i=n-1;i;i--){
			int dlt=a[i+1]-a[i];
			sum=sum*ksm(m,dlt)%mod;
			if(cursm){
				for(int j=1;cursm<=n&&j<=dlt;j++){
					cursm*=m;
				}
			}
			if(cursm>0){
				cursm--;sum--;
			}
			else{
				cursm++;sum++;
			}
		}
		sum=sum*ksm(m,a[1]);
		printf("%lld\n",sum);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=2e6+10;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
int n,m;
int k[N],f[N];
ll qpow(ll x,int y){
	ll ans=1;
	while (y){
		if (y&1)ans=ans*x%mod;
		y>>=1;x=x*x%mod;
	}
	return ans;
}
int main(){
	int T=1,x,y,p;
	cin>>T;
	while (T--){
		ll r=0;
		scanf("%d%d",&n,&p);
		for (int i=1;i<=n;i++)scanf("%d",&k[i]);
		if (p==1){
			printf("%d\n",n&1);
			continue;
		}
		sort(k+1,k+1+n);
		ll now=0,q=k[n];
        for (int i=n;i>0;i--){
        	if (now>i){
        		break;
			}
		    else if (now==0){
		    	n=i;
				now++;
				q=k[i];
			}
        	else if (k[i]<q){
        		now*=p;
        		q--;
        		i++;
			}
			else if (k[i]==q){
				now--;
			}
			else break;
		}
		ll ans=qpow(p,k[n]);
		for (int i=n-1;i>0;i--){
		    ans=(ans-qpow(p,k[i])+mod)%mod;
		}
		printf("%d\n",ans);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=2e6+10;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
int n,m;
int k[N],f[N];
ll qpow(ll x,int y){
	ll ans=1;
	while (y){
		if (y&1)ans=ans*x%mod;
		y>>=1;x=x*x%mod;
	}
	return ans;
}
int main(){
	int T=1,x,y,p;
	cin>>T;
	while (T--){
		ll r=0;
		scanf("%d%d",&n,&p);
		for (int i=1;i<=n;i++)scanf("%d",&k[i]);
		if (p==1){
			printf("%d",n&1);
			continue;
		}
		sort(k+1,k+1+n);
		ll now=0,q=k[n];
        for (int i=n;i>0;i--){
        	if (now>i){
        		break;
			}
		    else if (now==0){
		    	n=i;
				now++;
				q=k[i];
			}
        	else if (k[i]<q){
        		now*=p;
        		q--;
        		i++;
			}
			else if (k[i]==q){
				now--;
			}
			else break;
		}
		ll ans=qpow(p,k[n]);
		for (int i=n-1;i>0;i--){
		    ans=(ans-qpow(p,k[i])+mod)%mod;
		}
		printf("%d",ans);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=2e6+10;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
int n,m;
int k[N],f[N];
ll qpow(ll x,int y){
	ll ans=1;
	while (y){
		if (y&1)ans=ans*x%mod;
		y>>=1;x=x*x%mod;
	}
	return ans;
}
int main(){
	int T=1,x,y,p;
	cin>>T;
	while (T--){
		ll r=0;
		cin>>n>>p;
		for (int i=1;i<=n;i++)cin>>k[i];
		if (p==1){
			cout<<n%2<<endl;
			continue;
		}
		sort(k+1,k+1+n);
		while (k[n]==k[n-1]&&n>=2)n-=2;
		int tot=0,now=0;
		for (int i=1;i<=n;i++){
			if (k[i]==now)tot++;
			else{
				while (now<k[i]){
					f[now]=tot%p;
				    tot=tot/p;
					now++;
				}
				i--;
			}
		}
		while (tot){
			f[now]=tot%p;
			tot/=p;
			now++;
		}
		ll ans=0;
		for (int i=now-1;i>=0;i--){
			if (f[i]%2){
				ans+=qpow(p,i);
				for (int j=i-1;j>=0;j--){
					ans=(ans-f[j]*qpow(p,j)+mod)%mod;
				}
				break;
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=2e6+10;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
int n,m;
int k[N],f[N];
ll qpow(ll x,int y){
	ll ans=1;
	while (y){
		if (y&1)ans=ans*x%mod;
		y>>=1;x=x*x%mod;
	}
	return ans;
}
int main(){
	int T=1,x,y,p;
	cin>>T;
	while (T--){
		ll r=0;
		cin>>n>>p;
		for (int i=1;i<=n;i++)cin>>k[i];
		if (p==1){
			cout<<n%2<<endl;
			continue;
		}
		sort(k+1,k+1+n);
		ll now=0,q=k[n];
        for (int i=n;i>0;i--){
        	if (now>i){
        		break;
			}
		    else if (now==0){
		    	n=i;
				now++;
				q=k[i];
			}
        	else if (k[i]<q){
        		now*=p;
        		q--;
        		i++;
			}
			else if (k[i]==q){
				now--;
			}
			else break;
		}
		ll ans=qpow(p,k[n]);
		for (int i=n-1;i>0;i--){
		    ans=(ans-qpow(p,k[i])+mod)%mod;
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

typedef pair<ll, ll> par;
typedef pair<par, int> tri;

int T;
ll n, m;
int a;
vector<int> V;
vector<int> V1;
vector<int> S;

ll pot[1000005];

ll mod = 1000000007ll;

ll powm( ll x, ll y ){
    if(y == 0 ) return 1ll;
    if(y == 1 ) return x;

    if( y % 2 == 0 ){
        return powm( (x*x)%mod, y/2 )%mod;
    }

    return (x*powm(x, y-1))%mod;
}

int main()
{
   //freopen("a.in", "r", stdin);
   // freopen("a.out", "w", stdout);

       cin.tie(0);
    ios_base::sync_with_stdio(0);

    cin >> T;

    while( T -- ){
        cin  >> n >> m;

        V.clear();
        for( int i = 1; i <= n; i ++ ){
            cin >> a;
            V.push_back(a*-1);
        }

        if( m == 1 ){
            cout << n%2 << '\n';
            continue;
        }

        sort(V.begin(), V.end());
        int ant = -1, dif=-1;
        ll arr = 0, ten = 0;
        int i = 0;
        bool band = false;
        ll sol = 0;
        for( i = 0; i < n; i ++ ){
            int v = -1*V[i];
            while( !band && ant != -1 && ant != v  ){
                ant --;
                arr *= m;
                ten *= m;
                if(arr-ten > n)
                    band = true;
            }

            if( ant == -1 ){
                arr = 1;
                sol = powm(m,v);
                ant = v;
                band = false;
                ten = 0;
            }else
                if( !band ){
                    ten ++;
                    sol = (sol -  powm(m,v) + mod)%mod;

                    if( ten == arr ){
                        sol = 0;
                        ant = -1;
                    }
                }else{
                    sol = (sol -  powm(m,v) + mod)%mod;
                }
        }

        cout << sol << '\n';
    }

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

typedef pair<ll, ll> par;
typedef pair<par, int> tri;

int T;
ll n, m;
int a;
vector<int> V;
vector<int> S;

ll mod = 1e9+7;

ll powm( ll x, ll y ){
    if(y == 0 ) return 1ll;
    if(y == 1 ) return x;

    if( y % 2 == 0 ){
        return powm( (x*x)%mod, y/2 )%mod;
    }

    return (x*powm(x, y-1))%mod;
}

int main()
{
   //freopen("a.in", "r", stdin);
   // freopen("a.out", "w", stdout);

    cin >> T;

    while( T -- ){
        cin  >> n >> m;
        V.clear();
        S.clear();
        for( int i = 1; i <= n; i ++ ){
            cin >> a;
            V.push_back(a);
        }

        sort(V.begin(), V.end());

        int t = 0;
        int ant = -1;
        for( int i = 0; i < n; i ++ ){
            if( ant != V[i] ){
                t %= 2;
                if( t == 1 )
                    S.push_back( ant );

                t = 0;
            }

            t ++;
            ant = V[i];
        }

        t %= 2;
        if( t == 1 )
            S.push_back( ant );

        int ta = S.size();

        ll sol = 0;
        for( int i = 0; i < ta; i ++ ){
            if( i +1==ta ){
                sol = (powm(m,S[i]) - sol + mod)%mod;
            }else{
                sol = (sol + powm(m,S[i]))%mod;
            }
        }

        cout << ((m == 1)? mod - sol : sol) << '\n' ;

    }

   // cout << powm(5,100) << endl;

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

typedef pair<ll, ll> par;
typedef pair<par, int> tri;

int T;
ll n, m;
int a;
vector<int> V;
vector<int> S;

ll mod = 1e9+7;

ll powm( ll x, ll y ){
    if(y == 0 ) return 1ll;
    if(y == 1 ) return x;

    if( y % 2 == 0 ){
        return powm( (x*x)%mod, y/2 )%mod;
    }

    return (x*powm(x, y-1))%mod;
}

int main()
{
   //freopen("a.in", "r", stdin);
   // freopen("a.out", "w", stdout);

    cin >> T;

    while( T -- ){
        cin  >> n >> m;
        V.clear();
        S.clear();
        for( int i = 1; i <= n; i ++ ){
            cin >> a;
            V.push_back(a);
        }

        sort(V.begin(), V.end());

        int t = 0;
        int ant = -1;
        for( int i = 0; i < n; i ++ ){
            if( ant != V[i] ){
                t %= 2;
                if( t == 1 )
                    S.push_back( ant );

                t = 0;
            }

            t ++;
            ant = V[i];
        }

        t %= 2;
        if( t == 1 )
            S.push_back( ant );

        int ta = S.size();

        ll sol = 0;
        for( int i = 0; i < ta; i ++ ){
            if( i +1==ta ){
                sol = (powm(m,S[i]) - sol + mod)%mod;
            }else{
                sol = (sol + powm(m,S[i]))%mod;
            }
        }

        cout << ((m == 1)? mod - sol : sol);
        if( T )
         cout << '\n' ;

    }

   // cout << powm(5,100) << endl;

    return 0;
}


class Sigment:
  def __init__(self, l =1, r=1):
    self.l, self.r = l,r

n = int(input())
ans =[]
mark= 0
index=[-1]
for i in range (n):
  sigment = Sigment()
  sigment.l, sigment.r = map(int,input().split())
  if i==0:
    ans.append(sigment.l)
    ans.append(sigment.r)
  else:
    if sigment.l < ans[0]:
      ans[0]=sigment.l
      ans[1]=sigment.r
      index.append(i+1)
      mark =1
    elif sigment.l == ans[0]:
      if sigment.r >= ans[1]:
        ans[0]=sigment.l
        ans[1]=sigment.r
        mark =1
        index.append(i+1)

# def seek (a,b):
#   for i in range (len(a)):
#     if a[i]==min(a):
#       if b[i]== max(b):
#         return (i+1)
#   return -1
# print(seek(L_list, R_list))
# print(ans)
# print(mark)
if mark ==1:
  print(index[-1])
else:
  print(index[0])


import sys
sys.setrecursionlimit(100000)

n = int(input())
s1 = []
s2 =[]
for i in range (n):
  r, l = map(int, input().split())
  s1.append(r)
  s2.append(l)

def big_segment (a1,a2):
  for i in range (len(a1)):
    if a1[i]== min(a1):
      if a2[i] == max(a2):
        return i+1
  return -1

print(big_segment(s1,s2))