#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
typedef long long ll;
ll p=999998639;

long long surplus(long long a,long long b)
{
	if(b==0&&p==1)return 0;
    long long result=1;
    long long base=a%p;
    while(b)
    {
        if(b&1) result=(result*base)%p;//如果模不超int，这里追求快速的话可以省去mul
        base=(base*base)%p;
        b>>=1;//b/=2
    }
    return result;
}
ll a[1000005];
queue<ll>t1,t2;
int main(){
	ll n,t,i,j,m,x;
	scanf("%lld",&t);
	while(t--){
		scanf("%lld%lld",&n,&x);
		for(i=1;i<=n;i++){
			scanf("%lld",&a[i]);
		}
		sort(a+1,a+n+1);
		ll ans1=0,ans2=0;
		for(i=n;i>=1;i--){
			if(ans1==0&&ans2==0){
				ans1=(surplus(x,a[i]))%p;
				p=1e9+7;
				ans2=(surplus(x,a[i]))%p;
				p=999998639;
				t1.push(a[i]);
			}
			else {
				ans1=(ans1-surplus(x,a[i])+p)%p;
				p=1e9+7;
				ans2=(ans2-surplus(x,a[i])+p)%p;
				p=999998639;
				t2.push(a[i]);
			}
		}
		p=1e9+7;
		ll ans=0;
		while(!t1.empty()){
			ans=(ans+surplus(x,t1.front()))%p;
			t1.pop();
		}
		ll sum=0;
		while(!t2.empty()){
			sum=(sum+surplus(x,t2.front()))%p;
			t2.pop();
		}
		printf("%lld\n",(abs(ans-sum))%p);
	}

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
typedef long long ll;
ll p=999998639;

long long surplus(long long a,long long b)
{
	if(b==0&&p==1)return 0;
    long long result=1;
    long long base=a%p;
    while(b)
    {
        if(b&1) result=(result*base)%p;//如果模不超int，这里追求快速的话可以省去mul
        base=(base*base)%p;
        b>>=1;//b/=2
    }
    return result;
}
ll a[1000005];

int main(){
	ll n,t,i,j,m,x;
	scanf("%lld",&t);
	while(t--){
		scanf("%lld%lld",&n,&x);
		for(i=1;i<=n;i++){
			scanf("%lld",&a[i]);
		}
		sort(a+1,a+n+1);
		ll ans1=0,ans2=0;
		for(i=n;i>=1;i--){
			if(ans1==0&&ans2==0){
				ans1=(surplus(x,a[i]))%p;
				p=1e9+7;
				ans2=(surplus(x,a[i]))%p;
				p=999998639;

			}
			else {
				ans1=(ans1-surplus(x,a[i])+p)%p;
				p=1e9+7;
				ans2=(ans2-surplus(x,a[i])+p)%p;
				p=999998639;

			}
		}

		printf("%lld\n",ans2);
	}

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
typedef long long ll;
ll p=999998639;
ll P=1e9+7;

long long surplus(long long a,long long b)
{
	if(b==0&&P==1)return 0;
    long long result=1;
    long long base=a%P;
    while(b)
    {
        if(b&1) result=(result*base)%P;//如果模不超int，这里追求快速的话可以省去mul
        base=(base*base)%P;
        b>>=1;//b/=2
    }
    return result;
}
long long surplus2(long long a,long long b)
{
	if(b==0&&p==1)return 0;
    long long result=1;
    long long base=a%p;
    while(b)
    {
        if(b&1) result=(result*base)%p;//如果模不超int，这里追求快速的话可以省去mul
        base=(base*base)%p;
        b>>=1;//b/=2
    }
    return result;
}
ll a[1000005];
queue<ll>t1,t2;
int main(){
	ll n,t,i,j,m,x;
	scanf("%lld",&t);
	while(t--){
		scanf("%lld%lld",&n,&x);
		for(i=1;i<=n;i++){
			scanf("%lld",&a[i]);
		}
		sort(a+1,a+n+1);
		ll ans1=0,ans2=0;
		for(i=n;i>=1;i--){
			if(ans1==0&&ans2==0){
				ans1=surplus(x,a[i]);
				ans2=surplus2(x,a[i]);
				t1.push(a[i]);
			}
			else {
				ans1=(ans1-surplus(x,a[i])+P)%P;
				ans2=(ans2-surplus2(x,a[i])+p)%p;
				t2.push(a[i]);
			}
		}

		printf("%lld\n",ans1);
	}

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
typedef long long ll;
ll p=999998639;
ll P=1e9+7;

long long surplus(long long a,long long b)
{
	if(b==0&&P==1)return 0;
    long long result=1;
    long long base=a%P;
    while(b)
    {
        if(b&1) result=(result*base)%P;//如果模不超int，这里追求快速的话可以省去mul
        base=(base*base)%P;
        b>>=1;//b/=2
    }
    return result;
}

ll a[1000005];
queue<ll>t1,t2;
int main(){
	ll n,t,i,j,m,x;
	scanf("%lld",&t);
	while(t--){
		scanf("%lld%lld",&n,&x);
		for(i=1;i<=n;i++){
			scanf("%lld",&a[i]);
		}
		sort(a+1,a+n+1);
		ll ans1=0,ans2=0;
		for(i=n;i>=1;i--){
			if(ans1==0&&ans2==0){
				ans1=surplus(x,a[i]);
				P=999998639;
				ans2=surplus(x,a[i]);
				P=1e9+7;
				t1.push(a[i]);
			}
			else {
				ans1=(ans1-surplus(x,a[i])+P)%P;
				P=999998639;
				ans2=(ans2-surplus(x,a[i])+P)%P;
				p=1e9+7;
				t2.push(a[i]);
			}
		}

		printf("%lld\n",ans1);
	}

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
typedef long long ll;
ll p=999998639;

long long surplus(long long a,long long b)
{
	if(b==0&&p==1)return 0;
    long long result=1;
    long long base=a%p;
    while(b)
    {
        if(b&1) result=(result*base)%p;//如果模不超int，这里追求快速的话可以省去mul
        base=(base*base)%p;
        b>>=1;//b/=2
    }
    return result;
}
ll a[1000005];
queue<ll>t1,t2;
int main(){
	ll n,t,i,j,m,x;
	scanf("%lld",&t);
	while(t--){
		scanf("%lld%lld",&n,&x);
		for(i=1;i<=n;i++){
			scanf("%lld",&a[i]);
		}
		sort(a+1,a+n+1);
		ll ans1=0,ans2=0;
		for(i=n;i>=1;i--){
			if(ans1==0&&ans2==0){
				ans1=(surplus(x,a[i]))%p;
				p=100001651;
				ans2=(surplus(x,a[i]))%p;
				p=999998639;
				t1.push(a[i]);
			}
			else {
				ans1=(ans1-surplus(x,a[i])+p)%p;
				p=100001651;
				ans2=(ans2-surplus(x,a[i])+p)%p;
				p=999998639;
				t2.push(a[i]);
			}
		}
		p=1e9+7;
		ll ans=0;
		while(!t1.empty()){
			ans=(ans+surplus(x,t1.front()))%p;
			t1.pop();
		}
		ll sum=0;
		while(!t2.empty()){
			sum=(sum+surplus(x,t2.front()))%p;
			t2.pop();
		}
		printf("%lld\n",(abs(ans-sum))%p);
	}

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
typedef long long ll;
ll p=998244353;

long long surplus(long long a,long long b)
{
	if(b==0&&p==1)return 0;
    long long result=1;
    long long base=a%p;
    while(b)
    {
        if(b&1) result=(result*base)%p;//如果模不超int，这里追求快速的话可以省去mul
        base=(base*base)%p;
        b>>=1;//b/=2
    }
    return result;
}
ll a[1000005];
queue<ll>t1,t2;
int main(){
	ll n,t,i,j,m,x;
	cin>>t;
	while(t--){
		cin>>n>>x;
		for(i=1;i<=n;i++){
			cin>>a[i];
		}
		sort(a+1,a+n+1);
		ll ans1=0,ans2=0;
		for(i=n;i>=1;i--){
			if(ans1==0&&ans2==0){
				ans1=(surplus(x,a[i]))%p;
				p=100001651;
				ans2=(surplus(x,a[i]))%p;
				p=998244353;
				t1.push(a[i]);
			}
			else {
				ans1=(ans1-surplus(x,a[i])+p)%p;
				p=100001651;
				ans2=(ans2+surplus(x,a[i])+p)%p;
				p=998244353;
				t2.push(a[i]);
			}
		}
		p=1e9+7;
		ll ans=0;
		while(!t1.empty()){
			ans=(ans+surplus(x,t1.front()))%p;
			t1.pop();
		}
		ll sum=0;
		while(!t2.empty()){
			sum=(sum+surplus(x,t2.front()))%p;
			t2.pop();
		}
		cout<<abs(ans-sum)%p<<endl;
	}

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
typedef long long ll;
ll p=998244353;
ll P=1e9+7;

long long surplus(long long a,long long b)
{
	if(b==0&&P==1)return 0;
    long long result=1;
    long long base=a%P;
    while(b)
    {
        if(b&1) result=(result*base)%P;//如果模不超int，这里追求快速的话可以省去mul
        base=(base*base)%P;
        b>>=1;//b/=2
    }
    return result;
}
long long surplus2(long long a,long long b)
{
	if(b==0&&p==1)return 0;
    long long result=1;
    long long base=a%p;
    while(b)
    {
        if(b&1) result=(result*base)%p;//如果模不超int，这里追求快速的话可以省去mul
        base=(base*base)%p;
        b>>=1;//b/=2
    }
    return result;
}
ll a[1000005];
queue<ll>t1,t2;
int main(){
	ll n,t,i,j,m,x;
	scanf("%lld",&t);
	while(t--){
		scanf("%lld%lld",&n,&x);
		for(i=1;i<=n;i++){
			scanf("%lld",&a[i]);
		}
		sort(a+1,a+n+1);
		ll ans1=0,ans2=0;
		for(i=n;i>=1;i--){
			if(ans1==0&&ans2==0){
				ans1=surplus(x,a[i]);
				ans2=surplus2(x,a[i]);
				t1.push(a[i]);
			}
			else {
				ans1=(ans1-surplus(x,a[i])+P)%P;
				ans2=(ans2-surplus2(x,a[i])+p)%p;
				t2.push(a[i]);
			}
		}

		printf("%lld\n",ans1);
	}

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
typedef long long ll;
ll p=999998639;

long long surplus(long long a,long long b)
{
	if(b==0&&p==1)return 0;
    long long result=1;
    long long base=a%p;
    while(b)
    {
        if(b&1) result=(result*base)%p;//如果模不超int，这里追求快速的话可以省去mul
        base=(base*base)%p;
        b>>=1;//b/=2
    }
    return result;
}
ll a[1000005];
queue<ll>t1,t2;
int main(){
	ll n,t,i,j,m,x;
	scanf("%lld",&t);
	while(t--){
		scanf("%lld%lld",&n,&x);
		for(i=1;i<=n;i++){
			scanf("%lld",&a[i]);
		}
		sort(a+1,a+n+1);
		ll ans1=0,ans2=0;
		for(i=n;i>=1;i--){
			if(ans1==0&&ans2==0){
				ans1=(surplus(x,a[i]))%p;
				p=1e9+7;
				ans2=(surplus(x,a[i]))%p;
				p=999998639;
				t1.push(a[i]);
			}
			else {
				ans1=(ans1-surplus(x,a[i])+p)%p;
				p=1e9+7;
				ans2=(ans2-surplus(x,a[i])+p)%p;
				p=999998639;
				t2.push(a[i]);
			}
		}
		p=1e9+7;

		printf("%lld\n",ans1);
	}

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
typedef long long ll;
ll p=998244353;

long long surplus(long long a,long long b)
{
	if(b==0&&p==1)return 0;
    long long result=1;
    long long base=a%p;
    while(b)
    {
        if(b&1) result=(result*base)%p;//如果模不超int，这里追求快速的话可以省去mul
        base=(base*base)%p;
        b>>=1;//b/=2
    }
    return result;
}
ll a[1000005];
queue<ll>t1,t2;
int main(){
	ll n,t,i,j,m,x;
	cin>>t;
	while(t--){
		cin>>n>>x;
		for(i=1;i<=n;i++){
			cin>>a[i];
		}
		sort(a+1,a+n+1);
		ll ans1=0,ans2=0,ans3=0,ans4=0;
		for(i=n;i>=1;i--){
			if(ans2==ans1&&ans4==ans3){
				ans1=(surplus(x,a[i]))%p;
				p=100001651;
				ans3=(surplus(x,a[i]))%p;
				p=998244353;
				t1.push(a[i]);
			}
			else {
				ans2=(ans2+surplus(x,a[i]))%p;
				p=100001651;
				ans4=(ans4+surplus(x,a[i]))%p;
				p=998244353;
				t2.push(a[i]);
			}
		}
		p=1e9+7;
		ll ans=0;
		while(!t1.empty()){
			ans=(ans+surplus(x,t1.front()))%p;
			t1.pop();
		}
		ll sum=0;
		while(!t2.empty()){
			sum=(sum+surplus(x,t2.front()))%p;
			t2.pop();
		}
		cout<<abs(ans-sum)%p<<endl;
	}

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
typedef long long ll;
ll p=999998639;

long long surplus(long long a,long long b)
{
	if(b==0&&p==1)return 0;
    long long result=1;
    long long base=a%p;
    while(b)
    {
        if(b&1) result=(result*base)%p;//如果模不超int，这里追求快速的话可以省去mul
        base=(base*base)%p;
        b>>=1;//b/=2
    }
    return result;
}
ll a[1000005];

int main(){
	ll n,t,i,j,m,x;
	scanf("%lld",&t);
	while(t--){
		scanf("%lld%lld",&n,&x);
		for(i=1;i<=n;i++){
			scanf("%lld",&a[i]);
		}
		sort(a+1,a+n+1);
		ll ans1=0,ans2=0,ans3=0,ans4=0;
		for(i=n;i>=1;i--){
			if(ans2==ans1&&ans4==ans3){
				ans1=(surplus(x,a[i]))%p;
				p=1e9+7;
				ans3=(surplus(x,a[i]))%p;
				p=999998639;
				ans2=0;ans4=0;

			}
			else {
				ans2=(ans2+surplus(x,a[i]))%p;
				p=1e9+7;
				ans4=(ans4+surplus(x,a[i]))%p;
				p=999998639;

			}
		}
		printf("%lld\n",ans3-ans4);
	}

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
typedef long long ll;
ll p=998244353;

long long mul(long long a,long long b) //此函数是对两个数相乘可能超范围的处理
{
    long long ans=0;
    while(b)
    {
        if(b&1) ans=(ans+a)%p;
        a=(a+a)%p;
        b>>=1;//b/=2
    }
    return ans;
}
long long surplus(long long a,long long b)
{
	if(b==0&&p==1)return 0;
    long long result=1;
    long long base=a%p;
    while(b)
    {
        if(b&1) result=mul(result,base)%p;//如果模不超int，这里追求快速的话可以省去mul
        base=mul(base,base)%p;
        b>>=1;//b/=2
    }
    return result;
}
ll a[1000005];
queue<ll>t1,t2;
int main(){
	ll n,t,i,j,m,x;
	cin>>t;
	while(t--){
		cin>>n>>x;
		for(i=1;i<=n;i++){
			cin>>a[i];
		}
		sort(a+1,a+n+1);
		ll ans1=0,ans2=0;
		for(i=n;i>=1;i--){
			if(ans2>ans1){
				ans1=(ans1+surplus(x,a[i]))%p;
				t1.push(a[i]);
			}
			else {
				ans2=(ans2+surplus(x,a[i]))%p;
				t2.push(a[i]);
			}
		}
		p=1e9+7;
		ll ans=0;
		while(!t1.empty()){
			ans=(ans+surplus(x,t1.front()))%p;
			t1.pop();
		}
		ll sum=0;
		while(!t2.empty()){
			sum=(sum+surplus(x,t2.front()))%p;
			t2.pop();
		}
		cout<<abs(ans-sum)%p<<endl;
	}

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
typedef long long ll;
ll p=998244353;

long long surplus(long long a,long long b)
{
	if(b==0&&p==1)return 0;
    long long result=1;
    long long base=a%p;
    while(b)
    {
        if(b&1) result=(result*base)%p;//如果模不超int，这里追求快速的话可以省去mul
        base=(base*base)%p;
        b>>=1;//b/=2
    }
    return result;
}
ll a[1000005];
queue<ll>t1,t2;
int main(){
	ll n,t,i,j,m,x;
	cin>>t;
	while(t--){
		cin>>n>>x;
		for(i=1;i<=n;i++){
			cin>>a[i];
		}
		sort(a+1,a+n+1);
		ll ans1=0,ans2=0,ans3=0,ans4=0;
		for(i=n;i>=1;i--){
			if(ans2>ans1&&ans4>ans3){
				ans1=(ans1+surplus(x,a[i]))%p;
				p=100001651;
				ans3=(ans3+surplus(x,a[i]))%p;
				p=998244353;
				t1.push(a[i]);
			}
			else {
				ans2=(ans2+surplus(x,a[i]))%p;
				p=100001651;
				ans4=(ans4+surplus(x,a[i]))%p;
				p=998244353;
				t2.push(a[i]);
			}
		}
		p=1e9+7;
		ll ans=0;
		while(!t1.empty()){
			ans=(ans+surplus(x,t1.front()))%p;
			t1.pop();
		}
		ll sum=0;
		while(!t2.empty()){
			sum=(sum+surplus(x,t2.front()))%p;
			t2.pop();
		}
		cout<<abs(ans-sum)%p<<endl;
	}

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
typedef long long ll;
ll P=1e9+7;

long long surplus(long long a,long long b)
{
	if(b==0&&P==1)return 0;
    long long result=1;
    long long base=a%P;
    while(b)
    {
        if(b&1) result=(result*base)%P;//如果模不超int，这里追求快速的话可以省去mul
        base=(base*base)%P;
        b>>=1;//b/=2
    }
    return result;
}

ll a[1000005];
queue<ll>t1,t2;
int main(){
	ll n,t,i,j,m,x;
	scanf("%lld",&t);
	while(t--){
		scanf("%lld%lld",&n,&x);
		for(i=1;i<=n;i++){
			scanf("%lld",&a[i]);
		}
		sort(a+1,a+n+1);
		ll ans1=0,ans2=0;
		for(i=n;i>=1;i--){
			if(ans1==0&&ans2==0){
				ans1=surplus(x,a[i]);
				P=999998639;
				ans2=surplus(x,a[i]);
				P=1e9+7;
				t1.push(a[i]);
			}
			else {
				ans1=(ans1-surplus(x,a[i])+P)%P;
				P=999998639;
				ans2=(ans2-surplus(x,a[i])+P)%P;
				P=1e9+7;
				t2.push(a[i]);
			}
		}

		printf("%lld\n",ans1);
	}

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
typedef long long ll;
ll p=999998639;

long long surplus(long long a,long long b)
{
	if(b==0&&p==1)return 0;
    long long result=1;
    long long base=a%p;
    while(b)
    {
        if(b&1) result=(result*base)%p;//如果模不超int，这里追求快速的话可以省去mul
        base=(base*base)%p;
        b>>=1;//b/=2
    }
    return result;
}
ll a[1000005];

int main(){
	ll n,t,i,j,m,x;
	scanf("%lld",&t);
	while(t--){
		scanf("%lld%lld",&n,&x);
		for(i=1;i<=n;i++){
			scanf("%lld",&a[i]);
		}
		sort(a+1,a+n+1);
		ll ans1=0,ans2=0;
		for(i=n;i>=1;i--){
			if(ans1==0&&ans2==0){
				ans1=(surplus(x,a[i]))%p;
				p=1e9+7;
				ans2=(surplus(x,a[i]))%p;
				p=999998639;
				t1.push(a[i]);
			}
			else {
				ans1=(ans1-surplus(x,a[i])+p)%p;
				p=1e9+7;
				ans2=(ans2-surplus(x,a[i])+p)%p;
				p=999998639;
				t2.push(a[i]);
			}
		}

		printf("%lld\n",ans2);
	}

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
typedef long long ll;
ll p=998244353;

long long surplus(long long a,long long b)
{
	if(b==0&&p==1)return 0;
    long long result=1;
    long long base=a%p;
    while(b)
    {
        if(b&1) result=(result*base)%p;//如果模不超int，这里追求快速的话可以省去mul
        base=(base*base)%p;
        b>>=1;//b/=2
    }
    return result;
}
ll a[1000005];
queue<ll>t1,t2;
int main(){
	ll n,t,i,j,m,x;
	cin>>t;
	while(t--){
		cin>>n>>x;
		for(i=1;i<=n;i++){
			cin>>a[i];
		}
		sort(a+1,a+n+1);
		ll ans1=0,ans2=0;
		for(i=n;i>=1;i--){
			if(ans1==0&&ans2==0){
				ans1=(surplus(x,a[i]))%p;
				p=100001651;
				ans2=(surplus(x,a[i]))%p;
				p=998244353;
				t1.push(a[i]);
			}
			else {
				ans1=(ans1-surplus(x,a[i])+p)%p;
				p=100001651;
				ans2=(ans2-surplus(x,a[i])+p)%p;
				p=998244353;
				t2.push(a[i]);
			}
		}
		p=1e9+7;
		ll ans=0;
		while(!t1.empty()){
			ans=(ans+surplus(x,t1.front()))%p;
			t1.pop();
		}
		ll sum=0;
		while(!t2.empty()){
			sum=(sum+surplus(x,t2.front()))%p;
			t2.pop();
		}
		cout<<(abs(ans-sum))%p<<endl;
	}

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
typedef long long ll;
ll p=998244353;

long long surplus(long long a,long long b)
{
	if(b==0&&p==1)return 0;
    long long result=1;
    long long base=a%p;
    while(b)
    {
        if(b&1) result=(result*base)%p;//如果模不超int，这里追求快速的话可以省去mul
        base=(base*base)%p;
        b>>=1;//b/=2
    }
    return result;
}
ll a[1000005];
queue<ll>t1,t2;
int main(){
	ll n,t,i,j,m,x;
	cin>>t;
	while(t--){
		cin>>n>>x;
		for(i=1;i<=n;i++){
			cin>>a[i];
		}
		sort(a+1,a+n+1);
		ll ans1=0,ans2=0;
		for(i=n;i>=1;i--){
			if(ans1==0&&ans2==0){
				ans1=(surplus(x,a[i]))%p;
				p=100001651;
				ans2=(surplus(x,a[i]))%p;
				p=998244353;
				t1.push(a[i]);
			}
			else {
				ans1=(ans1-surplus(x,a[i])+p)%p;
				p=100001651;
				ans2=(ans2-surplus(x,a[i])+p)%p;
				p=998244353;
				t2.push(a[i]);
			}
		}
		p=1e9+7;
		ll ans=0;
		while(!t1.empty()){
			ans=(ans+surplus(x,t1.front()))%p;
			t1.pop();
		}
		ll sum=0;
		while(!t2.empty()){
			sum=(sum+surplus(x,t2.front()))%p;
			t2.pop();
		}
		cout<<abs(ans-sum)%p<<endl;
	}

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
typedef long long ll;
ll p=998244353;
ll P=1e9+7;

long long surplus(long long a,long long b)
{
	if(b==0&&P==1)return 0;
    long long result=1;
    long long base=a%P;
    while(b)
    {
        if(b&1) result=(result*base)%P;//如果模不超int，这里追求快速的话可以省去mul
        base=(base*base)%P;
        b>>=1;//b/=2
    }
    return result;
}
long long surplus2(long long a,long long b)
{
	if(b==0&&p==1)return 0;
    long long result=1;
    long long base=a%p;
    while(b)
    {
        if(b&1) result=(result*base)%p;//如果模不超int，这里追求快速的话可以省去mul
        base=(base*base)%p;
        b>>=1;//b/=2
    }
    return result;
}
ll a[1000005];
queue<ll>t1,t2;
int main(){
	ll n,t,i,j,m,x;
	cin>>t;
	while(t--){
		cin>>n>>x;
		for(i=1;i<=n;i++){
			cin>>a[i];
		}
		sort(a+1,a+n+1);
		ll ans1=0,ans2=0;
		for(i=n;i>=1;i--){
			if(ans1==0&&ans2==0){
				ans1=surplus(x,a[i]);
				ans2=surplus2(x,a[i]);
				t1.push(a[i]);
			}
			else {
				ans1=(ans1-surplus(x,a[i])+P)%P;
				ans2=(ans2-surplus2(x,a[i])+p)%p;
				t2.push(a[i]);
			}
		}

		cout<<ans1<<endl;
	}

    return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
#define int long long
#define maxn (1000010)
#define mod (1000000007)
int a[maxn], n, p;
bool have_mod;
int calcmod(int a) {
	have_mod = bool(a > a % mod);
	return a % mod;
}
int pow_mod(int a, int n) {
	have_mod = false;
	int ret = 1;
	while(n) {
		if(n & 1) ret = calcmod(ret * a);
		a = (a * a) % mod;
		n >>= 1;
	}
	return ret;
}
int calc(int n) {
	int k = a[n], cnt = 1, i = n-1;
	while(i > 0 && cnt > 0) {
		have_mod = false;
		int pow = pow_mod(p, k-a[i]);
		if(have_mod) return 0;
		cnt = calcmod(cnt * pow);
		if(have_mod) return 0;
		if(cnt >= i) return 0; // not: cnt >= n-1
		cnt--;
		i--;
		k = a[i]; // add
	}
	return i;
}
signed main()
{
	ios::sync_with_stdio(false);
	int t; cin >> t; while(t--) {
		cin >> n >> p;
		for(int i = 1; i <= n; i++) cin >> a[i];
		if(p == 1) {
			cout << n % 2 << endl;
			continue;
		}
		if(n == 1) {
			cout << pow_mod(p, a[1]) << endl;
			continue;
		}

		sort(a + 1, a + n + 1);
		for(;;) {
			int newn = calc(n);
			if(!newn) break;
			n = newn;
		}
		int ans1 = 0, ans2 = 0;
		ans1 = pow_mod(p, a[n]);
		for(int i = 1; i < n; i++) {
			ans2 += pow_mod(p, a[i]);
			ans2 %= mod;
		}
		cout << (ans1 + mod - ans2) % mod << endl;
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
#define int long long
#define maxn (1000010)
#define mod (1000000007)
int k[maxn];
int pow(int a, int n) {
	int ret = 1;
	while(n) {
		if(n & 1) ret = (ret * a) % mod;
		a = (a * a) % mod;
		n >>= 1;
	}
	return ret;
}
signed main()
{
	int t; cin >> t; while(t--) {
		int n, p; cin >> n >> p;
		for(int i = 0; i < n; i++) {
			cin >> k[i];
		}
		if(p == 1) {
			cout << n % 2 << endl;
			continue;
		}
		int cur_k = -1, cnt = 0, over = 0, last = -1;
		for(int i = n-1; i >= 0; i--) {
			if(cnt <= 0) {
				cur_k = k[i];
				cnt = 1;
				last = i;
			} else {
				int ret = 1;
				int x = cur_k - k[i];
				for(int t = 0; t < min(x, 20ll); t++) {
					ret *= p;
					if(ret > i) {over = 1; break;}
				}
				cnt = ret - 1;
				cur_k = k[i];
			}
			if(over) break;
		}
		if(over) {
			int a = pow(p, k[last]);
			int b = 0;
			for(int i = 0; i < last; i++) {
				b = (b + pow(p, k[i])) % mod;
			}
			cout << abs(a - b) << endl;
		} else {
			cout << (cnt * pow(p, cur_k)) % mod << endl;
		}
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
#define int long long
#define maxn (1000010)
#define mod (1000000007)
int a[maxn], n, p;
bool have_mod;
int calcmod(int a) {
	have_mod = bool(a > a % mod);
	return a % mod;
}
int pow_mod(int a, int n) {
	have_mod = false;
	int ret = 1;
	while(n) {
		if(n & 1) ret = calcmod(ret * a);
		a = (a * a) % mod;
		n >>= 1;
	}
	return ret;
}
int calc(int n) {
	int k = a[n], cnt = 1, i = n-1;
	while(i > 0 && cnt > 0) {
		have_mod = false;
		int pow = pow_mod(p, k-a[i]);
		if(have_mod) return 0;
		cnt = calcmod(cnt * pow);
		if(have_mod) return 0;
		if(cnt >= i) return 0; // not: cnt >= n-1
		cnt--;
		i--;
		k = a[i]; // add
	}
	return i;
}
signed main()
{
	ios::sync_with_stdio(false);
	int t; cin >> t; while(t--) {
		cin >> n >> p;
		for(int i = 1; i <= n; i++) cin >> a[i];
		if(p == 1) {
			cout << n % 2 << endl;
			continue;
		}
		if(n == 1) {
			cout << pow_mod(p, a[1]) << endl;
			continue;
		}

		sort(a + 1, a + n + 1);
		for(;;) {
			int newn = calc(n);
			if(!newn) break;
			n = newn;
		}
		int ans1 = 0, ans2 = 0;
		ans1 = pow_mod(p, a[n]);
		for(int i = 1; i < n; i++) {
			ans2 += pow_mod(p, a[i]);
			ans2 %= mod;
		}
		cout << abs(ans1 - ans2) % mod << endl;
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
#define int long long
#define maxn (1000010)
#define mod (1000000007)
int a[maxn], n, p;
bool have_mod;
int calcmod(int a) {
	if(!have_mod && a > a % mod) have_mod = true; // wrong answer[4]: have_mod = bool(a > a % mod);
	return a % mod;
}
int pow_mod(int a, int n) {
	int ret = 1;
	while(n) {
		if(n & 1) ret = calcmod(ret * a); // may call more once time function calcmod()
		n >>= 1;
		if(n) a = calcmod(a * a);   // wrong answer[4]: a = (a * a) % mod;
	}
	return ret;
}
int calc(int n) {
	// cout << "calc begin\n";
	int k = a[n], cnt = 1, i = n-1;
	while(i > 0 && cnt > 0) {
		have_mod = false;
		cnt = calcmod(cnt * pow_mod(p, k-a[i]));
		if(have_mod || cnt >= i) return 0;
		if(cnt >= i) return 0; // not: cnt >= n-1
		k = a[i];
		cnt--;
		i--;
	}
	return i;
}
signed main()
{
	ios::sync_with_stdio(false);
	int t; cin >> t; while(t--) {
		cin >> n >> p;
		for(int i = 1; i <= n; i++) cin >> a[i];
		if(p == 1) {
			cout << n % 2 << endl;
			continue;
		}
		if(n == 1) {
			cout << pow_mod(p, a[1]) << endl;
			continue;
		}

		sort(a + 1, a + n + 1);
		for(;;) {
			int newn = calc(n);
			// cout << "newn = " << newn << endl;
			if(!newn) break;
			n = newn;
		}
		int ans1 = 0, ans2 = 0;
		ans1 = pow_mod(p, a[n]);
		for(int i = 1; i < n; i++) {
			ans2 += pow_mod(p, a[i]);
			ans2 %= mod;
		}
		cout << (ans1 + mod - ans2) % mod << endl;
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
#define int long long
#define maxn (1000010)
#define mod (1000000007)
int a[maxn], n, p;
bool have_mod;
int calcmod(int a) {
	if(!have_mod && a > a % mod) have_mod = true; // wrong answer[4]: have_mod = bool(a > a % mod);
	return a % mod;
}
int pow_mod(int a, int n) {
	int ret = 1;
	while(n) {
		if(n & 1) ret = calcmod(ret * a); // may call more once time function calcmod()
		a = (a * a) % mod;
		n >>= 1;
	}
	return ret;
}
int calc(int n) {
	// cout << "calc begin\n";
	int k = a[n], cnt = 1, i = n-1;
	while(i > 0 && cnt > 0) {
		have_mod = false;
		int pow = pow_mod(p, k-a[i]);
		cnt = calcmod(cnt * pow);
		// printf("a[%lld] = %lld, cnt = %lld\n", i, a[i], cnt);
		if(have_mod) return 0;
		if(cnt >= i) return 0; // not: cnt >= n-1
		k = a[i];
		cnt--;
		i--;
	}
	return i;
}
signed main()
{
	ios::sync_with_stdio(false);
	int t; cin >> t; while(t--) {
		cin >> n >> p;
		for(int i = 1; i <= n; i++) cin >> a[i];
		if(p == 1) {
			cout << n % 2 << endl;
			continue;
		}
		if(n == 1) {
			cout << pow_mod(p, a[1]) << endl;
			continue;
		}

		sort(a + 1, a + n + 1);
		for(;;) {
			int newn = calc(n);
			// cout << "newn = " << newn << endl;
			if(!newn) break;
			n = newn;
		}
		int ans1 = 0, ans2 = 0;
		ans1 = pow_mod(p, a[n]);
		for(int i = 1; i < n; i++) {
			ans2 += pow_mod(p, a[i]);
			ans2 %= mod;
		}
		cout << abs(ans1 - ans2) << endl;
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
#define int long long
#define maxn (1000010)
#define mod (1000000007)
int a[maxn], n, p;
bool have_mod;
int calcmod(int a) {
	if(!have_mod && a > a % mod) have_mod = true; // wrong answer[4]: have_mod = bool(a > a % mod);
	return a % mod;
}
int pow_mod(int a, int n) {
	int ret = 1;
	while(n) {
		a = calcmod(a);   // wrong answer[4]: no this code
		if(n & 1) ret = calcmod(ret * a); // may call more once time function calcmod()
		a = a * a;   // wrong answer[4]: a = (a * a) % mod;
		n >>= 1;
	}
	return ret;
}
int calc(int n) {
	// cout << "calc begin\n";
	int k = a[n], cnt = 1, i = n-1;
	while(i > 0 && cnt > 0) {
		have_mod = false;
		cnt = calcmod(cnt * pow_mod(p, k-a[i]));
		if(have_mod || cnt >= i) return 0;
		if(cnt >= i) return 0; // not: cnt >= n-1
		k = a[i];
		cnt--;
		i--;
	}
	return i;
}
signed main()
{
	ios::sync_with_stdio(false);
	int t; cin >> t; while(t--) {
		cin >> n >> p;
		for(int i = 1; i <= n; i++) cin >> a[i];
		if(p == 1) {
			cout << n % 2 << endl;
			continue;
		}
		if(n == 1) {
			cout << pow_mod(p, a[1]) << endl;
			continue;
		}

		sort(a + 1, a + n + 1);
		for(;;) {
			int newn = calc(n);
			// cout << "newn = " << newn << endl;
			if(!newn) break;
			n = newn;
		}
		int ans1 = 0, ans2 = 0;
		ans1 = pow_mod(p, a[n]);
		for(int i = 1; i < n; i++) {
			ans2 += pow_mod(p, a[i]);
			ans2 %= mod;
		}
		cout << abs(ans1 - ans2) << endl;
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
#define int long long
#define maxn (1000010)
#define mod (1000000007)
int k[maxn];
int pow(int a, int n) {
	int ret = 1;
	while(n) {
		if(n & 1) ret = (ret * a) % mod;
		a = (a * a) % mod;
		n >>= 1;
	}
	return ret;
}
signed main()
{
	int t; cin >> t; while(t--) {
		int n, p; cin >> n >> p;
		for(int i = 0; i < n; i++) {
			cin >> k[i];
		}
		if(p == 1) {
			cout << n % 2 << endl;
			continue;
		}
		int cur_k = -1, cnt = 0, over = 0, last = -1;
		for(int i = n-1; i >= 0; i--) {
			if(cnt <= 0) {
				cur_k = k[i];
				cnt = 1;
				last = i;
			} else {
				int ret = 1;
				int x = cur_k - k[i];
				for(int t = 0; t < min(x, 20ll); t++) {
					ret *= p;
					if(ret > i) {over = 1; break;}
				}
				cnt = cnt * ret;
				if(cnt > i) {over = 1;}
				cnt--;
				cur_k = k[i];
			}
			if(over) break;
		}
		if(over) {
			int a = pow(p, k[last]);
			int b = 0;
			for(int i = 0; i < last; i++) {
				b = (b + pow(p, k[i])) % mod;
			}
			cout << abs(a - b) << endl;
		} else {
			cout << (cnt * pow(p, cur_k)) % mod << endl;
		}
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
#define int long long
#define maxn (1000010)
#define mod (1000000007)
int a[maxn], n, p;
bool have_mod;
int calcmod(int a) {
	have_mod = bool(a > a % mod);
	return a % mod;
}
int pow_mod(int a, int n) {
	have_mod = false;
	int ret = 1;
	while(n) {
		if(n & 1) ret = calcmod(ret * a);
		a = (a * a) % mod;
		n >>= 1;
	}
	return ret;
}
int calc(int n) {
	int k = a[n], cnt = 1, i = n-1;
	while(i > 0 && cnt > 0) {
		have_mod = false;
		int pow = pow_mod(p, k-a[i]);
		if(have_mod) return 0;
		cnt = calcmod(cnt * pow);
		if(have_mod) return 0;
		if(cnt >= n-1) return 0;
		cnt--;
		i--;
	}
	return i;
}
signed main()
{
	int t; cin >> t; while(t--) {
		cin >> n >> p;
		for(int i = 1; i <= n; i++) cin >> a[i];
		if(p == 1) {
			cout << n % 2 << endl;
			continue;
		}
		if(n == 1) {
			cout << pow_mod(p, a[1]) << endl;
			continue;
		}

		sort(a + 1, a + n + 1);
		for(;;) {
			int newn = calc(n);
			if(!newn) break;
			n = newn;
		}
		int ans1 = 0, ans2 = 0;
		ans1 = pow_mod(p, a[n]);
		for(int i = 1; i < n; i++) {
			ans2 += pow_mod(p, a[i]);
			ans2 %= mod;
		}
		cout << (ans1 + mod - ans2) % mod << endl;
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
#define int long long
#define maxn (1000010)
#define mod (1000000007)
int a[maxn], n, p;
bool have_mod;
int calcmod(int a) {
	if(!have_mod && a > a % mod) have_mod = true; // wrong answer[4]: have_mod = bool(a > a % mod);
	return a % mod;
}
int pow_mod(int a, int n) {
	int ret = 1;
	while(n) {
		if(n & 1) ret = calcmod(ret * a); // may call more once time function calcmod()
		n >>= 1;
		if(n) a = calcmod(a * a);   // wrong answer[4]: a = (a * a) % mod;
	}
	return ret;
}
int calc(int n) {
	// cout << "calc begin\n";
	int k = a[n], cnt = 1, i = n-1;
	while(i > 0 && cnt > 0) {
		have_mod = false;
		cnt = calcmod(cnt * pow_mod(p, k-a[i]));
		if(have_mod || cnt >= i) return 0;
		if(cnt >= i) return 0; // not: cnt >= n-1
		k = a[i];
		cnt--;
		i--;
	}
	return i;
}
signed main()
{
	ios::sync_with_stdio(false);
	int t; cin >> t; while(t--) {
		cin >> n >> p;
		for(int i = 1; i <= n; i++) cin >> a[i];
		if(p == 1) {
			cout << n % 2 << endl;
			continue;
		}
		if(n == 1) {
			cout << pow_mod(p, a[1]) << endl;
			continue;
		}

		sort(a + 1, a + n + 1);
		for(;;) {
			int newn = calc(n);
			// cout << "newn = " << newn << endl;
			if(!newn) break;
			n = newn;
		}
		int ans1 = 0, ans2 = 0;
		ans1 = pow_mod(p, a[n]);
		for(int i = 1; i < n; i++) {
			ans2 += pow_mod(p, a[i]);
			ans2 %= mod;
		}
		cout << abs(ans1 - ans2) << endl;
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
#define int long long
#define maxn (1000010)
#define mod (1000000007)
int a[maxn], n, p;
bool have_mod;
int calcmod(int a) {
	have_mod = bool(a > a % mod);
	return a % mod;
}
int pow_mod(int a, int n) {
	have_mod = false;
	int ret = 1;
	while(n) {
		if(n & 1) ret = calcmod(ret * a);
		a = (a * a) % mod;
		n >>= 1;
	}
	return ret;
}
int calc(int n) {
	// cout << "calc begin\n";
	int k = a[n], cnt = 1, i = n-1;
	while(i > 0 && cnt > 0) {
		have_mod = false;
		int pow = pow_mod(p, k-a[i]);
		if(have_mod) return 0;
		cnt = calcmod(cnt * pow);
		// printf("a[%lld] = %lld, cnt = %lld\n", i, a[i], cnt);
		if(have_mod) return 0;
		if(cnt >= i) return 0; // not: cnt >= n-1
		cnt--;
		k = a[i]; // add
		i--;
	}
	return i;
}
signed main()
{
	ios::sync_with_stdio(false);
	int t; cin >> t; while(t--) {
		cin >> n >> p;
		for(int i = 1; i <= n; i++) cin >> a[i];
		if(p == 1) {
			cout << n % 2 << endl;
			continue;
		}
		if(n == 1) {
			cout << pow_mod(p, a[1]) << endl;
			continue;
		}

		sort(a + 1, a + n + 1);
		for(;;) {
			int newn = calc(n);
			// cout << "newn = " << newn << endl;
			if(!newn) break;
			n = newn;
		}
		int ans1 = 0, ans2 = 0;
		ans1 = pow_mod(p, a[n]);
		for(int i = 1; i < n; i++) {
			ans2 += pow_mod(p, a[i]);
			ans2 %= mod;
		}
		cout << abs(ans1 - ans2) % mod << endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define ts cout<<"ok"<<endl
#define int long long
#define hh puts("")
#define pc putchar
#define mo 1000000007
//#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
//char buf[1<<21],*p1=buf,*p2=buf;
using namespace std;
const int N=1000005;
int n,p,a[N];
inline int read(){
    int ret=0,ff=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-') ff=-1;ch=getchar();}
    while(isdigit(ch)){ret=ret*10+(ch^48);ch=getchar();}
    return ret*ff;
}
void write(int x){if(x<0){x=-x,pc('-');}if(x>9) write(x/10);pc(x%10+48);}
void writeln(int x){write(x),hh;}
void writesp(int x){write(x),pc(' ');}
int ksm(int x,int y){
	int res=1;
	while(y){
		if(y&1) res=res*x%mo;
		y>>=1;
		x=x*x%mo;
	}
	return res;
}
bool cmp(int A,int B){
	return A>B;
}
signed main(){
	int T=read();
	while(T--){
		n=read(),p=read();
		for(int i=1;i<=n;i++) a[i]=read();
		sort(a+1,a+n+1);
		if(p==1){
			if(n&1) writeln(1);
			else writeln(0);
		}
		else{
			int fl=0,j,now=0,fu=1;
			for(int i=n;i>=1;i--){
				for(j=a[i+1];j>a[i];j--){
					if(!fl&&abs(now)<1e8) now*=p;
					else{
						fl=1;
						if(now<0) fu=-1;
						else fu=1;
						break;
					}
				}
				if(fl){
					now=now%mo*ksm(p,j-a[i])%mo;
					if(fu==-1) now++;
					else now--;
				}
				else{
					if(now>0) now--;
					else now++;
				}
			}
			now=now%mo*ksm(p,a[1])%mo;
			writeln((now%mo+mo)%mo);
		}
	}
    return 0;
}


#include<bits/stdc++.h>
#define ts cout<<"ok"<<endl
#define int long long
#define hh puts("")
#define pc putchar
#define mo 1000000007
//#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
//char buf[1<<21],*p1=buf,*p2=buf;
using namespace std;
const int N=1000005;
int n,p,a[N];
inline int read(){
    int ret=0,ff=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-') ff=-1;ch=getchar();}
    while(isdigit(ch)){ret=ret*10+(ch^48);ch=getchar();}
    return ret*ff;
}
void write(int x){if(x<0){x=-x,pc('-');}if(x>9) write(x/10);pc(x%10+48);}
void writeln(int x){write(x),hh;}
void writesp(int x){write(x),pc(' ');}
int ksm(int x,int y){
	int res=1;
	while(y){
		if(y&1) res=res*x%mo;
		y>>=1;
		x=x*x%mo;
	}
	return res;
}
signed main(){
	int T=read();
	while(T--){
		n=read(),p=read();
		for(int i=1;i<=n;i++) a[i]=read();
		sort(a+1,a+n+1);
		if(p==1){
			if(n&1) writeln(1);
			else writeln(0);
		}
		else{
			int now=1,fl=0;//p^(1*a[n])
			for(int i=n-1;i>=1;i--){
				if(a[i+1]==a[i]){
					if(now>0) now--;
					else if(!now) now++;
				}
				else{
					if(!now) now++;
					else{
						for(int j=a[i+1]-1;j>=a[i];j--){
							now=now*p;
							if(now>1e10){
								int ans=now%mo*ksm(p,j)%mo;
								for(int k=1;k<=i;k++)
									ans=(ans-ksm(p,a[k])%mo+mo)%mo;
								writeln(ans);
								fl=1;
								break;
							}
						}
						now--;
					}
					if(fl) break;
				}
				writeln(now);
			}
			if(!fl) writeln(now%mo*ksm(p,a[1])%mo);
		}
	}
    return 0;
}


#include<bits/stdc++.h>
#define ts cout<<"ok"<<endl
#define int long long
#define hh puts("")
#define pc putchar
#define mo 1000000007
//#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
//char buf[1<<21],*p1=buf,*p2=buf;
using namespace std;
const int N=1000005;
int n,p,a[N];
inline int read(){
    int ret=0,ff=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-') ff=-1;ch=getchar();}
    while(isdigit(ch)){ret=ret*10+(ch^48);ch=getchar();}
    return ret*ff;
}
void write(int x){if(x<0){x=-x,pc('-');}if(x>9) write(x/10);pc(x%10+48);}
void writeln(int x){write(x),hh;}
void writesp(int x){write(x),pc(' ');}
int ksm(int x,int y){
	int res=1;
	while(y){
		if(y&1) res=res*x%mo;
		y>>=1;
		x=x*x%mo;
	}
	return res;
}
signed main(){
	int T=read();
	while(T--){
		n=read(),p=read();
		for(int i=1;i<=n;i++) a[i]=read();
		sort(a+1,a+n+1);
		if(p==1){
			if(n&1) writeln(1);
			else writeln(0);
		}
		else{
			int now=1,fl=0;//p^(1*a[n])
			for(int i=n-1;i>=1;i--){
				if(a[i+1]==a[i]){
					if(now>0) now--;
					else if(!now) now++;
				}
				else{
					if(!now) now++;
					else{
						for(int j=a[i+1]-1;j>=a[i];j--){
							now=now*p;
							if(now>1e10){
								int ans=now%mo*ksm(p,j)%mo;
								for(int k=1;k<=i;k++)
									ans=(ans-ksm(p,a[k])%mo+mo)%mo;
								writeln(ans);
								fl=1;
								break;
							}
						}
					}
					if(fl) break;
				}
			}
			if(!fl) writeln(now%mo*ksm(p,a[1])%mo);
		}
	}
    return 0;
}


#include<bits/stdc++.h>
#define ts cout<<"ok"<<endl
#define int long long
#define hh puts("")
#define pc putchar
#define mo 1000000007
//#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
//char buf[1<<21],*p1=buf,*p2=buf;
using namespace std;
const int N=1000005;
int n,p,a[N];
inline int read(){
    int ret=0,ff=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-') ff=-1;ch=getchar();}
    while(isdigit(ch)){ret=ret*10+(ch^48);ch=getchar();}
    return ret*ff;
}
void write(int x){if(x<0){x=-x,pc('-');}if(x>9) write(x/10);pc(x%10+48);}
void writeln(int x){write(x),hh;}
void writesp(int x){write(x),pc(' ');}
int ksm(int x,int y){
	int res=1;
	while(y){
		if(y&1) res=res*x%mo;
		y>>=1;
		x=x*x%mo;
	}
	return res;
}
signed main(){
	int T=read();
	while(T--){
		n=read(),p=read();
		for(int i=1;i<=n;i++) a[i]=read();
		sort(a+1,a+n+1);
		if(p==1){
			if(n&1) writeln(1);
			else writeln(0);
		}
		else{
			int now=1,fl=0;//p^(1*a[n])
			for(int i=n-1;i>=1;i--){
				if(a[i+1]==a[i]){
					if(now>0) now--;
					else if(!now) now++;
				}
				else{
					if(!now) now++;
					else{
						for(int j=a[i+1]-1;j>=a[i];j--){
							now=now*p;
							if(now>1e10){
								int ans=now%mo*ksm(p,j)%mo;
								for(int k=1;k<=i;k++)
									ans=(ans-ksm(p,a[k])%mo+mo)%mo;
								writeln(ans);
								fl=1;
								break;
							}
						}
						now--;
					}
					if(fl) break;
				}
			}
			if(!fl) writeln(now%mo*ksm(p,a[1])%mo);
		}
	}
    return 0;
}


#include<cstdio>
#include<algorithm>
using namespace std;
const long long M=1000000007;
int k[1000010],l[1000010],o[1000010],m[1000010],pan[1000010];
long long powermod(long long p,int x)
{
	long long ans=1;
	while(x)
	{
		if(x&1)
		 ans=ans*p%M;
		p=p*p%M;
		x/=2;
	}
	return ans%M;
}
int main()
{
	int t;
	k[0]=-1;
	scanf("%d",&t);
	while(t--)
	{
		int n,i,j,cnt=0;
		long long ans=0,p,e;
		scanf("%d%lld",&n,&p);
		for(i=1;i<=n;i++)
		 scanf("%d",&k[i]);
		sort(k+1,k+n+1);
		for(i=1;i<=n;i++)
		 if(k[i]!=k[i-1])
		  {
		  	l[++cnt]=1;
		  	o[cnt]=k[i];
		  }
		 else
		  l[cnt]++;
		//printf("%d\n",l[1]);
		m[0]=0;
		for(i=0;i<cnt;i++)
		 {
		 	e=1;
		 	for(j=1;j<=o[i+1]-o[i];j++)
		 	 {
		 	 	e*=p;
		 	 	if(e>n)
		 	 	 break;
		 	 }
		 	if(m[i]>=e)
		 	 {
		 	 	m[i+1]=l[i+1]+m[i]/e;
		 	 	m[i]%=e;
		 	 	pan[i+1]=1;
		 	 }
		 	 else
		 	  {
		 	  	m[i+1]=l[i+1];
		 	  	pan[i+1]=0;
		 	  }
		 }
		 for(i=cnt;i>=1;i--)
		 {

		 	if(pan[i])
		 	 {
		 	 	if(m[i]%2)
		 	  	 m[i-1]+=powermod(p,o[i]-o[i-1]);
		 	 }
		 	else
		 	 if(!ans)
		 	  {
		 	  	if(m[i]%2)
		 	   	 ans=powermod(p,o[i]);
		 	  }
		 	else
		 	 ans=(((ans-m[i]*powermod(p,o[i]))%M)+M)%M;
		 	//printf("%d %lld\n",pan[i],ans);
		 }
		 printf("%lld\n",ans);
	}
	return 0;
}


#include<cstdio>
#include<algorithm>
using namespace std;
const long long M=1000000007;
int k[1000010],l[1000010],o[1000010],m[1000010],pan[1000010];
long long powermod(long long p,int x)
{
	long long ans=1;
	while(x)
	{
		if(x&1)
		 ans=ans*p%M;
		p=p*p%M;
		x/=2;
	}
	return ans%M;
}
int main()
{
	int t;
	k[0]=-1;
	scanf("%d",&t);
	while(t--)
	{
		int n,i,j,cnt=0;
		long long ans=0,p,e;
		scanf("%d%lld",&n,&p);
		for(i=1;i<=n;i++)
		 scanf("%d",&k[i]);
		sort(k+1,k+n+1);
		for(i=1;i<=n;i++)
		 if(k[i]!=k[i-1])
		  {
		  	l[++cnt]=1;
		  	o[cnt]=k[i];
		  }
		 else
		  l[cnt]++;
		//printf("%d\n",l[1]);
		m[0]=0;
		for(i=0;i<cnt;i++)
		 {
		 	e=1;
		 	for(j=1;j<=o[i+1]-o[i];j++)
		 	 {
		 	 	e*=p;
		 	 	if(e>n)
		 	 	 break;
		 	 }
		 	if(m[i]>=e)
		 	 {
		 	 	m[i+1]=l[i+1]+m[i]/e;
		 	 	m[i]%=e;
		 	 	pan[i+1]=1;
		 	 }
		 	 else
		 	  {
		 	  	m[i+1]=l[i+1];
		 	  	pan[i+1]=0;
		 	  }
		 }
		 for(i=cnt;i>=1;i--)
		 {

		 	if(pan[i])
		 	 {
		 	 	if(m[i]%2)
		 	  	 m[i-1]+=powermod(p,o[i]-o[i-1]);
		 	 }
		 	else
			  if(m[i]%2)
		 	   if(!ans)
		 	    ans=powermod(p,o[i]);
		 	   else
		 	    ans=(((ans-m[i]*powermod(p,o[i]))%M)+M)%M;
		 	//printf("%d %lld\n",pan[i],ans);
		 }
		 printf("%lld\n",ans);
	}
	return 0;
}


#include<cstdio>
#include<algorithm>
using namespace std;
const long long M=1000000007;
int k[1000010],l[1000010],o[1000010],m[1000010],pan[1000010];
long long powermod(long long p,int x)
{
	long long ans=1;
	while(x)
	{
		if(x&1)
		 ans=ans*p%M;
		p=p*p%M;
		x/=2;
	}
	return ans%M;
}
int main()
{
	int t;
	k[0]=-1;
	scanf("%d",&t);
	while(t--)
	{
		int n,i,j,cnt=0;
		long long ans=0,p,e;
		scanf("%d%lld",&n,&p);
		for(i=1;i<=n;i++)
		 scanf("%d",&k[i]);
		sort(k+1,k+n+1);
		for(i=1;i<=n;i++)
		 if(k[i]!=k[i-1])
		  {
		  	l[++cnt]=1;
		  	o[cnt]=k[i];
		  }
		 else
		  l[cnt]++;
		//printf("%d\n",l[1]);
		m[0]=0;
		for(i=0;i<cnt;i++)
		 {
		 	e=1;
		 	if(p!=1)
		 	 for(j=1;j<=o[i+1]-o[i];j++)
		 	 {
		 	 	e*=p;
		 	 	if(e>n)
		 	 	 break;
		 	 }
		 	if(m[i]>=e)
		 	 {
		 	 	m[i+1]=l[i+1]+m[i]/e;
		 	 	m[i]%=e;
		 	 	pan[i+1]=1;
		 	 }
		 	 else
		 	  {
		 	  	m[i+1]=l[i+1];
		 	  	pan[i+1]=0;
		 	  }
		 }
		 for(i=cnt;i>=1;i--)
		 {

		 	if(pan[i])
		 	 {
		 	 	if(ans)
		 	 	 ans=(((ans-m[i]*powermod(p,o[i]))%M)+M)%M;
		 	 	else
		 	 	 if(m[i]%2)
		 	  	  m[i-1]+=powermod(p,o[i]-o[i-1]);
		 	 }
		 	else
		 	 if(!ans)
		 	  {
		 	  	if(m[i]%2)
		 	   	 ans=powermod(p,o[i]);
		 	  }
		 	else
		 	 ans=(((ans-m[i]*powermod(p,o[i]))%M)+M)%M;
		 //	printf("%d %lld\n",pan[i],ans);
		 }
		 printf("%lld\n",ans);
		/* if(ans==98)
		  {
		  	printf("%d %lld\n",n,p);
		  	for(i=1;i<=n;i++)
		  	 printf("%d ",k[i]);
		  	printf("\n");
		  }*/
	}
	return 0;
}


#include<cstdio>
#include<algorithm>
using namespace std;
const long long M=1000000007;
int k[1000010],l[1000010],o[1000010],m[1000010],pan[1000010];
long long powermod(long long p,int x)
{
	long long ans=1;
	while(x)
	{
		if(x&1)
		 ans=ans*p%M;
		p=p*p%M;
		x/=2;
	}
	return ans%M;
}
int main()
{
	int t;
	k[0]=-1;
	scanf("%d",&t);
	while(t--)
	{
		int n,i,j,cnt=0;
		long long ans=0,p,e;
		scanf("%d%lld",&n,&p);
		for(i=1;i<=n;i++)
		 scanf("%d",&k[i]);
		sort(k+1,k+n+1);
		for(i=1;i<=n;i++)
		 if(k[i]!=k[i-1])
		  {
		  	l[++cnt]=1;
		  	o[cnt]=k[i];
		  }
		 else
		  l[cnt]++;
		//printf("%d\n",l[1]);
		m[0]=0;
		for(i=0;i<cnt;i++)
		 {
		 	e=1;
		 	for(j=1;j<=o[i+1]-o[i];j++)
		 	 {
		 	 	e*=p;
		 	 	if(e>n)
		 	 	 break;
		 	 }
		 	if(m[i]>=e)
		 	 {
		 	 	m[i+1]=l[i+1]+m[i]/e;
		 	 	m[i]%=e;
		 	 	pan[i+1]=1;
		 	 }
		 	 else
		 	  {
		 	  	m[i+1]=l[i+1];
		 	  	pan[i+1]=0;
		 	  }
		 }
		 for(i=cnt;i>=1;i--)
		 {

		 	if(pan[i])
		 	 {
		 	 	if(m[i]%2)
		 	  	 m[i-1]+=powermod(p,o[i]-o[i-1]);
		 	 }
		 	else
			 if(m[i]%2)
		 	 {
		 	 	if(!ans)
		 	  	 ans=powermod(p,o[i]);
		 	 	else
		 	  	 ans=(((ans-powermod(p,o[i]))%M)+M)%M;
		 	 }
		 }
		 printf("%lld\n",ans);
	}
	return 0;
}


#include<cstdio>
#include<algorithm>
using namespace std;
const long long M=1000000007;
int k[1000010],l[1000010],o[1000010],m[1000010],pan[1000010];
long long powermod(long long p,int x)
{
	long long ans=1;
	while(x)
	{
		if(x&1)
		 ans=ans*p%M;
		p=p*p%M;
		x/=2;
	}
	return ans%M;
}
int main()
{
	int t;
	k[0]=-1;
	scanf("%d",&t);
	while(t--)
	{
		int n,i,j,cnt=0;
		long long ans=0,p,e;
		scanf("%d%lld",&n,&p);
		for(i=1;i<=n;i++)
		 scanf("%d",&k[i]);
		sort(k+1,k+n+1);
		for(i=1;i<=n;i++)
		 if(k[i]!=k[i-1])
		  {
		  	l[++cnt]=1;
		  	o[cnt]=k[i];
		  }
		 else
		  l[cnt]++;
		//printf("%d\n",l[1]);
		m[0]=0;
		for(i=0;i<cnt;i++)
		 {
		 	e=1;
		 	for(j=1;j<=o[i+1]-o[i];j++)
		 	 {
		 	 	e*=p;
		 	 	if(e>n)
		 	 	 break;
		 	 }
		 	if(m[i]>=e)
		 	 {
		 	 	m[i+1]=l[i+1]+m[i]/e;
		 	 	m[i]%=e;
		 	 	pan[i+1]=1;
		 	 }
		 	 else
		 	  {
		 	  	m[i+1]=l[i+1];
		 	  	pan[i+1]=0;
		 	  }
		 }
		 for(i=cnt;i>=1;i--)
		 {

		 	if(pan[i])
		 	 {
		 	 	if(m[i]%2)
		 	  	 m[i-1]+=powermod(p,o[i]-o[i-1]);
		 	 }
		 	else
		 	 if(!ans)
		 	  {
		 	  	if(m[i]%2)
		 	   	 ans=powermod(p,o[i]);
		 	  }
		 	else
		 	 ans=(((ans-m[i]*powermod(p,o[i]))%M)+M)%M;
		 	//printf("%d %lld\n",pan[i],ans);
		 }
		 printf("%lld\n",ans);
		 if(ans==98)
		  {
		  	printf("%d %lld\n",n,p);
		  	for(i=1;i<=n;i++)
		  	 printf("%d ",k[i]);
		  	printf("\n");
		  }
	}
	return 0;
}


#include<cstdio>
#include<algorithm>
using namespace std;
const long long M=1000000007;
int k[1000010],l[1000010],o[1000010],m[1000010],pan[1000010];
long long powermod(long long p,int x)
{
	long long ans=1;
	while(x)
	{
		if(x&1)
		 ans=ans*p%M;
		p=p*p%M;
		x/=2;
	}
	return ans%M;
}
int main()
{
	int t;
	k[0]=-1;
	scanf("%d",&t);
	while(t--)
	{
		int n,i,j,cnt=0;
		long long ans=0,p,e;
		scanf("%d%lld",&n,&p);
		for(i=1;i<=n;i++)
		 scanf("%d",&k[i]);
		sort(k+1,k+n+1);
		for(i=1;i<=n;i++)
		 if(k[i]!=k[i-1])
		  {
		  	l[++cnt]=1;
		  	o[cnt]=k[i];
		  }
		 else
		  l[cnt]++;
		//printf("%d\n",l[1]);
		m[0]=0;
		for(i=0;i<cnt;i++)
		 {
		 	e=1;
		 	for(j=1;j<=o[i+1]-o[i];j++)
		 	 {
		 	 	e*=p;
		 	 	if(e>n)
		 	 	 break;
		 	 }
		 	if(m[i]>=e)
		 	 {
		 	 	m[i+1]=l[i+1]+m[i]/e;
		 	 	m[i]%=e;
		 	 	pan[i+1]=1;
		 	 }
		 	 else
		 	  {
		 	  	m[i+1]=l[i+1];
		 	  	pan[i+1]=0;
		 	  }
		 }
		 for(i=cnt;i>=1;i--)
		 {

		 	if(pan[i])
		 	 {
		 	 	if(m[i]%2)
		 	  	 m[i-1]+=powermod(p,o[i]-o[i-1]);
		 	 }
		 	else
		 	 if(!ans)
		 	  {
		 	  	if(m[i]%2)
		 	   	 ans=powermod(p,o[i]);
		 	  }
		 	else
		 	 ans=(((ans-m[i]*powermod(p,o[i]))%M)+M)%M;
		 	//printf("%d %lld\n",pan[i],ans);
		 }
		 printf("%lld\n",ans);
		 if(ans==98)
		  {
		  	printf("%d %d\n",n,p);
		  	for(i=1;i<=n;i++)
		  	 printf("%d ",k[i]);
		  	printf("%d\n");
		  }
	}
	return 0;
}


#include<cstdio>
#include<algorithm>
using namespace std;
const long long M=1000000007;
int k[1000010],l[1000010],o[1000010],m[1000010],pan[1000010];
long long powermod(long long p,int x)
{
	long long ans=1;
	while(x)
	{
		if(x&1)
		 ans=ans*p%M;
		p=p*p%M;
		x/=2;
	}
	return ans%M;
}
int main()
{
	int t;
	k[0]=-1;
	scanf("%d",&t);
	while(t--)
	{
		int n,i,j,cnt=0;
		long long ans=0,p,e;
		scanf("%d%lld",&n,&p);
		for(i=1;i<=n;i++)
		 scanf("%d",&k[i]);
		sort(k+1,k+n+1);
		for(i=1;i<=n;i++)
		 if(k[i]!=k[i-1])
		  {
		  	l[++cnt]=1;
		  	o[cnt]=k[i];
		  }
		 else
		  l[cnt]++;
		//printf("%d\n",l[1]);
		m[0]=0;
		for(i=0;i<cnt;i++)
		 {
		 	e=1;
		 	for(j=1;j<=o[i+1]-o[i];j++)
		 	 {
		 	 	e*=p;
		 	 	if(e>n)
		 	 	 break;
		 	 }
		 	if(m[i]>=e)
		 	 {
		 	 	m[i+1]=l[i+1]+m[i]/e;
		 	 	m[i]%=e;
		 	 	pan[i+1]=1;
		 	 }
		 	 else
		 	  {
		 	  	m[i+1]=l[i+1];
		 	  	pan[i+1]=0;
		 	  }
		 }
		 for(i=cnt;i>=1;i--)
		 {

		 	if(pan[i])
		 	 {
		 	 	if(m[i]%2)
		 	  	 m[i-1]+=powermod(p,o[i]-o[i-1]);
		 	 }
		 	else
		 	 if(!ans)
		 	  ans=powermod(p,o[i]);
		 	 else
		 	  ans=(((ans-m[i]*powermod(p,o[i]))%M)+M)%M;
		 	//printf("%d %lld\n",pan[i],ans);
		 }
		 printf("%lld\n",ans);
	}
	return 0;
}


#include<cstdio>
#include<algorithm>
using namespace std;
const long long M=1000000007;
int k[1000010],l[1000010],o[1000010],m[1000010],pan[1000010];
long long powermod(long long p,int x)
{
	long long ans=1;
	while(x)
	{
		if(x&1)
		 ans=ans*p%M;
		p=p*p%M;
		x/=2;
	}
	return ans%M;
}
int main()
{
	int t;
	k[0]=-1;
	scanf("%d",&t);
	while(t--)
	{
		int n,i,j,cnt=0;
		long long ans=0,p,e;
		scanf("%d%lld",&n,&p);
		for(i=1;i<=n;i++)
		 scanf("%d",&k[i]);
		sort(k+1,k+n+1);
		for(i=1;i<=n;i++)
		 if(k[i]!=k[i-1])
		  {
		  	l[++cnt]=1;
		  	o[cnt]=k[i];
		  }
		 else
		  l[cnt]++;
		//printf("%d\n",l[1]);
		m[0]=0;
		for(i=0;i<cnt;i++)
		 {
		 	e=1;
		 	for(j=1;j<=o[i+1]-o[i];j++)
		 	 {
		 	 	e*=p;
		 	 	if(e>n)
		 	 	 break;
		 	 }
		 	if(m[i]>=e)
		 	 {
		 	 	m[i+1]=l[i+1]+m[i]/e;
		 	 	m[i]%=e;
		 	 	pan[i+1]=1;
		 	 }
		 	 else
		 	  {
		 	  	m[i+1]=l[i+1];
		 	  	pan[i+1]=0;
		 	  }
		 }
		 for(i=cnt;i>=1;i--)
		 {

		 	if(pan[i])
		 	 {
		 	 	if(ans)
		 	 	 ans=(((ans-m[i]*powermod(p,o[i]))%M)+M)%M;
		 	 	else
		 	 	 if(m[i]%2)
		 	  	  m[i-1]+=powermod(p,o[i]-o[i-1]);
		 	 }
		 	else
		 	 if(!ans)
		 	  {
		 	  	if(m[i]%2)
		 	   	 ans=powermod(p,o[i]);
		 	  }
		 	else
		 	 ans=(((ans-m[i]*powermod(p,o[i]))%M)+M)%M;
		 //	printf("%d %lld\n",pan[i],ans);
		 }
		 printf("%lld\n",ans);
		/* if(ans==98)
		  {
		  	printf("%d %lld\n",n,p);
		  	for(i=1;i<=n;i++)
		  	 printf("%d ",k[i]);
		  	printf("\n");
		  }*/
	}
	return 0;
}


#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
#define inf 0x7fffffff
#define ll long long
#define maxn 1000007
#define mod (1000000007)
#define mod2 (1000000006)
ll k[maxn];

bool cmp(ll a,ll b){return a>b;}
ll qpow(ll a,ll b){
    ll ans=1;
    while(b){
        if(b&1){
            ans=(ans*a)%mod;
        }
        a=(a*a)%mod;b>>=1;
    }
    return ans;
}
ll qpow2(ll a,ll b){
    ll ans=1;
    while(b){
        if(b&1){
            ans=(ans*a)%mod2;
        }
        a=(a*a)%mod2;b>>=1;
    }
    return ans;
}
int main(){
    ll i, n, j, t,u,v,p;
    scanf("%lld",&t);
    while(t--){
        ll ans=0,ansn=0;
        scanf("%lld%lld",&n,&p);
        for(i=1;i<=n;i++) scanf("%lld",&k[i]);
        sort(k+1,k+1+n,cmp);
        for(i=1;i<=n;i++){
            if(!ans&&!ansn) ans=(ans+qpow(p,k[i]))%mod,ansn=(ansn+qpow(p,k[i]))%mod;
            else {
                ans=(ans-qpow(p,k[i])+mod)%mod;
                ansn=(ansn-qpow(p,k[i])+mod2)%mod2;
            }
        }
        printf("%lld\n",ans);
    }
}


#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
#define inf 0x7fffffff
#define ll long long
#define maxn 1000007
#define mod (1000000007)
#define mod2 (100000007)
ll k[maxn];

bool cmp(ll a,ll b){return a>b;}
ll qpow(ll a,ll b){
    ll ans=1;
    while(b){
        if(b&1){
            ans=(ans*a)%mod;
        }
        a=(a*a)%mod;b>>=1;
    }
    return ans;
}
ll qpow2(ll a,ll b){
    ll ans=1;
    while(b){
        if(b&1){
            ans=(ans*a)%mod2;
        }
        a=(a*a)%mod2;b>>=1;
    }
    return ans;
}
int main(){
    ll i, n, j, t,u,v,p;
    scanf("%lld",&t);
    while(t--){
        ll ans=0,ansn=0;
        scanf("%lld%lld",&n,&p);
        for(i=1;i<=n;i++) scanf("%lld",&k[i]);
        sort(k+1,k+1+n,cmp);
        for(i=1;i<=n;i++){
            if(!ans&&!ansn) ans=(ans+qpow(p,k[i]))%mod,ansn=(ansn+qpow2(p,k[i]))%mod2;
            else {
                ans=(ans-qpow(p,k[i])+mod)%mod;
                ansn=(ansn-qpow2(p,k[i])+mod2)%mod2;
            }
        }
        printf("%lld\n",ans);
    }
}


#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
#define inf 0x7fffffff
#define ll long long
#define maxn 1000007
#define mod (1000000007)
#define mod2 (100000007)
ll k[maxn];

bool cmp(ll a,ll b){return a>b;}
ll qpow(ll a,ll b){
    ll ans=1;
    while(b){
        if(b&1){
            ans=(ans*a)%mod;
        }
        a=(a*a)%mod;b>>=1;
    }
    return ans;
}

int main(){
    ll i, n, j, t,u,v,p;
    scanf("%lld",&t);
    while(t--){
        ll ans=0,ansn=0;
        scanf("%lld%lld",&n,&p);
        for(i=1;i<=n;i++) scanf("%lld",&k[i]);
        sort(k+1,k+1+n,cmp);
        for(i=1;i<=n;i++){
            if(!ans&&!ansn) ans=(ans+qpow(p,k[i]))%mod,ansn=(ansn+qpow(p,k[i]))%mod;
            else {
                ans=(ans-qpow(p,k[i])+mod)%mod;
                ansn=(ansn-qpow(p,k[i])+mod2)%mod2;
            }
        }
        printf("%lld\n",ans);
    }
}


#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
#define inf 0x7fffffff
#define ll long long
#define maxn 1000007
#define mod (1000000007)
#define mod2 (100000007)
ll k[maxn];

bool cmp(ll a,ll b){return a>b;}
ll qpow(ll a,ll b){
    ll ans=1;
    while(b){
        if(b&1){
            ans=(ans*a)%mod;
        }
        a=(a*a)%mod;b>>=1;
    }
    return ans;
}

int main(){
    ll i, n, j, t,u,v,p;
    scanf("%lld",&t);
    while(t--){
        ll ans=0,ansn=0;
        scanf("%lld%lld",&n,&p);
        for(i=1;i<=n;i++) scanf("%lld",&k[i]);
        sort(k+1,k+1+n,cmp);
        for(i=1;i<=n;i++){
            if(!ans&&!ansn) ans=(ans+qpow(p,k[i]))%mod,ansn=(ansn+qpow(p,k[i]))%mod2;
            else {
                ans=(ans-qpow(p,k[i])+mod)%mod;
                ansn=(ansn-qpow(p,k[i])+mod2)%mod2;
            }
        }
        printf("%lld\n",ans);
    }
}


#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
#define inf 0x7fffffff
#define ll long long
#define maxn 1000007
#define mod (1000000007)
#define mod2 (1000000006)
ll k[maxn];

bool cmp(ll a,ll b){return a>b;}
ll qpow(ll a,ll b){
    ll ans=1;
    while(b){
        if(b&1){
            ans=(ans*a)%mod;
        }
        a=(a*a)%mod;b>>=1;
    }
    return ans;
}
ll qpow2(ll a,ll b){
    ll ans=1;
    while(b){
        if(b&1){
            ans=(ans*a)%mod2;
        }
        a=(a*a)%mod2;b>>=1;
    }
    return ans;
}
int main(){
    ll i, n, j, t,u,v,p;
    scanf("%lld",&t);
    while(t--){
        ll ans=0,ansn=0;
        scanf("%lld%lld",&n,&p);
        for(i=1;i<=n;i++) scanf("%lld",&k[i]);
        sort(k+1,k+1+n,cmp);
        for(i=1;i<=n;i++){
            if(!ans&&!ansn) ans=(ans+qpow(p,k[i]))%mod,ansn=(ansn+qpow2(p,k[i]))%mod2;
            else {
                ans=(ans-qpow(p,k[i])+mod)%mod;
                ansn=(ansn-qpow2(p,k[i])+mod2)%mod2;
            }
        }
        printf("%lld\n",ans);
    }
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef pair<ll,ll> ii;
typedef vector<ii> vii;
#define all(v) v.begin(), v.end()
#define rep(i, from, to) for (ll i = from; i < ll(to); ++i)

ll M = 1e9 + 7;

ll R = 1e6 + 6;
bool rf = false;

ll xp(ll p, ll k) {
    if (k == 0) return 1;
    ll d = xp(p, k/2);
    d = d * d;
    if (d >= R)
        rf = true;
    d = d % M;
    if (k%2==1)
        d*=p;
    if (d >= R)
        rf = true;
    d = d % M;
    return d;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin.exceptions(cin.failbit);

    ll big = 1e6 + 10;
    ll t;
    cin>>t;
    rep(_,0,t) {
        ll n,p;
        cin>>n>>p;
        vi k(n);
        rep(iii,0,n)
            cin>>k[iii];
        // greedy works
        sort(k.rbegin(),k.rend());
        vi alll(n);
        ll lsr = 0;
        ll lcur = k[0];
        rep(i,0,n) {
            ll diff = lcur - k[i];
            rf = false;
            ll x = xp(p, diff);
            if (rf || (lsr * x > R)) {
                rep(j,i,n) {
                    alll[j] = (lsr < 0) ? 1 : -1;
                }
                break;
            }
            lcur = k[i];
            lsr = lsr * x;
            alll[i] = (lsr < 0) ? 1 : -1;
            lsr = lsr + alll[i];
        }
        ll val=0;
        rep(i,0,n){
            val = (M*M + val + alll[i] * xp(p,k[i])) % M;
        }
        if (lsr < 0)
            cout<<((M-val)%M)<<endl;
        else
            cout <<val<<endl;
    }

    return 0;
}


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

template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl

typedef long long ll;
const ll mod = 1e9+7;
const int maxn = 1e6 + 5;

int n, k;
map<string,vector<ll>> mp;

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);  cout.tie(0);

    cin>>n>>k;

    map<string,vector<ll>> pals;

    auto pal = [](string& s) {
	int len = s.length();
	for (int i=0; i<len/2; i++) {
	    if (s[i] != s[len-i-1]) return false;
	}
	return true;
    };

    for (int i=0; i<n; i++) {
	string s; ll c;
	cin>>s>>c;
	if (pal(s)) {
	    pals[s].push_back(c);
	} else {
	    mp[s].push_back(c);
	}
    }

    for (auto& p: mp) {
	sort(p.second.begin(), p.second.end());
    }

    ll ans = 0;

    //group nonpals
    for (auto &p: mp) {
	string s = p.first;
	string srev = s;
	reverse(srev.begin(), srev.end());
	if (mp.count(srev)) {
	    while (!mp[s].empty() && !mp[srev].empty()) {
		if (mp[s].back() + mp[srev].back() > 0) {
		    ans += mp[s].back() + mp[srev].back();
		    mp[s].pop_back();
		    mp[srev].pop_back();
		} else {
		    break;
		}
	    }
	}
    }

    //group pals, take positive ones
    for (auto& p: pals) {
	sort(p.second.begin(), p.second.end());

	while ((int)p.second.size() >= 2) {
	    int len = p.second.size();
	    if (p.second[len-1]>=0 && p.second[len-2]>=0) {
		ans += p.second[len-1] + p.second[len-2];
		p.second.pop_back();
		p.second.pop_back();
	    } else {
		break;
	    }
	}
    }

    ll single=-1;
    for (auto &p: pals) {
	if (p.second.empty() || p.second.back() <= 0) continue;
	if ((int)p.second.size() == 1) {
	    single = max(single, p.second.back());
	}
    }

    //we have to take this single because it's isolated (only way to take it)
    if (single != -1) {
	//add single, take pairs for rest
	ans += single;
	for (auto& p: pals) {
	    while ((int)p.second.size() >= 2) {
		int len = p.second.size();
		if (p.second[len-1] + p.second[len-2] > 0) {
		    ans += p.second[len-1] + p.second[len-2];
		    p.second.pop_back();
		    p.second.pop_back();
		} else {
		    break;
		}
	    }
	}
    } else {

	ll cur=0;
	set<ll> ss;

	//no isolated single exists
	// only pals of form (-,+) exist
	for (auto &p: pals) {
	    if ((int)p.second.size() >= 2) {
		int len = p.second.size();
		if (p.second[len-1] + p.second[len-2] > 0) {
		    ll a = p.second[len-1];
		    ll b = p.second[len-2];
		    assert(b<0);
		    cur += a+b;
		    ss.insert(b);
		}
	    }
	}

	if (ss.size()) {
	    cur -= *ss.begin();
	    ans += cur;
	}
    }

    cout<<ans<<endl;

    return 0;
}


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

template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl

typedef long long ll;
const ll mod = 1e9+7;
const int maxn = 1e6 + 5;

int n, k;
map<string,vector<ll>> mp;
int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);  cout.tie(0);
    cin>>n>>k;
    for (int i=0; i<n; i++) {
	string s; ll c; cin>>s>>c;
	mp[s].push_back(c);
    }

    ll ans = 0;
    ll mx = 0;
    for (auto& p: mp) {
	string s = p.first;
	string t = s; reverse(t.begin(), t.end());
	if (s<t) continue;

	//pal
	if (s==t) {
	    sort(mp[s].begin(), mp[s].end(), greater<ll>());
	    int i;
	    for (i=0; i+1<(int)mp[s].size(); i+=2) {
		if (mp[s][i]+mp[s][i+1]>=0) {
		    ans += mp[s][i]+mp[s][i+1];
		    mx=max(mx, -mp[s][i+1]);//last neg pair not worth
		} else {
		    break;
		}
	    }
	    if (i<(int)mp[s].size()) {
		mx=max(mx, mp[s][i]);//take odd pal
	    }
	} else {

	    sort(mp[s].begin(), mp[s].end(), greater<ll>());
	    sort(mp[t].begin(), mp[t].end(), greater<ll>());
	    for (int i=0; i<(int)min(mp[s].size(), mp[t].size()); i++) {
		if (mp[s][i]+mp[t][i]>=0) {
		    ans += mp[s][i]+mp[t][i];
		}
	    }
	}

    }

    ans += mx;
    cout<<ans<<endl;

    return 0;
}


#include <bits/stdc++.h>

using ll = long long;
using ull = unsigned long long;
using std::cin;
using std::cout;

// clang-format off
#define newline std::endl
#define print(...) write(cout, __VA_ARGS__)
#define printF(fmt, ...) writeF(cout, fmt, __VA_ARGS__)
#define contains(X, elem) (X.find(elem) != X.end())
inline void write(std::ostream &) {}
template <typename T> void write(std::ostream &s, T t) { s<<t; }
template <typename T, typename... Args> void write(std::ostream &s,T t, Args... args) { s << t << " "; write(s,args...); }
template <typename... Args> void println(Args... args) { print(args...); cout<<newline; }
inline void writeF(std::ostream &s, const char *&format) { s << format; }
template <typename T, typename... Args> void writeF(std::ostream &s, const char *format, T t, Args... args) {
while (*format != '%' && *format) {s.put(*format++);} if (*format++ == '\0') {return;} s << t, writeF(s, format, args...);}
template <typename T> inline void printC(T t) { for (auto &elem : t) print(elem, ""); println(); }
// clang-format on

bool isPalindrome(const std::string &s) {
	return std::equal(s.begin(), s.begin() + s.size() / 2, s.rbegin());
}

int main() {
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(NULL);
	int K, N;
	std::cin >> K >> N;
	std::map<std::string, std::vector<ll>> mapping;
	for (int i = 0; i < K; ++i) {
		std::string str;
		ll cost;
		std::cin >> str >> cost;
		mapping[str].push_back(cost);
	}

	ll ans = 0, max = 0;

	std::map<std::string, std::vector<ll>> palindromes;
	std::map<std::string, std::pair<std::vector<ll>, std::vector<ll>>> pairs;
	for (auto & [ key, values ] : mapping) {
		if (isPalindrome(key))
			palindromes[key] = values;
		else {
			std::string revStr(key.begin(), key.end());
			std::reverse(revStr.begin(), revStr.end());
			pairs.insert({key, {values, mapping[revStr]}});
			mapping.erase(revStr);
		}
	}

	for (auto &elem : palindromes) {
		auto &values = elem.second;
		std::sort(values.begin(), values.end());
		while (values.size() >= 2) {
			ll max1 = values.back();
			values.pop_back();
			ll max2 = values.back();
			values.pop_back();
			if (max1 + max2 > 0LL) {
				ans += max1 + max2;
				max = std::max(max, -max2);
				// println(elem.first, max1, max2);
			}
		}
		// printC(values);
		if (!values.empty()) {
			max = std::max(max, values.back());
			// println(elem.first, values.back(), "max", max);
		}
	}
	for (auto &elem : pairs) {
		auto &first = elem.second.first;
		auto &second = elem.second.second;
		std::sort(first.begin(), first.end());
		std::sort(second.begin(), second.end());
		while (first.size() > 0 && second.size() > 0 && first.back() + second.back() > 0LL) {
			ans += first.back() + second.back();
			// println(elem.first, first.back(), second.back());
			first.pop_back();
			second.pop_back();
		}
	}
	std::cout << ans + max << "\n";
	return 0;
}

/***

7 3
abb 2
aaa -3
bba -1
zyz -4
abb 5
aaa 7
xyx 4
---
12

3 1
a 1
a 2
a 3
---
6

2 5
abcde 10000
abcde 10000
---
0

11 1
c -7359
c -1501
d -7495
d -4777
a -2768
i -2801
u -2385
w -5861
x -5862
q -8036
y -3173

***/


#include <bits/stdc++.h>

using ll = long long;
using ull = unsigned long long;
using std::cin;
using std::cout;

// clang-format off
#define newline std::endl
#define print(...) write(cout, __VA_ARGS__)
#define printF(fmt, ...) writeF(cout, fmt, __VA_ARGS__)
#define contains(X, elem) (X.find(elem) != X.end())
inline void write(std::ostream &) {}
template <typename T> void write(std::ostream &s, T t) { s<<t; }
template <typename T, typename... Args> void write(std::ostream &s,T t, Args... args) { s << t << " "; write(s,args...); }
template <typename... Args> void println(Args... args) { print(args...); cout<<newline; }
inline void writeF(std::ostream &s, const char *&format) { s << format; }
template <typename T, typename... Args> void writeF(std::ostream &s, const char *format, T t, Args... args) {
while (*format != '%' && *format) {s.put(*format++);} if (*format++ == '\0') {return;} s << t, writeF(s, format, args...);}
template <typename T> inline void printC(T t) { for (auto &elem : t) print(elem, ""); println(); }
// clang-format on

bool isPalindrome(const std::string &s) {
	return std::equal(s.begin(), s.begin() + s.size() / 2, s.rbegin());
}

int main() {
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(NULL);
	int K, N;
	std::cin >> K >> N;
	std::map<std::string, std::vector<ll>> mapping;
	for (int i = 0; i < K; ++i) {
		std::string str;
		ll cost;
		std::cin >> str >> cost;
		mapping[str].push_back(cost);
	}

	ll ans = 0, max = 0;

	std::map<std::string, std::vector<ll>> palindromes;
	std::map<std::string, std::pair<std::vector<ll>, std::vector<ll>>> pairs;
	for (auto & [ key, values ] : mapping) {
		if (isPalindrome(key))
			palindromes[key] = values;
		else {
			std::string revStr(key.begin(), key.end());
			std::reverse(revStr.begin(), revStr.end());
			pairs.insert({key, {values, mapping[revStr]}});
			mapping.erase(revStr);
		}
	}

	for (auto &elem : palindromes) {
		auto &values = elem.second;
		std::sort(values.begin(), values.end());
		while (values.size() >= 2) {
			ll max1 = values.back();
			values.pop_back();
			ll max2 = values.back();
			values.pop_back();
			if (max1 + max2 > 0LL) {
				ans += max1 + max2;
				// println(elem.first, max1, max2);
			}
		}
		// printC(values);
		if (!values.empty()) {
			max = std::max(max, values.back());
			// println(elem.first, values.back(), "max", max);
		}
	}
	for (auto &elem : pairs) {
		auto &first = elem.second.first;
		auto &second = elem.second.second;
		std::sort(first.begin(), first.end());
		std::sort(second.begin(), second.end());
		while (first.size() > 0 && second.size() > 0 && first.back() + second.back() > 0LL) {
			ans += first.back() + second.back();
			// println(elem.first, first.back(), second.back());
			first.pop_back();
			second.pop_back();
		}
	}
	std::cout << ans + max << "\n";
	return 0;
}

/***

7 3
abb 2
aaa -3
bba -1
zyz -4
abb 5
aaa 7
xyx 4
---
12

3 1
a 1
a 2
a 3
---
6

2 5
abcde 10000
abcde 10000
---
0

11 1
c 7359
c 1501
d 7495
d 4777
a 2768
i 2801
u 2385
w 5861
x 5862
q 8036
y -3173

***/


#include <bits/stdc++.h>

using ll = long long;
using ull = unsigned long long;
using std::cin;
using std::cout;

// clang-format off
#define newline std::endl
#define print(...) write(cout, __VA_ARGS__)
#define printF(fmt, ...) writeF(cout, fmt, __VA_ARGS__)
#define contains(X, elem) (X.find(elem) != X.end())
inline void write(std::ostream &) {}
template <typename T> void write(std::ostream &s, T t) { s<<t; }
template <typename T, typename... Args> void write(std::ostream &s,T t, Args... args) { s << t << " "; write(s,args...); }
template <typename... Args> void println(Args... args) { print(args...); cout<<newline; }
inline void writeF(std::ostream &s, const char *&format) { s << format; }
template <typename T, typename... Args> void writeF(std::ostream &s, const char *format, T t, Args... args) {
while (*format != '%' && *format) {s.put(*format++);} if (*format++ == '\0') {return;} s << t, writeF(s, format, args...);}
template <typename T> inline void printC(T t) { for (auto &elem : t) print(elem, ""); println(); }
// clang-format on

bool isPalindrome(const std::string &s) {
	return std::equal(s.begin(), s.begin() + s.size() / 2, s.rbegin());
}

int main() {
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(NULL);
	int K, N;
	std::cin >> K >> N;
	std::map<std::string, std::vector<ll>> mapping;
	for (int i = 0; i < K; ++i) {
		std::string str;
		ll cost;
		std::cin >> str >> cost;
		mapping[str].push_back(cost);
	}

	ll ans = 0, max = 0;

	std::map<std::string, std::vector<ll>> palindromes;
	std::map<std::string, std::pair<std::vector<ll>, std::vector<ll>>> pairs;
	for (auto & [ key, values ] : mapping) {
		if (isPalindrome(key))
			palindromes[key] = values;
		else {
			std::string revStr(key.begin(), key.end());
			std::reverse(revStr.begin(), revStr.end());
			pairs.insert({key, {values, mapping[revStr]}});
			mapping.erase(revStr);
		}
	}

	for (auto &elem : palindromes) {
		auto &values = elem.second;
		std::sort(values.begin(), values.end());
		while (values.size() >= 2) {
			ll max1 = values.back();
			values.pop_back();
			ll max2 = values.back();
			values.pop_back();
			if (max1 + max2 > 0LL) {
				ans += max1 + max2;
				max = std::max(max, -max2);
				// println(elem.first, max1, max2);
			} else {
				values.push_back(max2);
				values.push_back(max1);
				break;
			}
		}
		// printC(values);
		if (!values.empty()) {
			max = std::max(max, values.back());
			// println(elem.first, values.back(), "max", max);
		}
	}
	for (auto &elem : pairs) {
		auto &first = elem.second.first;
		auto &second = elem.second.second;
		std::sort(first.begin(), first.end());
		std::sort(second.begin(), second.end());
		while (first.size() > 0 && second.size() > 0 && first.back() + second.back() > 0LL) {
			ans += first.back() + second.back();
			// println(elem.first, first.back(), second.back());
			first.pop_back();
			second.pop_back();
		}
	}
	std::cout << ans + max << "\n";
	return 0;
}

/***

7 3
abb 2
aaa -3
bba -1
zyz -4
abb 5
aaa 7
xyx 4
---
12

3 1
a 1
a 2
a 3
---
6

2 5
abcde 10000
abcde 10000
---
0

11 1
c -7359
c -1501
d -7495
d -4777
a -2768
i -2801
u -2385
w -5861
x -5862
q -8036
y -3173

***/


#include <bits/stdc++.h>

using ll = long long;
using ull = unsigned long long;
using std::cin;
using std::cout;

// clang-format off
#define newline std::endl
#define print(...) write(cout, __VA_ARGS__)
#define printF(fmt, ...) writeF(cout, fmt, __VA_ARGS__)
#define contains(X, elem) (X.find(elem) != X.end())
inline void write(std::ostream &) {}
template <typename T> void write(std::ostream &s, T t) { s<<t; }
template <typename T, typename... Args> void write(std::ostream &s,T t, Args... args) { s << t << " "; write(s,args...); }
template <typename... Args> void println(Args... args) { print(args...); cout<<newline; }
inline void writeF(std::ostream &s, const char *&format) { s << format; }
template <typename T, typename... Args> void writeF(std::ostream &s, const char *format, T t, Args... args) {
while (*format != '%' && *format) {s.put(*format++);} if (*format++ == '\0') {return;} s << t, writeF(s, format, args...);}
template <typename T> inline void printC(T t) { for (auto &elem : t) print(elem, ""); println(); }
// clang-format on

bool isPalindrome(const std::string &s) {
	return std::equal(s.begin(), s.begin() + s.size() / 2, s.rbegin());
}

int main() {
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(NULL);
	int K, N;
	std::cin >> K >> N;
	std::map<std::string, std::vector<ll>> mapping;
	for (int i = 0; i < K; ++i) {
		std::string str;
		ll cost;
		std::cin >> str >> cost;
		mapping[str].push_back(cost);
	}

	ll ans = 0, max = 0;

	std::map<std::string, std::vector<ll>> palindromes;
	std::map<std::string, std::pair<std::vector<ll>, std::vector<ll>>> pairs;
	for (auto & [ key, values ] : mapping) {
		if (isPalindrome(key))
			palindromes[key] = values;
		else {
			std::string revStr(key.begin(), key.end());
			std::reverse(revStr.begin(), revStr.end());
			pairs.insert({key, {values, mapping[revStr]}});
			mapping.erase(revStr);
		}
	}

	for (auto &elem : palindromes) {
		auto &values = elem.second;
		std::sort(values.begin(), values.end());
		while (values.size() >= 2) {
			ll max1 = values.back();
			values.pop_back();
			ll max2 = values.back();
			values.pop_back();
			if (max1 + max2 > 0LL) {
				ans += max1 + max2;
				max = std::max(max, -max2);
				// println(elem.first, max1, max2);
			}
			else
				break;
		}
		// printC(values);
		if (!values.empty()) {
			max = std::max(max, values.back());
			// println(elem.first, values.back(), "max", max);
		}
	}
	for (auto &elem : pairs) {
		auto &first = elem.second.first;
		auto &second = elem.second.second;
		std::sort(first.begin(), first.end());
		std::sort(second.begin(), second.end());
		while (first.size() > 0 && second.size() > 0 && first.back() + second.back() > 0LL) {
			ans += first.back() + second.back();
			// println(elem.first, first.back(), second.back());
			first.pop_back();
			second.pop_back();
		}
	}
	std::cout << ans + max << "\n";
	return 0;
}

/***

7 3
abb 2
aaa -3
bba -1
zyz -4
abb 5
aaa 7
xyx 4
---
12

3 1
a 1
a 2
a 3
---
6

2 5
abcde 10000
abcde 10000
---
0

11 1
c -7359
c -1501
d -7495
d -4777
a -2768
i -2801
u -2385
w -5861
x -5862
q -8036
y -3173

***/


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

#define MX1       100005
#define MX2       67108870
#define IMX       1000000000
#define BASE1     31LL
#define BASE2     59LL
#define MOD1      1000000007LL
#define MOD2      1000000009LL
#define LL        long long int
#define PII       pair<int,int>
#define PIP       pair<int,PII>
#define PLI       pair<LL,int>
#define PIL       pair<int,LL>
#define PLL       pair<LL,LL>
#define PDD       pair<double,double>
#define PPI       pair<PLL,int>
#define PCI       pair<char,int>
#define LFT(x)    (x<<1)
#define RHT(x)    ((x<<1)+1)
#define MID(x,y)  ((x+y)>>1)
#define FAST      ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

inline int setBit(int mask,int pos){    return  (mask | (1<<pos));}
inline int resetBit(int mask,int pos){  return  (mask & ~(1<<pos));}
inline bool checkBit(int mask,int pos){ return  (bool)(mask & (1<<pos));}
#define PC(x)   __builtin_popcount(x)

const LL BASE[] = {31LL, 59LL};
const LL MOD[] = {1000000007LL, 1000000009LL};

map<PLL,vector<int>>mpHashVal;
map<PLL,PLL>mpRevHash;
map<PLL,bool>mpPal,done;
vector<PII>vec;
int n,k;
string str;

void calcHash(int val)
{
    LL h[4],x;
    h[0] = h[1] = 0;

    for(int i=0; str[i]; i++)
    {
        x = str[i] - 'a';
        x++;
        for(int j=0; j<2; j++)
        {
            h[j] = (h[j] * BASE[j])%MOD[j];
            h[j] = (h[j] + x)%MOD[j];
        }
    }
    //cout<<h[0]<<" "<<h[1]<<endl;
    mpHashVal[PLL(h[0], h[1])].push_back(val);

    h[2] = h[3] = 0;
    for(int i=(int)str.size()-1; i>=0; i--)
    {
        x = str[i] - 'a';
        x++;
        for(int j=0; j<2; j++)
        {
            h[j+2] = (h[j+2] * BASE[j])%MOD[j];
            h[j+2] = (h[j+2] + x)%MOD[j];
        }
    }
    mpRevHash[PLL(h[0], h[1])] = PLL(h[2], h[3]);

    if(h[0]==h[2] && h[1]==h[3])    mpPal[PLL(h[0], h[1])] = true;
}

int main()
{
    FAST;
    int w;

    cin >> n >> k;
    for(int i=0; i<n; i++)
    {
        cin >> str >> w;
        calcHash(w);
    }

    map<PLL,vector<int>>::iterator it;
    for(it=mpHashVal.begin(); it!=mpHashVal.end(); it++)
    {
        sort(it->second.begin(), it->second.end(), greater<int>());
    }

    int ans = 0;
    for(it=mpHashVal.begin(); it!=mpHashVal.end(); it++)
    {
        if(done.find(it->first)!=done.end())    continue;
        if(mpPal.find(it->first)!=mpPal.end())  /// palindrome
        {
            int c = 0,s = 0;
            int sz = it->second.size();
            for(int i=0; i<sz; i++)
            {
                int itv = it->second[i];
                //cout<<itv<<" ";
                if(itv>0)
                {
                    if(c==1)
                    {
                        s += itv;
                        ans += s;
                        s = 0;
                        c = 0;
                    }
                    else
                    {
                        s += itv;
                        c++;
                        if((i+1)==sz)   vec.push_back(PII(s, INT_MAX));
                    }
                }
                else if(itv==0 && c==1)
                {
                    ans += s;
                    break;
                }
                else if(itv<0 && c==1)
                {
                    vec.push_back(PII(s, itv));
                    break;
                }
                else    break;
            }
            //cout<<endl;
        }
        else
        {
            PII pr = mpRevHash[it->first];
            if(mpHashVal.find(pr)==mpHashVal.end()) continue;

            int s1 = it->second.size();
            int s2 = mpHashVal[pr].size();
            s1 = min(s1, s2);
            for(int i=0; i<s1; i++)
            {
                int v1 = mpHashVal[it->first][i];
                int v2 = mpHashVal[pr][i];
                if((v1+v2)>=0)  ans += (v1 + v2);
            }
        }

        done[it->first] = true;
        done[mpRevHash[it->first]] = true;
    }
    //cout<<ans<<endl;

    int s1 = 0,s2 = 0;
    for(PII pr : vec)
    {
        if(pr.second==INT_MAX)  s1 = max(s1, pr.first);
        else    s2 += (pr.first + pr.second);
    }
    for(PII pr : vec)
    {
        if(pr.second==INT_MAX)  continue;

        int d = (pr.first + pr.second);
        int t1 = s2 - d;
        int t2 = s2 + s1;
        int t3 = t1 + s1;
        int t4 = t1 + pr.first;
        t1 = max(max(t1, t2), max(t3, t4));
        s2 = max(s2, t1);
    }
    s2 = max(s2, s1);
    ans += s2;

    cout<<ans<<endl;

    return  0;
}


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

#define MX1       100005
#define MX2       67108870
#define IMX       1000000000
#define BASE1     31LL
#define BASE2     59LL
#define MOD1      1000000007LL
#define MOD2      1000000009LL
#define LL        long long int
#define PII       pair<int,int>
#define PIP       pair<int,PII>
#define PLI       pair<LL,int>
#define PIL       pair<int,LL>
#define PLL       pair<LL,LL>
#define PDD       pair<double,double>
#define PPI       pair<PLL,int>
#define PCI       pair<char,int>
#define LFT(x)    (x<<1)
#define RHT(x)    ((x<<1)+1)
#define MID(x,y)  ((x+y)>>1)
#define FAST      ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

inline int setBit(int mask,int pos){    return  (mask | (1<<pos));}
inline int resetBit(int mask,int pos){  return  (mask & ~(1<<pos));}
inline bool checkBit(int mask,int pos){ return  (bool)(mask & (1<<pos));}
#define PC(x)   __builtin_popcount(x)

const LL BASE[] = {31LL, 59LL};
const LL MOD[] = {1000000007LL, 1000000009LL};

map<PLL,vector<int>>mpHashVal;
map<PLL,PLL>mpRevHash;
map<PLL,bool>mpPal,done;
vector<PII>vec;
int n,k;
string str;

void calcHash(int val)
{
    LL h[4],x;
    h[0] = h[1] = 0;

    for(int i=0; str[i]; i++)
    {
        x = str[i] - 'a';
        x++;
        for(int j=0; j<2; j++)
        {
            h[j] = (h[j] * BASE[j])%MOD[j];
            h[j] = (h[j] + x)%MOD[j];
        }
    }
    //cout<<h[0]<<" "<<h[1]<<endl;
    mpHashVal[PLL(h[0], h[1])].push_back(val);

    h[2] = h[3] = 0;
    for(int i=(int)str.size()-1; i>=0; i--)
    {
        x = str[i] - 'a';
        x++;
        for(int j=0; j<2; j++)
        {
            h[j+2] = (h[j+2] * BASE[j])%MOD[j];
            h[j+2] = (h[j+2] + x)%MOD[j];
        }
    }
    mpRevHash[PLL(h[0], h[1])] = PLL(h[2], h[3]);

    if(h[0]==h[2] && h[1]==h[3])    mpPal[PLL(h[0], h[1])] = true;
}

int main()
{
    FAST;
    int w;

    cin >> n >> k;
    for(int i=0; i<n; i++)
    {
        cin >> str >> w;
        calcHash(w);
    }

    map<PLL,vector<int>>::iterator it;
    for(it=mpHashVal.begin(); it!=mpHashVal.end(); it++)
    {
        sort(it->second.begin(), it->second.end(), greater<int>());
    }

    int ans = 0;
    for(it=mpHashVal.begin(); it!=mpHashVal.end(); it++)
    {
        if(done.find(it->first)!=done.end())    continue;
        if(mpPal.find(it->first)!=mpPal.end())  /// palindrome
        {
            int c = 0,s = 0;
            int sz = it->second.size();
            for(int i=0; i<sz; i++)
            {
                int itv = it->second[i];
                //cout<<itv<<" ";
                if(itv>0)
                {
                    if(c==1)
                    {
                        s += itv;
                        ans += s;
                        s = 0;
                        c = 0;
                    }
                    else
                    {
                        s += itv;
                        c++;
                        if((i+1)==sz)   vec.push_back(PII(s, INT_MAX));
                    }
                }
                else if(itv==0 && c==1)
                {
                    ans += s;
                    break;
                }
                else if(itv<0 && c==1)
                {
                    vec.push_back(PII(s, itv));
                    break;
                }
                else    break;
            }
            //cout<<endl;
        }
        else
        {
            PII pr = mpRevHash[it->first];
            if(mpHashVal.find(pr)==mpHashVal.end()) continue;

            int s1 = it->second.size();
            int s2 = mpHashVal[pr].size();
            s1 = min(s1, s2);
            for(int i=0; i<s1; i++)
            {
                int v1 = it->second[i];
                int v2 = mpHashVal[pr][i];
                if((v1+v2)>=0)  ans += (v1 + v2);
            }
        }

        done[it->first] = true;
        done[mpRevHash[it->first]] = true;
    }
    //cout<<ans<<endl;

    int s1 = 0,s2 = 0;
    for(PII pr : vec)
    {
        if(pr.second==INT_MAX || (pr.first+pr.second)<=0)   s1 = max(s1, pr.first);
        else    s2 += (pr.first + pr.second);
    }

    int mxs = s1 + s2,s3 = 0;
    for(PII pr : vec)
    {
        if(pr.second==INT_MAX || (pr.first+pr.second)<=0)   continue;

        int d = (pr.first + pr.second);
        int t1 = s2 - d + pr.first + s3;
        int t2 = s2 + s1;
        if(t1>t2)
        {
            s2 = s2 - d + s3;
            s1 = pr.first;
            s3 += d;
        }
    }
    s2 += s1;
    mxs = max(mxs, s2);
    ans += mxs;

    cout<<ans<<endl;

    return  0;
}


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

#define MX1       100005
#define MX2       67108870
#define IMX       1000000000
#define BASE1     31LL
#define BASE2     59LL
#define MOD1      1000000007LL
#define MOD2      1000000009LL
#define LL        long long int
#define PII       pair<int,int>
#define PIP       pair<int,PII>
#define PLI       pair<LL,int>
#define PIL       pair<int,LL>
#define PLL       pair<LL,LL>
#define PDD       pair<double,double>
#define PPI       pair<PLL,int>
#define PCI       pair<char,int>
#define LFT(x)    (x<<1)
#define RHT(x)    ((x<<1)+1)
#define MID(x,y)  ((x+y)>>1)
#define FAST      ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

inline int setBit(int mask,int pos){    return  (mask | (1<<pos));}
inline int resetBit(int mask,int pos){  return  (mask & ~(1<<pos));}
inline bool checkBit(int mask,int pos){ return  (bool)(mask & (1<<pos));}
#define PC(x)   __builtin_popcount(x)

const LL BASE[] = {31LL, 59LL};
const LL MOD[] = {1000000007LL, 1000000009LL};

map<PLL,vector<int>>mpHashVal;
map<PLL,PLL>mpRevHash;
map<PLL,bool>mpPal,done;
vector<PII>vec;
int n,k;
string str;

void calcHash(int val)
{
    LL h[4],x;
    h[0] = h[1] = 0;

    for(int i=0; str[i]; i++)
    {
        x = str[i] - 'a';
        x++;
        for(int j=0; j<2; j++)
        {
            h[j] = (h[j] * BASE[j])%MOD[j];
            h[j] = (h[j] + x)%MOD[j];
        }
    }
    //cout<<h[0]<<" "<<h[1]<<endl;
    mpHashVal[PLL(h[0], h[1])].push_back(val);

    h[2] = h[3] = 0;
    for(int i=(int)str.size()-1; i>=0; i--)
    {
        x = str[i] - 'a';
        x++;
        for(int j=0; j<2; j++)
        {
            h[j+2] = (h[j+2] * BASE[j])%MOD[j];
            h[j+2] = (h[j+2] + x)%MOD[j];
        }
    }
    mpRevHash[PLL(h[0], h[1])] = PLL(h[2], h[3]);

    if(h[0]==h[2] && h[1]==h[3])    mpPal[PLL(h[0], h[1])] = true;
}

int main()
{
    FAST;
    int w;

    cin >> n >> k;
    for(int i=0; i<n; i++)
    {
        cin >> str >> w;
        calcHash(w);
    }

    map<PLL,vector<int>>::iterator it;
    for(it=mpHashVal.begin(); it!=mpHashVal.end(); it++)
    {
        sort(it->second.begin(), it->second.end(), greater<int>());
    }

    int ans = 0;
    for(it=mpHashVal.begin(); it!=mpHashVal.end(); it++)
    {
        if(done.find(it->first)!=done.end())    continue;
        if(mpPal.find(it->first)!=mpPal.end())  /// palindrome
        {
            int c = 0,s = 0;
            int sz = it->second.size();
            for(int i=0; i<sz; i++)
            {
                int itv = it->second[i];
                //cout<<itv<<" ";
                if(itv>0)
                {
                    if(c==1)
                    {
                        s += itv;
                        ans += s;
                        s = 0;
                        c = 0;
                    }
                    else
                    {
                        s += itv;
                        c++;
                        if((i+1)==sz)   vec.push_back(PII(s, INT_MAX));
                    }
                }
                else if(itv==0 && c==1)
                {
                    ans += s;
                    break;
                }
                else if(itv<0 && c==1)
                {
                    vec.push_back(PII(s, itv));
                    break;
                }
                else    break;
            }
            //cout<<endl;
        }
        else
        {
            PII pr = mpRevHash[it->first];
            if(mpHashVal.find(pr)==mpHashVal.end()) continue;

            int s1 = it->second.size();
            int s2 = mpHashVal[pr].size();
            s1 = min(s1, s2);
            for(int i=0; i<s1; i++)
            {
                int v1 = it->second[i];
                int v2 = mpHashVal[pr][i];
                if((v1+v2)>=0)  ans += (v1 + v2);
            }
        }

        done[it->first] = true;
        done[mpRevHash[it->first]] = true;
    }
    //cout<<ans<<endl;

    int s1 = 0,s2 = 0;
    for(PII pr : vec)
    {
        if(pr.second==INT_MAX || (pr.first+pr.second)<=0)   s1 = max(s1, pr.first);
        else    s2 += (pr.first + pr.second);
    }

    int mxs = s1 + s2,s3 = 0;
    for(PII pr : vec)
    {
        if(pr.second==INT_MAX || (pr.first+pr.second)<=0)   continue;

        int d = (pr.first + pr.second);
        int t1 = s2 - d + pr.first + s3;
        int t2 = s2 + s1;
        if(t1>t2)
        {
            s2 = s2 - d + s3;
            s1 = pr.first;
            s3 = d;
        }
    }
    s2 += s1;
    mxs = max(mxs, s2);
    ans += mxs;

    cout<<ans<<endl;

    return  0;
}


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

#define MX1       100005
#define MX2       67108870
#define IMX       1000000000
#define BASE1     31LL
#define BASE2     59LL
#define MOD1      1000000007LL
#define MOD2      1000000009LL
#define LL        long long int
#define PII       pair<int,int>
#define PIP       pair<int,PII>
#define PLI       pair<LL,int>
#define PIL       pair<int,LL>
#define PLL       pair<LL,LL>
#define PDD       pair<double,double>
#define PPI       pair<PLL,int>
#define PCI       pair<char,int>
#define LFT(x)    (x<<1)
#define RHT(x)    ((x<<1)+1)
#define MID(x,y)  ((x+y)>>1)
#define FAST      ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

inline int setBit(int mask,int pos){    return  (mask | (1<<pos));}
inline int resetBit(int mask,int pos){  return  (mask & ~(1<<pos));}
inline bool checkBit(int mask,int pos){ return  (bool)(mask & (1<<pos));}
#define PC(x)   __builtin_popcount(x)

const LL BASE[] = {31LL, 59LL};
const LL MOD[] = {1000000007LL, 1000000009LL};

map<PLL,vector<int>>mpHashVal;
map<PLL,PLL>mpRevHash;
map<PLL,bool>mpPal,done;
vector<PII>vec;
int n,k;
string str;

void calcHash(int val)
{
    LL h[4],x;
    h[0] = h[1] = 0;

    for(int i=0; str[i]; i++)
    {
        x = str[i] - 'a';
        x++;
        for(int j=0; j<2; j++)
        {
            h[j] = (h[j] * BASE[j])%MOD[j];
            h[j] = (h[j] + x)%MOD[j];
        }
    }
    //cout<<h[0]<<" "<<h[1]<<endl;
    mpHashVal[PLL(h[0], h[1])].push_back(val);

    h[2] = h[3] = 0;
    for(int i=(int)str.size()-1; i>=0; i--)
    {
        x = str[i] - 'a';
        x++;
        for(int j=0; j<2; j++)
        {
            h[j+2] = (h[j+2] * BASE[j])%MOD[j];
            h[j+2] = (h[j+2] + x)%MOD[j];
        }
    }
    mpRevHash[PLL(h[0], h[1])] = PLL(h[2], h[3]);

    if(h[0]==h[2] && h[1]==h[3])    mpPal[PLL(h[0], h[1])] = true;
}

int main()
{
    FAST;
    int w;

    cin >> n >> k;
    for(int i=0; i<n; i++)
    {
        //cout<<i<<endl;
        cin >> str >> w;
        calcHash(w);
    }

    map<PLL,vector<int>>::iterator it;
    for(it=mpHashVal.begin(); it!=mpHashVal.end(); it++)
    {
        sort(it->second.begin(), it->second.end(), greater<int>());
    }

    int ans = 0;
    for(it=mpHashVal.begin(); it!=mpHashVal.end(); it++)
    {
        if(done.find(it->first)!=done.end())    continue;
        if(mpPal.find(it->first)!=mpPal.end())  /// palindrome
        {
            int c = 0,s = 0;
            int sz = it->second.size();
            for(int i=0; i<sz; i++)
            {
                int itv = it->second[i];
                //cout<<itv<<" ";
                if(itv>0)
                {
                    if(c==1)
                    {
                        s += itv;
                        ans += s;
                        s = 0;
                        c = 0;
                    }
                    else
                    {
                        s += itv;
                        c++;
                        if((i+1)==sz)   vec.push_back(PII(s, INT_MAX));
                    }
                }
                else if(itv==0 && c==1)
                {
                    ans += s;
                    break;
                }
                else if(itv<0 && c==1)
                {
                    vec.push_back(PII(s, itv));
                    break;
                }
            }
            //cout<<endl;
        }
        else
        {
            PII pr = mpRevHash[it->first];
            if(mpHashVal.find(pr)==mpHashVal.end()) continue;

            int s1 = it->second.size();
            int s2 = mpHashVal[pr].size();
            s1 = min(s1, s2);
            for(int i=0; i<s1; i++)
            {
                int v1 = mpHashVal[it->first][i];
                int v2 = mpHashVal[pr][i];
                if((v1+v2)>=0)  ans += (v1 + v2);
            }
        }

        done[it->first] = true;
        done[mpRevHash[it->first]] = true;
    }
    //cout<<ans<<endl;

    int s1 = 0,s2 = 0;
    for(PII pr : vec)
    {
        if(pr.second==INT_MAX)  s1 = max(s1, pr.first);
        else    s2 += (pr.first + pr.second);
    }
    for(PII pr : vec)
    {
        if(pr.second==INT_MAX)  continue;

        int d = (pr.first + pr.second);
        int t1 = s2 - d;
        int t2 = s2 + s1;
        int t3 = t1 + s1;
        int t4 = t1 + pr.first;
        t1 = max(max(t1, t2), max(t3, t4));
        s2 = max(s2, t1);
    }
    s2 = max(s2, s1);
    ans += s2;

    cout<<ans<<endl;

    return  0;
}


#include <bits/stdc++.h>
#include <fstream>
using namespace std;
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define repe(i,a,b) for(ll i=a;i<=b;i++)
#define bac(i,a,b) for(ll i=a;i>=b;i--)
#define pb push_back
#define mp make_pair
#define F first
#define S second
typedef long long ll;
typedef vector< ll > vi;
typedef vector<pair<ll,ll> > vpii;
typedef pair<ll,ll> pii;
#define all(c) c.begin(),c.end()
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define setbitc(x) __builtin_popcount(x)
#define init(x,a) memset(x,a,sizeof(x))
#define PI           3.14159265358979323846  /* pi */
mt19937 rng32(chrono::steady_clock::now().time_since_epoch().count());
#define N 1000005
//Variables Start
ll inf=1e18,MOD=1e9+7;
ll n,m,k,ar[N],br[N];
string s,s1;
vi adj[N];
map<string,vi> ma;
map<string,set<ll>> ma2;
int main()
{
    ios
    int ti=1;
    //cin>>ti;
    while(ti--)
    {
        cin>>n>>m;
        rep(i,0,n)
        {
            ll val;string s;
            cin>>s>>val;
            ma[s].pb(val);
        }
        ll max_neg=0,max_pos=0;
        ll ans=0;
        //cout<<"yo"<<endl;
        for(auto i:ma)
        {

            string s=i.F;
            string z=s;
            reverse(all(z));
            if(z<s)continue;
            sort(all(ma[s]),greater<ll> ());
            sort(all(ma[z]),greater<ll> ());
            if(z==s)
            {
                ll j;
                for(j=0;j+1<ma[s].size();j+=2)
                {
                    //cout<<i.S[j-1]<<" "<<i.S[j]<<endl;
                    if(ma[s][j]+ma[s][j+1]>=0)
                    {
                        ans+=ma[s][j]+ma[s][j+1];
                        max_pos=max(max_pos,-ma[s][j+1]);
                    }
                }
                if(j<ma[s].size())max_pos=max(max_pos,ma[s][j]);
            }
            else
            {
                    vi temp=ma[z];
                    rep(j,0,min(temp.size(),ma[s].size()))
                    {
                        if(temp[j]+ma[s][j]>=0)ans+=temp[j]+ma[s][j];
                    }

            }
        }
        //cout<<max_pos<<endl;
        ans+=max_pos;

        cout<<ans<<endl;

    }
    return 0;

}


#include <bits/stdc++.h>
using namespace std;
const double pi=2*acos(0.0);
double inff=1.0/0.0;
#define ll                       long long int
#define inf                      10000000000000LL
#define MOD1                     998244353
#define MOD2                     1000000007
#define pb                       push_back
#define sz(x)                    (int)x.size()
#define mp                       make_pair
#define lbd                      lower_bound
#define ubd                      upper_bound
#define ipair                    pair<ll,ll>
#define all(v)                   v.begin(),v.end()
#define rep(a,b,c)               for(a=(int)b;a<(int)c;a++)
#define rep2(a,b,c)              for(a=(int)b;a<(int)c;a+=2)
#define bac(a,b,c)               for(a=(int)b;a>=(int)c;a--)
#define se                       second
#define f                        first
#define ios                      ios::sync_with_stdio(false), cout.tie(0), cin.tie(0);
#define mem(a,val)               memset(a,val,sizeof a)
#define endl                     "\n"
#define mxn                      100011
#define vi                       vector<ll>
#define vii                      vector<ipair>
#define si                       set<ll>
#define trace1(x)                cerr << #x << ": " << x << "\n";
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << "\n";
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << "\n";
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << "\n";
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
/*Variables Start*/
ll x,y,n,i,z,j,k,w,u,m,t,g,q;
map<string,vi> m1;
/*Variables End*/
void solve()
{
  string str,res="";
  cin>>k>>n;
  rep(i,0,k)
  {
    cin>>str>>x;
    m1[str].pb(x);
  }
  for(auto a:m1)
  {
    string rev=a.f;
    reverse(all(rev));
    if(rev<a.f) continue;
    sort(all(a.se),greater<ll>());
    if(rev==a.f)
    {
      for(j=0;j<a.se.size()-1;j+=2)
      {
        if(a.se[j]+a.se[j+1]>=0)
        {
          w+=(a.se[j]+a.se[j+1]);
          y=max(y,-a.se[j+1]);
        }
      }
      if(j!=a.se.size()) y=max(y,a.se[j]);
    }
    else
    {
      sort(all(m1[rev]),greater<ll>());
      for(j=0;j<min(a.se.size(),m1[rev].size());j++)
      {
        if(a.se[j]+m1[rev][j]>0) w+=a.se[j]+m1[rev][j];
      }
    }
  }
  w+=y;
  cout<<w<<endl;
}
int main()
{
  int TESTS=1;
  ios;
  // cin>>TESTS;
  while(TESTS--)
  {
    solve();
  }
}


#include <bits/stdc++.h>
using namespace std;
const double pi=2*acos(0.0);
double inff=1.0/0.0;
#define ll                       long long int
#define inf                      10000000000000LL
#define MOD1                     998244353
#define MOD2                     1000000007
#define pb                       push_back
#define sz(x)                    (int)x.size()
#define mp                       make_pair
#define lbd                      lower_bound
#define ubd                      upper_bound
#define ipair                    pair<ll,ll>
#define all(v)                   v.begin(),v.end()
#define rep(a,b,c)               for(a=(int)b;a<(int)c;a++)
#define rep2(a,b,c)              for(a=(int)b;a<(int)c;a+=2)
#define bac(a,b,c)               for(a=(int)b;a>=(int)c;a--)
#define se                       second
#define f                        first
#define ios                      ios::sync_with_stdio(false), cout.tie(0), cin.tie(0);
#define mem(a,val)               memset(a,val,sizeof a)
#define endl                     "\n"
#define mxn                      100011
#define vi                       vector<ll>
#define vii                      vector<ipair>
#define si                       set<ll>
#define trace1(x)                cerr << #x << ": " << x << "\n";
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << "\n";
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << "\n";
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << "\n";
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
/*Variables Start*/
ll x,y,n,i,z,j,k,w,u,m,t,g,q;
map<string,vi> m1;
/*Variables End*/
void solve()
{
  string str,res="";
  cin>>k>>n;
  rep(i,0,k)
  {
    cin>>str>>x;
    m1[str].pb(x);
  }
  for(auto a:m1)
  {
    string rev=a.f;
    reverse(all(rev));
    if(rev<a.f) continue;
    sort(all(a.se),greater<ll>());
    if(rev==a.f)
    {
      for(j=0;j<a.se.size()-1;j+=2)
      {
        if(a.se[j]+a.se[j+1]>=0)
        {
          w+=(a.se[j]+a.se[j+1]);
          y=max(y,-a.se[j+1]);
        }
      }
      if(j<a.se.size()) y=max(y,a.se[j]);
    }
    else
    {
      sort(all(m1[rev]),greater<ll>());
      for(j=0;j<min(a.se.size(),m1[rev].size());j++)
      {
        if(a.se[j]+m1[rev][j]>0) w+=a.se[j]+m1[rev][j];
      }
    }
  }
  w+=y;
  cout<<w<<endl;
}
int main()
{
  int TESTS=1;
  ios;
  // cin>>TESTS;
  while(TESTS--)
  {
    solve();
  }
}


#include <bits/stdc++.h>
using namespace std;
const double pi=2*acos(0.0);
double inff=1.0/0.0;
#define ll                       long long int
#define inf                      10000000000000LL
#define MOD1                     998244353
#define MOD2                     1000000007
#define pb                       push_back
#define sz(x)                    (int)x.size()
#define mp                       make_pair
#define lbd                      lower_bound
#define ubd                      upper_bound
#define ipair                    pair<ll,ll>
#define all(v)                   v.begin(),v.end()
#define rep(a,b,c)               for(a=(int)b;a<(int)c;a++)
#define rep2(a,b,c)              for(a=(int)b;a<(int)c;a+=2)
#define bac(a,b,c)               for(a=(int)b;a>=(int)c;a--)
#define se                       second
#define f                        first
#define ios                      ios::sync_with_stdio(false), cout.tie(0), cin.tie(0);
#define mem(a,val)               memset(a,val,sizeof a)
#define endl                     "\n"
#define mxn                      100011
#define vi                       vector<ll>
#define vii                      vector<ipair>
#define si                       set<ll>
#define trace1(x)                cerr << #x << ": " << x << "\n";
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << "\n";
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << "\n";
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << "\n";
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
/*Variables Start*/
ll x,y,n,i,z,j,k,w,u,m,t,g,q;
map<string,vi> m1;
/*Variables End*/
void solve()
{
  string str,res="";
  cin>>k>>n;
  rep(i,0,k)
  {
    cin>>str>>x;
    m1[str].pb(x);
  }
  for(auto a:m1)
  {
    string rev=a.f;
    reverse(all(rev));
    if(rev<a.f) continue;
    sort(all(a.se),greater<ll>());
    if(rev==a.f)
    {
      for(j=0;j<a.se.size()-1;j+=2)
      {
        if(a.se[j]+a.se[j+1]>=0)
        {
          w+=(a.se[j]+a.se[j+1]);
          y=max(y,-a.se[j+1]);
        }
        else break;
      }
      if(j<a.se.size()) y=max(y,a.se[j]);
    }
    else
    {
      sort(all(m1[rev]),greater<ll>());
      for(j=0;j<min(a.se.size(),m1[rev].size());j++)
      {
        if(a.se[j]+m1[rev][j]>0) w+=a.se[j]+m1[rev][j];
        else break;
      }
    }
  }
  w+=y;
  cout<<w<<endl;
}
int main()
{
  int TESTS=1;
  ios;
  // cin>>TESTS;
  while(TESTS--)
  {
    solve();
  }
}