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

const int MOD = 1000000007;

int pow(const int x, const int y, bool& flag) {
    if (0 == x) {
        return 0;
    }
    long long res = 1;
    for (int i = 0; i < y; ++i) {
        res *= x;
        if (res >= MOD) {
            flag = true;
        }
        res %= MOD;
    }
    return res;
}

int multi(const int x, const int y, bool& flag) {
    if (0 == x) {
        return 0;
    }
    long long res = (long long)x*y;
    if (res >= MOD) {
        flag = true;
    }
    return res%MOD;
}

int sub(const int x, const int y) {
    return (x-y+MOD)%MOD;
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n, p;
        cin >> n >> p;
        map<int, int> x2n;
        for (int i = 0; i < n; ++i) {
            int k;
            cin >> k;
            x2n[k]++;
        }
        int x = 0;
        bool flag = false;
        for (auto iter = x2n.rbegin(); iter != x2n.rend(); ++iter) {
            if (iter != x2n.rbegin()) {
                int k = iter->first;
                --iter;
                x = multi(x, pow(p, iter->first-k, flag), flag);
                ++iter;
            }
            if (flag) {
                x = sub(x, iter->second);
            } else if (x <= iter->second) {
                if ((iter->second-x)%2) {
                    x = 1;
                } else {
                    x = 0;
                }
            } else {
                x = sub(x, iter->second);
            }
        }
        cout << multi(x, pow(p, x2n.begin()->first, flag), flag) << endl;
    }

    return 0;
}


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

const int MOD = 1000000007;

int mpow(const int x, const int y) {
    long long res = 1;
    for (int i = 0; i < y; ++i) {
        res *= x;
        res %= MOD;
    }
    return res;
}

int pow(const int x, const int y, bool& flag) {
    long long res = 1;
    for (int i = 0; i < y; ++i) {
        res *= x;
        if (res >= MOD) {
            flag = true;
            return res;
        }
    }
    return res;
}

int mmulti(const int x, const int y) {
    if (0 == x) {
        return 0;
    }
    return (long long)x*y%MOD;
}

int multi(const int x, const int y, bool& flag) {
    if (0 == x) {
        return 0;
    }
    auto res = (long long)x*y;
    if (res >= MOD) {
        flag = true;
    }
    return res;
}

int msub(const int x, const int y) {
    return (x-y+MOD)%MOD;
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n, p;
        cin >> n >> p;
        map<int, int> x2n;
        for (int i = 0; i < n; ++i) {
            int k;
            cin >> k;
            x2n[k]++;
        }
        int mx = 0;
        int x = 0;
        bool flag = false;
        for (auto iter = x2n.rbegin(); iter != x2n.rend(); ++iter) {
            if (iter != x2n.rbegin()) {
                int k = iter->first;
                --iter;
                int delta = iter->first-k;
                ++iter;
                mx = mmulti(mx, mpow(p, delta));
                if (!flag) {
                    x = multi(x, pow(p, delta, flag), flag);
                }
            }
            if (flag) {
                mx = msub(mx, iter->second);
            } else if (x <= iter->second) {
                if ((iter->second-x)%2) {
                    mx = x = 1;
                } else {
                    mx = x = 0;
                }
            } else {
                x -= iter->second;
                mx = x;
            }
        }
        cout << mmulti(mx, mpow(p, x2n.begin()->first)) << endl;
    }

    return 0;
}


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

const long long INF = 1000010;
const long long MOD = 1000000007;

int mpow(const int x, int y) {
    long long res = 1;
    long long base = x;
    while (y) {
        if (y&1) {
            res *= base;
            res %= MOD;
        }
        base = base*base%MOD;
        y >>= 1;
    }
    return res;
}

int pow(const int x, int y) {
    long long res = 1;
    long long base = x;
    while (y) {
        if (y&1) {
            res *= base;
            if (res >= INF) {
                return INF;
            }
        }
        base = base*base;
        if (base >= INF) {
            return INF;
        }
        y >>= 1;
    }
    return res;
}

int mmulti(const int x, const int y) {
    if (0 == x) {
        return 0;
    }
    return (long long)x*y%MOD;
}

int multi(const int x, const int y, bool& flag) {
    if (flag) {
        return 0;
    }
    if (0 == x) {
        return 0;
    }
    auto res = (long long)x*y;
    if (res >= INF) {
        flag = true;
    }
    return res;
}

int msub(const int x, const int y) {
    return (x+y*(MOD-1))%MOD;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--) {
        int n, p;
        cin >> n >> p;
        map<int, int> x2n;
        for (int i = 0; i < n; ++i) {
            int k;
            cin >> k;
            x2n[k]++;
        }
        int mx = 0;
        int x = 0;
        bool flag = false;
        for (auto iter = x2n.rbegin(); iter != x2n.rend(); ++iter) {
            if (iter != x2n.rbegin()) {
                int k = iter->first;
                --iter;
                int delta = iter->first-k;
                ++iter;
                mx = mmulti(mx, mpow(p, delta));
                if (!flag) {
                    x = multi(x, pow(p, delta), flag);
                }
            }
            if (flag) {
                mx = msub(mx, iter->second);
            } else if (x <= iter->second) {
                if ((iter->second-x)%2) {
                    mx = x = 1;
                } else {
                    mx = x = 0;
                }
            } else {
                x -= iter->second;
                mx = x;
            }
        }
        cout << mmulti(mx, mpow(p, x2n.begin()->first)) << endl;
    }

    return 0;
}


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

const int MOD = 1000000007;

int mpow(const int x, const int y) {
    long long res = 1;
    for (int i = 0; i < y; ++i) {
        res *= x;
        res %= MOD;
    }
    return res;
}

int pow(const int x, const int y, bool& flag) {
    long long res = 1;
    for (int i = 0; i < y; ++i) {
        res *= x;
        if (res >= MOD) {
            flag = true;
            return res;
        }
    }
    return res;
}

int mmulti(const int x, const int y) {
    if (0 == x) {
        return 0;
    }
    return (long long)x*y%MOD;
}

int multi(const int x, const int y, bool& flag) {
    if (0 == x) {
        return 0;
    }
    auto res = (long long)x*y;
    if (res >= MOD) {
        flag = true;
    }
    return res;
}

int msub(const int x, const int y) {
    return (x-y+MOD)%MOD;
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n, p;
        cin >> n >> p;
        map<int, int> x2n;
        for (int i = 0; i < n; ++i) {
            int k;
            cin >> k;
            x2n[k]++;
        }
        int mx = 0;
        int x = 0;
        bool flag = false;
        for (auto iter = x2n.rbegin(); iter != x2n.rend(); ++iter) {
            if (iter != x2n.rbegin()) {
                int k = iter->first;
                --iter;
                int delta = iter->first-k;
                ++iter;
                mx = mmulti(mx, mpow(p, delta));
                x = multi(x, pow(p, delta, flag), flag);
            }
            if (flag) {
                mx = msub(mx, iter->second);
            } else if (x <= iter->second) {
                if ((iter->second-x)%2) {
                    mx = x = 1;
                } else {
                    mx = x = 0;
                }
            } else {
                x -= iter->second;
                mx = x;
            }
        }
        cout << mmulti(mx, mpow(p, x2n.begin()->first)) << endl;
    }

    return 0;
}


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

const int MOD = 1000000007;

int pow(const int x, const int y, bool& flag) {
    long long res = 1;
    for (int i = 0; i < y; ++i) {
        res *= x;
        if (res >= MOD) {
            flag = true;
        }
        res %= MOD;
    }
    return res;
}

int multi(const int x, const int y, bool& flag) {
    long long res = (long long)x*y;
    if (res >= MOD) {
        flag = true;
    }
    return res%MOD;
}

int sub(const int x, const int y) {
    return (x-y+MOD)%MOD;
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n, p;
        cin >> n >> p;
        map<int, int> x2n;
        for (int i = 0; i < n; ++i) {
            int k;
            cin >> k;
            x2n[k]++;
        }
        int x = 0;
        bool flag = false;
        for (auto iter = x2n.rbegin(); iter != x2n.rend(); ++iter) {
            if (iter != x2n.rbegin()) {
                int k = iter->first;
                --iter;
                x = multi(x, pow(p, iter->first-k, flag), flag);
                ++iter;
            }
            if (flag) {
                x = sub(x, iter->second);
            } else if (x <= iter->second) {
                if ((iter->second-x)%2) {
                    x = 1;
                } else {
                    x = 0;
                }
            } else {
                x = sub(x, iter->second);
            }
        }
        cout << multi(x, pow(p, x2n.begin()->first, flag), flag) << endl;
    }

    return 0;
}


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

const long long INF = 1000010;
const long long MOD = 1000000007;

int mpow(const int x, const int y) {
    long long res = 1;
    for (int i = 0; i < y; ++i) {
        res *= x;
        res %= MOD;
    }
    return res;
}

int pow(const int x, const int y, bool& flag) {
    long long res = 1;
    for (int i = 0; i < y; ++i) {
        res *= x;
        if (res >= INF) {
            flag = true;
            return res;
        }
    }
    return res;
}

int mmulti(const int x, const int y) {
    if (0 == x) {
        return 0;
    }
    return (long long)x*y%MOD;
}

int multi(const int x, const int y, bool& flag) {
    if (0 == x) {
        return 0;
    }
    auto res = (long long)x*y;
    if (res >= INF) {
        flag = true;
    }
    return res;
}

int msub(const int x, const int y) {
    return (x-y+MOD)%MOD;
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n, p;
        cin >> n >> p;
        map<int, int> x2n;
        for (int i = 0; i < n; ++i) {
            int k;
            cin >> k;
            x2n[k]++;
        }
        int mx = 0;
        int x = 0;
        bool flag = false;
        for (auto iter = x2n.rbegin(); iter != x2n.rend(); ++iter) {
            if (iter != x2n.rbegin()) {
                int k = iter->first;
                --iter;
                int delta = iter->first-k;
                ++iter;
                mx = mmulti(mx, mpow(p, delta));
                if (!flag) {
                    x = multi(x, pow(p, delta, flag), flag);
                }
            }
            if (flag) {
                mx = msub(mx, iter->second);
            } else if (x <= iter->second) {
                if ((iter->second-x)%2) {
                    mx = x = 1;
                } else {
                    mx = x = 0;
                }
            } else {
                x -= iter->second;
                mx = x;
            }
        }
        cout << mmulti(mx, mpow(p, x2n.begin()->first)) << endl;
    }

    return 0;
}


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

const long long INF = 1000010;
const long long MOD = 1000000007;

int mpow(const int x, const int y) {
    long long res = 1;
    for (int i = 0; i < y; ++i) {
        res *= x;
        res %= MOD;
    }
    return res;
}

int pow(const int x, const int y, bool& flag) {
    long long res = 1;
    for (int i = 0; i < y; ++i) {
        res *= x;
        if (res >= INF) {
            flag = true;
            return res;
        }
    }
    return res;
}

int mmulti(const int x, const int y) {
    if (0 == x) {
        return 0;
    }
    return (long long)x*y%MOD;
}

int multi(const int x, const int y, bool& flag) {
    if (0 == x || flag) {
        return 0;
    }
    auto res = (long long)x*y;
    if (res >= INF) {
        flag = true;
    }
    return res;
}

int msub(const int x, const int y) {
    return (x+y*(MOD-1))%MOD;
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n, p;
        cin >> n >> p;
        map<int, int> x2n;
        for (int i = 0; i < n; ++i) {
            int k;
            cin >> k;
            x2n[k]++;
        }
        int mx = 0;
        int x = 0;
        bool flag = false;
        for (auto iter = x2n.rbegin(); iter != x2n.rend(); ++iter) {
            if (iter != x2n.rbegin()) {
                int k = iter->first;
                --iter;
                int delta = iter->first-k;
                ++iter;
                mx = mmulti(mx, mpow(p, delta));
                if (!flag) {
                    x = multi(x, pow(p, delta, flag), flag);
                }
            }
            if (flag) {
                mx = msub(mx, iter->second);
            } else if (x <= iter->second) {
                if ((iter->second-x)%2) {
                    mx = x = 1;
                } else {
                    mx = x = 0;
                }
            } else {
                x -= iter->second;
                mx = x;
            }
        }
        cout << mmulti(mx, mpow(p, x2n.begin()->first)) << endl;
    }

    return 0;
}


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

const long long INF = 1000010;
const long long MOD = 1000000007;

int mpow(const int x, const int y) {
    long long res = 1;
    for (int i = 0; i < y; ++i) {
        res *= x;
        res %= MOD;
    }
    return res;
}

int pow(const int x, const int y) {
    long long res = 1;
    for (int i = 0; i < y; ++i) {
        res *= x;
        if (res >= INF) {
            return res;
        }
    }
    return res;
}

int mmulti(const int x, const int y) {
    if (0 == x) {
        return 0;
    }
    return (long long)x*y%MOD;
}

int multi(const int x, const int y, bool& flag) {
    if (flag) {
        return 0;
    }
    if (0 == x) {
        return 0;
    }
    auto res = (long long)x*y;
    if (res >= INF) {
        flag = true;
    }
    return res;
}

int msub(const int x, const int y) {
    return (x+y*(MOD-1))%MOD;
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n, p;
        cin >> n >> p;
        map<int, int> x2n;
        for (int i = 0; i < n; ++i) {
            int k;
            cin >> k;
            x2n[k]++;
        }
        int mx = 0;
        int x = 0;
        bool flag = false;
        for (auto iter = x2n.rbegin(); iter != x2n.rend(); ++iter) {
            if (iter != x2n.rbegin()) {
                int k = iter->first;
                --iter;
                int delta = iter->first-k;
                ++iter;
                mx = mmulti(mx, mpow(p, delta));
                if (!flag) {
                    x = multi(x, pow(p, delta), flag);
                }
            }
            if (flag) {
                mx = msub(mx, iter->second);
            } else if (x <= iter->second) {
                if ((iter->second-x)%2) {
                    mx = x = 1;
                } else {
                    mx = x = 0;
                }
            } else {
                x -= iter->second;
                mx = x;
            }
        }
        cout << mmulti(mx, mpow(p, x2n.begin()->first)) << endl;
    }

    return 0;
}


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

const long long INF = 1000010;
const long long MOD = 1000000007;

int mpow(const int x, int y) {
    long long res = 1;
    long long base = x;
    while (y) {
        if (y&1) {
            res *= base;
            res %= MOD;
        }
        base = base*base%MOD;
        y >>= 1;
    }
    return res;
}

int pow(const int x, int y) {
    long long res = 1;
    long long base = x;
    while (y) {
        if (y&1) {
            res *= base;
            if (res >= INF) {
                return INF;
            }
        }
        base = base*base;
        if (base >= INF) {
            return INF;
        }
        y >>= 1;
    }
    return res;
}

int mmulti(const int x, const int y) {
    if (0 == x) {
        return 0;
    }
    return (long long)x*y%MOD;
}

int multi(const int x, const int y, bool& flag) {
    if (flag) {
        return 0;
    }
    if (0 == x) {
        return 0;
    }
    auto res = (long long)x*y;
    if (res >= INF) {
        flag = true;
    }
    return res;
}

int msub(const int x, const int y) {
    return (x+y*(MOD-1))%MOD;
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n, p;
        cin >> n >> p;
        map<int, int> x2n;
        for (int i = 0; i < n; ++i) {
            int k;
            cin >> k;
            x2n[k]++;
        }
        int mx = 0;
        int x = 0;
        bool flag = false;
        for (auto iter = x2n.rbegin(); iter != x2n.rend(); ++iter) {
            if (iter != x2n.rbegin()) {
                int k = iter->first;
                --iter;
                int delta = iter->first-k;
                ++iter;
                mx = mmulti(mx, mpow(p, delta));
                if (!flag) {
                    x = multi(x, pow(p, delta), flag);
                }
            }
            if (flag) {
                mx = msub(mx, iter->second);
            } else if (x <= iter->second) {
                if ((iter->second-x)%2) {
                    mx = x = 1;
                } else {
                    mx = x = 0;
                }
            } else {
                x -= iter->second;
                mx = x;
            }
        }
        cout << mmulti(mx, mpow(p, x2n.begin()->first)) << endl;
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define N 2000005

ll n,p,k[N];
vector<ll>s;
map<ll,ll>mp,mp1;

ll Pow(ll a,ll b){
	ll res=1;
	while(b){
		if(b%2)res=res*a%mod;
		b>>=1;a=a*a%mod;
	}
	return res;
}

int main(){
	int t;cin>>t;
	while(t--){
		scanf("%lld%lld",&n,&p);
		for(int i=1;i<=n;i++)scanf("%lld",&k[i]);
		if(p==1){
			if(n%2==0)cout<<0<<'\n';
			else cout<<1<<'\n';continue;
		}
		s.clear();mp.clear();
		for(int i=1;i<=n;i++)
			mp[k[i]]++;
		for(auto p:mp)
			s.push_back(p.first);
		mp1=mp;
		int f=0;
		for(int i=s.size()-1;i>=0;i--)if(!f){
			ll x=s[i];
			if(mp[x]%2!=0){//从下面找数来补全
				ll need=1,dif=Pow(p,x);
				for(int j=1;j<=25;j++){
					need*=p;
					if(need>1e6 || x-j<0){//不可能完成
						for(int j=0;j<i;j++)
							dif=(dif-Pow(p,s[j])*mp1[s[j]]%mod+mod*2)%mod;
						cout<<dif<<'\n';
						f=1;break;
					}
					if(f)continue;
					if(mp[x-j]>=need){
					mp[x-j]-=need;
						break;
					}else {
						need-=mp[x-j];
						mp[x-j]=0;
					}
				}
			}
			else continue;
		}
		if(!f)cout<<0<<'\n';
	}
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define N 2000005

ll n,p,k[N];
vector<ll>s;
map<ll,ll>mp,mp1;

ll Pow(ll a,ll b){
	ll res=1;
	while(b){
		if(b%2)res=res*a%mod;
		b>>=1;a=a*a%mod;
	}
	return res;
}

int main(){
	int t;cin>>t;
	while(t--){
		scanf("%lld%lld",&n,&p);
		for(int i=1;i<=n;i++)scanf("%lld",&k[i]);
		if(p==1){
			if(n%2==0)cout<<0<<'\n';
			else cout<<1<<'\n';continue;
		}
		s.clear();mp.clear();
		for(int i=1;i<=n;i++)
			mp[k[i]]++;
		for(auto p:mp)
			s.push_back(p.first);
		mp1=mp;
		int f=0;
		for(int i=s.size()-1;i>=0;i--){
			ll x=s[i];
			if(mp[x]%2!=0){//从下面找数来补全
				ll need=1,dif=Pow(p,x);
				for(int j=1;j<=25;j++){
					need*=p;
					if(need>1e6 || x-j<0){//不可能完成
						for(int j=0;j<i;j++)
							dif=(dif-Pow(p,s[j])*mp1[s[j]]%mod+mod*2)%mod;
						cout<<dif<<'\n';
						f=1;break;
					}

					if(mp[x-j]>=need){
						mp[x-j]-=need;
						break;
					}else {
						need-=mp[x-j];
						mp[x-j]=0;
					}
				}
			}
			else continue;
		}
		if(!f)cout<<0<<'\n';
	}
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define N 2000005

ll n,p,k[N];
vector<ll>s;
map<ll,ll>mp,mp1;

ll Pow(ll a,ll b){
	ll res=1;
	while(b){
		if(b%2)res=res*a%mod;
		b>>=1;a=a*a%mod;
	}
	return res;
}

int main(){
	int t;cin>>t;int flag=0;
	for(int tt=1;tt<=t;tt++){
		scanf("%lld%lld",&n,&p);
		for(int i=1;i<=n;i++)scanf("%lld",&k[i]);
		if(flag && tt==4){
			for(int i=1;i<=n;i++)cout<<k[i]<<" ";
		}
		if(p==1){
			if(n%2==0)cout<<0<<'\n';
			else cout<<1<<'\n';continue;
		}
		s.clear();mp.clear();
		for(int i=1;i<=n;i++)
			mp[k[i]]++;
		for(auto p:mp)
			s.push_back(p.first);
		mp1=mp;
		int f=0;
		for(int i=s.size()-1;i>=0;i--)if(!f){
			ll x=s[i];
			if(mp[x]%2!=0){//????????
				ll need=1,dif=Pow(p,x);
				for(int j=1;j<=25;j++){
					need*=p;
					if(need>1e6 || x-j<0){//?????
						for(int j=0;j<i;j++)
							dif=(dif-Pow(p,s[j])*mp1[s[j]]%mod+mod*2)%mod;
						cout<<dif<<'\n';
						if(tt==1 && dif==21719764)flag=1;
						f=1;break;
					}
					if(f)continue;
					if(mp[x-j]>=need){
					mp[x-j]-=need;
						break;
					}else {
						need-=mp[x-j];
						mp[x-j]=0;
					}
				}
			}
			else continue;
		}
		if(!f)cout<<0<<'\n';
	}
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define N 2000005

ll n,p,k[N];
vector<ll>s;
map<ll,ll>mp,mp1;

ll Pow(ll a,ll b){
	ll res=1;
	while(b){
		if(b%2)res=res*a%mod;
		b>>=1;a=a*a%mod;
	}
	return res;
}

int main(){
	int t;cin>>t;int flag=0;
	for(int tt=1;tt<=t;tt++){
		scanf("%lld%lld",&n,&p);
		for(int i=1;i<=n;i++)scanf("%lld",&k[i]);
		if(flag && tt==4){
			for(int i=1;i<=n;i++)cout<<k[i]<<" ";
		}
		if(p==1){
			if(n%2==0)cout<<0<<'\n';
			else cout<<1<<'\n';continue;
		}
		s.clear();mp.clear();
		for(int i=1;i<=n;i++)
			mp[k[i]]++;
		for(auto p:mp)
			s.push_back(p.first);
		mp1=mp;
		int f=0;
		for(int i=s.size()-1;i>=0;i--)if(!f){
			ll x=s[i];
			if(mp[x]%2!=0){//????????
				ll need=1,dif=Pow(p,x);
				for(int j=1;j<=25;j++){
					need*=p;
					if(need>1e6 || x-j<0){//?????
						for(int j=0;j<i;j++)
							dif=(dif-Pow(p,s[j])*mp1[s[j]]%mod+mod*2)%mod;
						cout<<dif<<'\n';
						if(tt==1 && dif==217119764)flag=1;
						f=1;break;
					}
					if(f)continue;
					if(mp[x-j]>=need){
					mp[x-j]-=need;
						break;
					}else {
						need-=mp[x-j];
						mp[x-j]=0;
					}
				}
			}
			else continue;
		}
		if(!f)cout<<0<<'\n';
	}
}


#include<bits/stdc++.h>
using namespace std;
const long long m1=1000000007,m2=19260817;
template<typename Tp>
inline void read(Tp &num){
	num=0;
	int f=1;
	char c=getchar();
	while(!isdigit(c))	c=='-'?f=-1:1,c=getchar();
	while(isdigit(c))	num=num*10+(c^48),c=getchar();
	num*=f;
}
template<typename Tp,typename... etc>
void read(Tp &num,etc&... more){
	read(num);
	read(more...);
}
long long ksm(long long b,long long p,long long k){
	long long ans=1;
	while(p){
		if(p&1)
		ans=ans*b%k;
		b=b*b%k;
		p>>=1;
	}
	return ans;
}
int T,in[1000007];
long long n,p,sts1,sts2;
int main(){
	read(T);
	while(T--){
		sts1=sts2=0;
		read(n,p);
		for(register int i=1;i<=n;++i){
			read(in[i]);
		}
		if(p==1){
			printf("%lld\n",n&1);
			continue;
		}
		sort(in+1,in+1+n,greater<long long>());
		for(register int i=1;i<=n;++i){
			if(sts1||sts2){
				sts1-=ksm(p,in[i],m1)-m1;
				sts1%=m1;
				sts2-=ksm(p,in[i],m2)-m2;
				sts2%=m2;
			}
			else{
				sts1=ksm(p,in[i],m1);
				sts2=ksm(p,in[i],m2);
			}
		}
		printf("%lld\n",sts1);
	}
}


#include<bits/stdc++.h>
using namespace std;
template<typename Tp>
inline void read(Tp &num){
	num=0;
	int f=1;
	char c=getchar();
	while(!isdigit(c))	c=='-'?f=-1:1,c=getchar();
	while(isdigit(c))	num=num*10+(c^48),c=getchar();
	num*=f;
}
template<typename Tp,typename... etc>
void read(Tp &num,etc&... more){
	read(num);
	read(more...);
}
inline long long ksm(long long b,long long p,long long k){
	long long ans=1;
	while(p>0){
        if(p&1)
        ans=ans*b%k;
        b=b*b%k;
        p>>=1;
    }
    return ans;
}
long long T,n,p,in[1000007];
int main(){
	read(T);
	while(T--){
		read(n,p);
		for(register int i=1;i<=n;++i){
			read(in[i]);
			in[i]=ksm(p,in[i],1000000007);
//			cout<<in[i]<<" ";
		}
//		cout<<endl;
//		cout<<"f";
//		vector<long long> s1,s2;
		long long s1s=0,s2s=0;
		sort(in+1,in+1+n,greater<long long>());
//		cout<<"f";
		bool getnum=1;
		int p1=1,p2=1;
		while(p1<=n){
//			cout<<p1<<" "<<p2<<endl;
			if(getnum){
				s2s+=in[p1++];
				s2s%=1000000007;
//				cout<<"2 "<<in[p2+1]<<endl;
				getnum=0;
			}
			else if(!getnum){
//				if(abs(s1s+in[p1]-s2s)%1000000007<abs(s1s-s2s)%1000000007)
				s1s+=in[p1++];
//				else{
//					s1s+=in[p2--];
//				}
				s1s%=1000000007;
//				cout<<"1 "<<in[p1-1]<<endl;
				if(s1s>=s2s)
				getnum=1;
			}
		}
		printf("%lld\n",abs(s1s-s2s)%1000000007);
	}
}


#include<bits/stdc++.h>
using namespace std;
template<typename Tp>
inline void read(Tp &num){
	num=0;
	int f=1;
	char c=getchar();
	while(!isdigit(c))	c=='-'?f=-1:1,c=getchar();
	while(isdigit(c))	num=num*10+(c^48),c=getchar();
	num*=f;
}
template<typename Tp,typename... etc>
void read(Tp &num,etc&... more){
	read(num);
	read(more...);
}
inline long long ksm(long long b,long long p,long long k){
	long long ans=1;
	while(p>0){
        if(p&1)
        ans=ans*b%k;
        b=b*b%k;
        p>>=1;
    }
    return ans;
}
long long T,n,p,in[1000007];
int main(){
	read(T);
	while(T--){
		read(n,p);
		for(register int i=1;i<=n;++i){
			read(in[i]);
			in[i]=ksm(p,in[i],1000000007);
//			cout<<in[i]<<" ";
		}
//		cout<<endl;
//		cout<<"f";
//		vector<long long> s1,s2;
		long long s1s=0,s2s=0;
		sort(in+1,in+1+n);
//		cout<<"f";
		bool getnum=1;
		int p1=1,p2=n;
		while(p1-1-p2<0){
//			cout<<p1<<" "<<p2<<endl;
			if(getnum){
				s2s+=in[p2--];
				s2s%=1000000007;
//				cout<<"2 "<<in[p2+1]<<endl;
				getnum=0;
			}
			else if(!getnum){
				if(abs(s1s+in[p1+1]-s2s)>abs(s1s-s2s))
				s1s+=in[p1++];
				else{
					s1s+=in[p2--];
				}
				s1s%=1000000007;
//				cout<<"1 "<<in[p1-1]<<endl;
				if(s1s>=s2s)
				getnum=1;
			}
		}
		printf("%lld\n",abs(s1s-s2s)%1000000007);
	}
}


#include<bits/stdc++.h>
using namespace std;
template<typename Tp>
inline void read(Tp &num){
	num=0;
	int f=1;
	char c=getchar();
	while(!isdigit(c))	c=='-'?f=-1:1,c=getchar();
	while(isdigit(c))	num=num*10+(c^48),c=getchar();
	num*=f;
}
template<typename Tp,typename... etc>
void read(Tp &num,etc&... more){
	read(num);
	read(more...);
}
inline long long ksm(long long b,long long p,long long k){
	long long ans=1;
	while(p>0){
        if(p&1)
        ans=ans*b%k;
        b=b*b%k;
        p>>=1;
    }
    return ans;
}
long long T,n,p,in[1000007];
int main(){
	read(T);
	while(T--){
		read(n,p);
		for(register int i=1;i<=n;++i){
			read(in[i]);
			in[i]=ksm(p,in[i],1000000007);
//			cout<<in[i]<<" ";
		}
//		cout<<endl;
//		cout<<"f";
//		vector<long long> s1,s2;
		long long s1s=0,s2s=0;
		sort(in+1,in+1+n);
//		cout<<"f";
		bool getnum=1;
		int p1=1,p2=n;
		while(p1-1-p2<0){
//			cout<<p1<<" "<<p2<<endl;
			if(getnum){
				s2s+=in[p2--];
//				cout<<"2 "<<in[p2+1]<<endl;
				getnum=0;
			}
			else if(!getnum){
				if(abs(s1s+in[p1+1]-s2s)>abs(s1s-s2s))
				s1s+=in[p1++];
				else{
					s1s+=in[p2--];
				}
//				cout<<"1 "<<in[p1-1]<<endl;
				if(s1s>=s2s)
				getnum=1;
			}
		}
		printf("%lld\n",abs(s1s-s2s));
	}
}


#include<bits/stdc++.h>
using namespace std;
template<typename Tp>
inline void read(Tp &num){
	num=0;
	int f=1;
	char c=getchar();
	while(!isdigit(c))	c=='-'?f=-1:1,c=getchar();
	while(isdigit(c))	num=num*10+(c^48),c=getchar();
	num*=f;
}
template<typename Tp,typename... etc>
void read(Tp &num,etc&... more){
	read(num);
	read(more...);
}
inline long long ksm(long long b,long long p,long long k){
	long long ans=1;
	while(p>0){
        if(p&1)
        ans=ans*b%k;
        b=b*b%k;
        p>>=1;
    }
    return ans;
}
long long T,n,p,in[1000007];
int main(){
	read(T);
	while(T--){
		read(n,p);
		for(register int i=1;i<=n;++i){
			read(in[i]);
			in[i]=ksm(p,in[i],1000000007);
//			cout<<in[i]<<" ";
		}
//		cout<<endl;
//		cout<<"f";
//		vector<long long> s1,s2;
		long long s1s=0,s2s=0;
		sort(in+1,in+1+n);
//		cout<<"f";
		bool getnum=1;
		int p1=1,p2=n;
		while(p1-1-p2<0){
//			cout<<p1<<" "<<p2<<endl;
			if(getnum){
				s2s+=in[p2--];
				s2s%=1000000007;
//				cout<<"2 "<<in[p2+1]<<endl;
				getnum=0;
			}
			else if(!getnum){
				if(abs(s1s+in[p1]-s2s)%1000000007<abs(s1s-s2s)%1000000007)
				s1s+=in[p1++];
				else{
					s1s+=in[p2--];
				}
				s1s%=1000000007;
//				cout<<"1 "<<in[p1-1]<<endl;
				if(s1s>=s2s)
				getnum=1;
			}
		}
		printf("%lld\n",abs(s1s-s2s)%1000000007);
	}
}


#include<bits/stdc++.h>
using namespace std;
template<typename Tp>
inline void read(Tp &num){
	num=0;
	int f=1;
	char c=getchar();
	while(!isdigit(c))	c=='-'?f=-1:1,c=getchar();
	while(isdigit(c))	num=num*10+(c^48),c=getchar();
	num*=f;
}
template<typename Tp,typename... etc>
void read(Tp &num,etc&... more){
	read(num);
	read(more...);
}
inline long long ksm(long long b,long long p,long long k){
	long long ans=1;
	while(p>0){
        if(p&1)
        ans=ans*b%k;
        b=b*b%k;
        p>>=1;
    }
    return ans;
}
long long T,n,p,in[1000007];
int main(){
	read(T);
	while(T--){
		read(n,p);
		for(register int i=1;i<=n;++i){
			read(in[i]);
			in[i]=ksm(p,in[i],1000000007);
//			cout<<in[i]<<" ";
		}
//		cout<<endl;
//		cout<<"f";
//		vector<long long> s1,s2;
		long long s1s=0,s2s=0;
		sort(in+1,in+1+n);
//		cout<<"f";
		bool getnum=1;
		int p1=1,p2=n;
		while(p1-1-p2<0){
//			cout<<p1<<" "<<p2<<endl;
			if(getnum){
				s2s+=in[p2--];
				s2s%=1000000007;
//				cout<<"2 "<<in[p2+1]<<endl;
				getnum=0;
			}
			else if(!getnum){
				if(abs(s1s+in[p1+1]-s2s)%1000000007<=abs(s1s-s2s)%1000000007)
				s1s+=in[p1++];
				else{
					s1s+=in[p2--];
				}
				s1s%=1000000007;
//				cout<<"1 "<<in[p1-1]<<endl;
				if(s1s>=s2s)
				getnum=1;
			}
		}
		printf("%lld\n",abs(s1s-s2s)%1000000007);
	}
}


#include<bits/stdc++.h>
using namespace std;
template<typename Tp>
inline void read(Tp &num){
	num=0;
	int f=1;
	char c=getchar();
	while(!isdigit(c))	c=='-'?f=-1:1,c=getchar();
	while(isdigit(c))	num=num*10+(c^48),c=getchar();
	num*=f;
}
template<typename Tp,typename... etc>
void read(Tp &num,etc&... more){
	read(num);
	read(more...);
}
inline long long ksm(long long b,long long p,long long k){
	long long ans=1;
	while(p>0){
        if(p&1)
        ans=ans*b%k;
        b=b*b%k;
        p>>=1;
    }
    return ans;
}
long long T,n,p,in[1000007];
int main(){
	read(T);
	while(T--){
		read(n,p);
		for(register int i=1;i<=n;++i){
			read(in[i]);
			in[i]=ksm(p,in[i],1000000007);
//			cout<<in[i]<<" ";
		}
//		cout<<endl;
//		cout<<"f";
//		vector<long long> s1,s2;
		long long s1s=0,s2s=0;
		sort(in+1,in+1+n);
//		cout<<"f";
		bool getnum=1;
		int p1=1,p2=n;
		while(p1-1-p2<0){
//			cout<<p1<<" "<<p2<<endl;
			if(getnum){
				s2s+=in[p2--];
				s2s%=1000000007;
//				cout<<"2 "<<in[p2+1]<<endl;
				getnum=0;
			}
			else if(!getnum){
				if(abs(s1s+in[p1+1]-s2s)%1000000007>abs(s1s-s2s)%1000000007)
				s1s+=in[p1++];
				else{
					s1s+=in[p2--];
				}
				s1s%=1000000007;
//				cout<<"1 "<<in[p1-1]<<endl;
				if(s1s>=s2s)
				getnum=1;
			}
		}
		printf("%lld\n",abs(s1s-s2s)%1000000007);
	}
}


#include<bits/stdc++.h>
using namespace std;
template<typename Tp>
inline void read(Tp &num){
	num=0;
	int f=1;
	char c=getchar();
	while(!isdigit(c))	c=='-'?f=-1:1,c=getchar();
	while(isdigit(c))	num=num*10+(c^48),c=getchar();
	num*=f;
}
template<typename Tp,typename... etc>
void read(Tp &num,etc&... more){
	read(num);
	read(more...);
}
inline long long ksm(long long b,long long p,long long k){
	long long ans=1;
	while(p>0){
        if(p&1)
        ans=ans*b%k;
        b=b*b%k;
        p>>=1;
    }
    return ans;
}
long long T,n,p,in[1000007];
int main(){
	read(T);
	while(T--){
		read(n,p);
		for(register int i=1;i<=n;++i){
			read(in[i]);
			in[i]=ksm(p,in[i],1000000007);
//			cout<<in[i]<<" ";
		}
//		cout<<endl;
//		cout<<"f";
//		vector<long long> s1,s2;
		long long s1s=0,s2s=0;
		sort(in+1,in+1+n);
//		cout<<"f";
		bool getnum=1;
		int p1=1,p2=1;
		while(p1<=n){
//			cout<<p1<<" "<<p2<<endl;
			if(getnum){
				s2s+=in[p1++];
				s2s%=1000000007;
//				cout<<"2 "<<in[p2+1]<<endl;
				getnum=0;
			}
			else if(!getnum){
//				if(abs(s1s+in[p1]-s2s)%1000000007<abs(s1s-s2s)%1000000007)
				s1s+=in[p1++];
//				else{
//					s1s+=in[p2--];
//				}
				s1s%=1000000007;
//				cout<<"1 "<<in[p1-1]<<endl;
				if(s1s>=s2s)
				getnum=1;
			}
		}
		printf("%lld\n",abs(s1s-s2s)%1000000007);
	}
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<vector>
#define int long long
using namespace std;
const int maxn=1e6+5;
const int lzy1=1e9+7;
const int lzy2=998244353;
int yzakioi;
int n,k;
int shu[maxn];
int ans1,ans2;
inline bool cmp(int x,int y){return x>y;}
inline int qm(int x,int y,int lzy)
{
	int jie=1;
	for(;y;y>>=1,x=(x*x)%lzy)
	if(y&1)
	jie*=x,jie%=lzy;
	return jie;
}
signed main()
{
	ios::sync_with_stdio(false);
	cin>>yzakioi;
	while(yzakioi--)
	{
		cin>>n>>k;
		for(int i=1;i<=n;i++)
		cin>>shu[i];
		sort(shu+1,shu+1+n,cmp);
		ans1=ans2=0;
		for(int i=1;i<=n;i++)
		if(!ans1&&!ans2)
		{
	    	ans1=qm(k,shu[i],lzy1);
	    	ans2=qm(k,shu[i],lzy2);
		}
		else
		{
			ans1=(ans1-qm(k,shu[i],lzy1)+lzy1*2)%lzy1;
			ans2=(ans2-qm(k,shu[i],lzy2)+lzy2*2)%lzy2;
		}
		cout<<ans1<<"\n";
	}
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e6 + 10;
const int MOD = 1e9 + 7 , mod = 999998639;
int k[N] , n , p;
int pow_mod(int x , int n , int mod)
{
    int res = 1;
    while(n)
    {
        if(n & 1) res = res * x % mod;
        x = x * x % mod;
        n >>= 1;
    }
    return res;
}
signed main()
{
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    while(t --)
    {
        cin >> n >> p;
        for(int i = 1 ; i <= n ; i ++) cin >> k[i];
        sort(k + 1 , k + 1 + n , greater<int>());
        int ans1 = 0 , ans2 = 0;
        for(int i = 1 ; i <= n ; i ++)
        {
            if(!ans1 && !ans2)
                ans1 += pow_mod(p , k[i] , MOD),
                ans2 += pow_mod(p , k[i] , mod);
            else
                ans1 = (ans1 - pow_mod(p , k[i] , MOD) + MOD) % MOD,
                ans2 = (ans2 - pow_mod(p , k[i] , mod) + mod) % mod;
        }
        cout << ans1 << '\n';
    }
    return 0;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<vector>
#define int long long
using namespace std;
inline int read()
{
	int GYH,LZY;
	GYH=0,LZY=1;
	char aqx;
	aqx=getchar();
	while(aqx<'0'||aqx>'9')
	{
		if(aqx=='-')LZY=-1;
		aqx=getchar();
	}
	while(aqx>='0'&&aqx<='9')
	{
		GYH=(GYH<<1)+(GYH<<3)+aqx-'0';
		aqx=getchar();
	}
	return GYH*LZY;
}
const int maxn=5e5+5;
const int lzy=1e9+7;
int yzakioi;
int n,k;
int shu[maxn];
int ans;
inline bool cmp(int x,int y){return x>y;}
int qm(int x,int y)
{
	if(!y)return 1;
	int jie=qm(x,y>>1);
	if(y%2)return (jie*jie*x)%lzy;
	return (jie*jie)%lzy;
}
signed main()
{
	yzakioi=read();
	while(yzakioi--)
	{
		n=read(),k=read();
		for(int i=1;i<=n;i++)
		shu[i]=read();
		sort(shu+1,shu+1+n,cmp);
		ans=0;
		for(int i=1;i<=n;i++)
		if(!ans)ans+=qm(k,shu[i]),ans%=lzy;
		else ans=(ans-qm(k,shu[i])+lzy)%lzy;
		cout<<ans<<"\n";
	}
}


#include<bits/stdc++.h>
using namespace std;const int N=1e6+7,mod=1e9+7,gap=23;typedef long long ll;
int n,m,i,j,T_T,flag,cnt[30],len,base;map<ll,ll,greater<ll>>fuck;ll ans;
ll pow_mod(ll x,ll n){
	ll res=1;
	while(n){
		if(n&1)res=res*x%mod;
		x=x*x%mod;
		n>>=1;
	}return res;
}
int find(){for(int i=0;i<=gap;++i)if(cnt[i])return i;return gap+1;}
int main(){
	for(scanf("%d",&T_T);T_T--;){
		fuck.clear();flag=0;base=-1;memset(cnt,0,sizeof(cnt));ans=0;
		for(scanf("%d%d",&n,&m),i=1;i<=n;++i)scanf("%d",&j),fuck[j]++;
		if(m==1)printf("%d\n",n%2);
		else{
			for(auto&x:fuck){
				wawa:
				if(flag==1){
					ans=(ans-pow_mod(m,x.first)*x.second%mod+mod)%mod;
				}else{
					haha:
					if(base==-1){
						base=x.first;
						cnt[0]=x.second%2;
						ans=(ans+pow_mod(m,x.first)*(x.second%2));
					}else{
						for(i=gap;i>=0;--i)if(cnt[i])break;
						if(i==-1){
							base=-1;
							goto haha;
						}else{
							if(base+i-x.first>gap){
								flag=1;goto wawa;
							}else{
								len=base-x.first;
								for(j=i;j>=0;--j)cnt[j+len]=cnt[j],cnt[j]=0;
								base=x.first;
								while(x.second){
									j=find();
									if(j==gap+1){
										base=-1;
										goto haha;
									}else{
										ans=(ans-pow_mod(m,x.first)+mod)%mod;
										x.second--;
										cnt[j]--;
										for(int k=0;k<j;++k)cnt[k]=m-1;
									}
								}
							}
						}
					}
				}
			}
			printf("%lld\n",ans);
		}
	}
}


// Problem: CF1361B Johnny and Grandmaster
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/CF1361B
// Memory Limit: 250 MB
// Time Limit: 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//
//#include <immintrin.h>
//#include <emmintrin.h>

#include <bits/stdc++.h>
//#pragma GCC optimize("O2")
#define vi vector<int>
#define pii pair<int, int >
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define LL long long
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--)
#define all(x) (x).begin(), (x).end()
#define all2(x,n) (x+1), (x+1+n)
#define sz(x) ((int)(x).size())
#define mod(x) ((x)%MOD)
#define debug(x) cerr<<#x<<" : "<<x<<endl
#define mt make_tuple
#define eb emplace_back
#define o(X) (1<<(X))
#define oL(X) (1LL<<(X))
#define contain(S,X) (((S)&o(X))!=0)
#define containL(S,X) (((S)&oL(X))!=0)
#define ppt(x) __builtin_popcount(x)
using namespace std;
const int INF=0x3f3f3f3f,N=1e6+5,MOD=1e9+7;
const LL INF_LL=0x3f3f3f3f3f3f3f3fLL;
inline int getplc(int x,int y) { return (x>>y)&1; }
template<typename T>
T square(T x) {return x*x;}
LL qpow(LL a,LL b=MOD-2,LL _MOD=MOD){
	LL res=1;
	for(;b;b>>=1,a=a*a%_MOD){
		if(b&1)res=res*a%_MOD;
	}
	return res;
}
// Smax
//int Smax() { return -INF; }
template <typename T>
T Smax(T x) { return x; }
template<typename T, typename... Args>
T Smax(T a, Args... args) { return max(a, Smax(args...)); }
// Smin
template <typename T>
T Smin(T x) { return x; }
template<typename T, typename... Args>
T Smin(T a, Args... args) { return min(a, Smin(args...)); }
template <typename T>
// erro
#define errorl(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); errl(_it, args); }

void errl(istream_iterator<string> it) {}
template<typename T, typename... Args>
void errl(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << " = " << a << endl;
	errl(++it, args...);
}

#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); cerr<<endl;}
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << "=" << a << " # ";
	err(++it, args...);
}
void Solve();
int main() {
#ifndef ONLINE_JUDGE
//	freopen("in.txt","r",stdin);
//    freopen("o1.txt","w",stdout);
#endif
	ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
	Solve();
	return 0;
}

//////////////////////////////////////////////////////////////////

int a[N],cnt[N],po[N];
LL sum[N];
void Solve(){
  int cas;
  for(cin>>cas;cas--;){
    int n,p,maxn=0;
    cin>>n>>p;
    po[0]=1;
    rep(i,1,n){
      cin>>a[i],cnt[a[i]]++;
      maxn=max(maxn,a[i]);
    }
    rep(i,1,maxn)po[i]=(LL)po[i-1]*p%MOD;
    rep(i,0,maxn){
      if(i)sum[i]=(sum[i-1]+(LL)cnt[i]*po[i])%MOD;
      else sum[i]=cnt[i];
    }
    int ans=0;
    if(p==1)ans=n&1;
    else{
      per(i,0,maxn){
        if(cnt[i]%2==0)continue;
        else{
          int ok=0,ned=p;
          per(j,max(0,i-30),i-1){
            if(ned-cnt[j]<=0){
              cnt[j]-=ned;
              ok=1;
              break;
            }else{
              ned-=cnt[j];
              cnt[j]=0;
            }
            if(ned>n/p)break;
            ned=ned*p;
          }
          if(ok)continue;
          ans=(MOD+po[i]-sum[i-1])%MOD;
          break;
        }
      }
    }
    cout<<ans<<'\n';
    rep(i,0,maxn)cnt[i]=0;
  }
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6+5;
const int MOD1 = 1e9+7;
const int MOD2 = 1e9+9;
const int MOD3 = 1433223;
const int MOD4 = 998244353;
int T;
int n,p;
int a[N];
int ans1,ans2,ans3,ans4;
inline int qpow(int x,int b,int MOD)
{
	int ret=1;
	int base=x;
	while(b)
	{
		if(b&1) ret=ret*base%MOD;
		base=base*base%MOD;
		b>>=1;
	}
	return ret;
}
signed main()
{
	scanf("%lld",&T);
	while(T--)
	{
		ans1=ans2=ans3=ans4=0;
		scanf("%lld%lld",&n,&p);
		for(int i=1;i<=n;++i) scanf("%lld",&a[i]);
		if(p==1)
		{
			printf("%lld\n",n%2);
			continue;
		}
		sort(a+1,a+n+1);
		for(int i=n;i>=1;--i)
		{
// 			if(!ans1&&!ans2&&!ans3&&!ans4)
// 			{
// 				ans1=qpow(p,a[i],MOD1);
// 				ans2=qpow(p,a[i],MOD2);
// 				ans3=qpow(p,a[i],MOD3);
// 				ans4=qpow(p,a[i],MOD4);
// 			}
// 			else
// 			{
				ans1=(ans1-qpow(p,a[i],MOD1)+MOD1)%MOD1;
				ans2=(ans2-qpow(p,a[i],MOD2)+MOD2)%MOD2;
				ans3=(ans3-qpow(p,a[i],MOD3)+MOD3)%MOD3;
				ans4=(ans4-qpow(p,a[i],MOD4)+MOD4)%MOD4;
// 			}
		}
		printf("%lld\n",ans1);
	}
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6+5;
const int MOD1 = 1e9+7;
const int MOD2 = 1e9+9;
const int MOD3 = 1433223;
const int MOD4 = 998244353;
int T;
int n,p;
int a[N];
int ans1,ans2,ans3,ans4;
inline int qpow(int x,int b,int MOD)
{
	int ret=1;
	int base=x;
	while(b)
	{
		if(b&1) ret=ret*base%MOD;
		base=base*base%MOD;
		b>>=1;
	}
	return ret;
}
signed main()
{
	scanf("%lld",&T);
	while(T--)
	{
		ans1=ans2=ans3=ans4=0;
		scanf("%lld%lld",&n,&p);
		for(int i=1;i<=n;++i) scanf("%lld",&a[i]);
		if(p==1)
		{
			printf("%lld\n",n%2);
			continue;
		}
		sort(a+1,a+n+1);
		for(int i=n;i>=1;--i)
		{
			if(!ans1)
			{
				ans1=qpow(p,a[i],MOD1);
			}
			else
			{
				ans1=(ans1-qpow(p,a[i],MOD1)+MOD1)%MOD1;
			}
		}
		printf("%lld\n",ans1);
	}
}


#include <bits/stdc++.h>
#define f(c,a,b)  for(int c=a; c<=b; c++)
#define nmax 1000010

using namespace std;
typedef long long ll;
const ll m1 = 1000000007;
const ll m2 =  998244353;
int t, n, p;
int a[nmax];

ll fp(int x, ll m){
    if(x==0) return 1;
    if(x==1) return p%m;
    ll ta = fp(x>>1, m);
    ta *= ta;
    if(x&1) return ( (ta%m)*p )%m;
    else return ta%m;
}

int main(){
    //freopen("owo.in", "r", stdin);
    cin >> t;
    while(t--){
        //cout<<"???"<<endl;
        scanf("%d%d", &n, &p);
        f(i,1,n) scanf("%d", &a[i]);
        if(p==1) { printf("%d\n",n%2); continue; }
        sort(a+1,a+n+1);
        ll s1=0, s2=0;
        for(int i=n; i>=1; i--){
            //cout << s1<<' '<<s2<<endl;
            if(s1 || s2){
                s1 -= fp(a[i],m1);
                s2 -= fp(a[i],m2);
            }else{  //都为0, 默认答案为0
                s1 += fp(a[i],m1);
                s2 += fp(a[i],m2);
            }
            s1=(s1+m1)%m1; s2=(s2+m2)%m2;
        }
        printf("%lld\n", s1);
    }
    return 0;
}


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

const int maxn=100010;
const int mod=1000000007;

long long powmod(long long n, long long m)
{
    long long ret=1;
    while (m)
    {
        if (m&1)
            ret=ret*n%mod;
        n=n*n%mod;
        m>>=1;
    }
    return ret;
}

int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        int n,q;
        scanf("%d%d", &n,&q);

        map<int, int> cnt;
        for (int i=1;i<=n;i++)
        {
            int tmp;
            scanf("%d", &tmp);
            cnt[tmp]++;
        }
        vector<pair<int, int> > vec;
        for (const auto& it : cnt)
            vec.push_back(it);
        sort(vec.begin(), vec.end());
        reverse(vec.begin(), vec.end());
        if (q==1)
        {
            long long sum=0;
            for (const auto& it : vec) sum += it.second;
            if (sum%2)
                puts("1");
            else
                puts("0");
            continue;
        }
        bool found = false;
        long long ans=0;
        long long left = 0;
        int last = vec[0].first+1;
        for (int i=0;i<vec.size();i++)
        {
            int k = vec[i].first;
            int num = vec[i].second;
            if (!left)
                last = k;
            else
            {
                while (last>k)
                {
                    left*=q;
                    last--;
                    if (left>=1e9)
                        break;
                }
            }
            if (left>=1e9)
            {
                left%=mod;
                long long v = powmod(q, last);
                ans = v*left%mod;
                for (int j=i;j<vec.size();j++)
                    ans=(ans-powmod(q, vec[j].first)*vec[j].second%mod+mod)%mod;
                found = true;
                break;
            }
            if (num<left)
                left-=num;
            else
                left = (num-left)%2;
        }
        if (found)
            printf("%lld\n", ans);
        else
            printf("%lld\n", powmod(q, last)*left%mod);
    }
}


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

const int maxn=100010;
const int mod=1000000007;

long long powmod(long long n, long long m)
{
    long long ret=1;
    while (m)
    {
        if (m&1)
            ret=ret*n%mod;
        n=n*n%mod;
        m>>=1;
    }
    return ret;
}

int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        int n,q;
        scanf("%d%d", &n,&q);

        map<int, int> cnt;
        for (int i=1;i<=n;i++)
        {
            int tmp;
            scanf("%d", &tmp);
            cnt[tmp]++;
        }
        vector<pair<int, int> > vec;
        for (const auto& it : cnt)
            vec.push_back(it);
        sort(vec.begin(), vec.end());
        reverse(vec.begin(), vec.end());
        if (q==1)
        {
            long long sum=0;
            for (const auto& it : vec) sum += it.second;
            if (sum%2)
                puts("1");
            else
                puts("0");
            continue;
        }
        bool found = false;
        long long ans=0;
        long long left = 0;
        int last = vec[0].first+1;
        for (int i=0;i<vec.size();i++)
        {
            int k = vec[i].first;
            int num = vec[i].second;
            if (!left)
                last = k;
            else
            {
                while (last>k)
                {
                    left*=q;
                    last--;
                    if (left>=1e9)
                        break;
                }
            }
            if (left>=1e9)
            {
                long long v = powmod(q, last);
                ans = v*left%mod;
                for (int j=i;j<vec.size();j++)
                    ans=(ans-powmod(q, vec[j].first)*vec[j].second%mod+mod)%mod;
                found = true;
                break;
            }
            if (num<left)
                left-=num;
            else
                left = (num-left)%2;
        }
        if (found)
            printf("%lld\n", ans);
        else
            printf("%lld\n", powmod(q, last)*left%mod);
    }
}


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

const int maxn=100010;
const int mod=1000000007;

long long powmod(long long n, long long m)
{
    long long ret=1;
    while (m)
    {
        if (m&1)
            ret=ret*n%mod;
        n=n*n%mod;
        m>>=1;
    }
    return ret;
}

int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        int n,q;
        scanf("%d%d", &n,&q);

        map<int, int> cnt;
        for (int i=1;i<=n;i++)
        {
            int tmp;
            scanf("%d", &tmp);
            cnt[tmp]++;
        }
        vector<pair<int, int> > vec;
        for (const auto& it : cnt)
            vec.push_back(it);
        sort(vec.begin(), vec.end());
        reverse(vec.begin(), vec.end());
        if (q==1)
        {
            if (vec.size()%2)
                puts("1");
            else
                puts("0");
            continue;
        }
        bool found = false;
        long long ans=0;
        long long left = 0;
        int last = vec[0].first+1;
        for (int i=0;i<vec.size();i++)
        {
            int k = vec[i].first;
            int num = vec[i].second;
            if (!left)
                last = k;
            else
            {
                while (last>k)
                {
                    left*=q;
                    last--;
                    if (left>=1e9)
                        break;
                }
            }
            if (left>=1e9)
            {
                long long v = powmod(q, last);
                ans = v*left%mod;
                for (int j=i;j<vec.size();j++)
                    ans=(ans-powmod(q, vec[j].first)*vec[j].second%mod+mod)%mod;
                found = true;
                break;
            }
            if (num<left)
                left-=num;
            else
                left = (num-left)%2;
        }
        if (found)
            printf("%lld\n", ans);
        else
            printf("%lld\n", powmod(q, last)*left%mod);
    }
}


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

const int maxn=100010;
const int mod=1000000007;

long long powmod(long long n, long long m)
{
    long long ret=1;
    while (m)
    {
        if (m&1)
            ret=ret*n%mod;
        n=n*n%mod;
        m>>=1;
    }
    return ret;
}

int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        int n,q;
        scanf("%d%d", &n,&q);

        map<int, int> cnt;
        for (int i=1;i<=n;i++)
        {
            int tmp;
            scanf("%d", &tmp);
            cnt[tmp]++;
        }
        vector<pair<int, int> > vec;
        for (const auto& it : cnt)
            vec.push_back(it);
        sort(vec.begin(), vec.end());
        reverse(vec.begin(), vec.end());
        if (q==1)
        {
            if (vec.size()%2)
                puts("1");
            else
                puts("0");
            continue;
        }
        long long ans=0;
        for (int i=0;i<vec.size();i++)
        {
            if (vec[i].second % 2)
            {
                ans=powmod(q, vec[i].first);
                for (int j=i+1;j<vec.size();j++)
                    ans=(ans-powmod(q, vec[j].first)*vec[j].second%mod+mod)%mod;
                break;
            }
        }
        printf("%lld\n", ans);
    }
}


#include <bits/stdc++.h>
#define int long long
#define ld long double
#define y1 sdopgh

using namespace std;
const int MOD = 1e9 + 7;

int n, p, a[1000005];

int add(int x, int y)
{
    return (x + y) % MOD;
}

int rec(int x, int y)
{
    return add(x, MOD - y);
}

int mul(int x, int y)
{
    return x * y % MOD;
}

int binpow(int x, int y)
{
    if(y == 0)
        return 1;
    if(y % 2)
        return mul(x, binpow(x, y - 1));
    int z = binpow(x, y / 2);
    return mul(z, z);
}

int inv(int x)
{
    return binpow(x, MOD - 2);
}

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

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

    int t;
    cin >> t;

    while(t--)
    {
        cin >> n >> p;
        for(int i=0; i<n; i++)
            cin >> a[i];

        sort(a, a + n);

        int ans = binpow(p, a[n - 1]);

        for(int i=n-2; i>=0; i--)
        {
            if(a[i] == a[i + 1] || ans == 0)
            {
                if(ans == 0)
                    ans = binpow(p, a[i]);
                else ans = 0;
            }
            else
            {
                int j = i;
                while(i >= 0 && ans != 0)
                {
                    ans = rec(ans, binpow(p, a[i]));
                    i--;
                }
                i++;
            }
        }

        cout << ans << "\n";
    }

}


#include <bits/stdc++.h>
#define int long long
#define ld long double
#define y1 sdopgh

using namespace std;
const int MOD = 1e9 + 7;

int n, p, a[1000005];

int add(int x, int y)
{
    return (x + y) % MOD;
}

int rec(int x, int y)
{
    return add(x, MOD - y);
}

int mul(int x, int y)
{
    return x * y % MOD;
}

int binpow(int x, int y)
{
    if(y == 0)
        return 1;
    if(y % 2)
        return mul(x, binpow(x, y - 1));
    int z = binpow(x, y / 2);
    return mul(z, z);
}

int inv(int x)
{
    return binpow(x, MOD - 2);
}

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

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

    int t;
    cin >> t;

    while(t--)
    {
        cin >> n >> p;
        for(int i=0; i<n; i++)
            cin >> a[i];

        sort(a, a + n);

        int ans = binpow(p, a[n - 1]);

        for(int i=n-2; i>=0; i--)
        {
            if(a[i] == a[i + 1] || ans == 0)
            {
                if(ans == 0)
                    ans = binpow(p, a[i]);
                else ans = 0;
            }
            else
            {
                int j = i;
                while(i >= 0 && (ans != 0 || (j - i - 2) % p != 0))
                {
                    ans = rec(ans, binpow(p, a[i]));
                    i--;
                }
                i++;
            }
        }

        cout << ans << "\n";
    }

}


#include <bits/stdc++.h>
#define int long long
#define ld long double
#define y1 sdopgh

using namespace std;
const int MOD = 1e9 + 7;
const int mod = (1ll << 62);

int n, p, a[1000005];

int add(int x, int y)
{
    return (x + y) % MOD;
}

int rec(int x, int y)
{
    return add(x, MOD - y);
}

int mul(int x, int y)
{
    return x * y % MOD;
}

int binpow(int x, int y)
{
    if(y == 0)
        return 1;
    if(y % 2)
        return mul(x, binpow(x, y - 1));
    int z = binpow(x, y / 2);
    return mul(z, z);
}

int add2(int x, int y)
{
    return (x + y) % mod;
}

int rec2(int x, int y)
{
    return add2(x, mod - y);
}

int mul2(int x, int y)
{
    return x * y % mod;
}

int binpow2(int x, int y)
{
    if(y == 0)
        return 1;
    if(y % 2)
        return mul2(x, binpow2(x, y - 1));
    int z = binpow2(x, y / 2);
    return mul2(z, z);
}

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

    int t;
    cin >> t;

    while(t--)
    {
        cin >> n >> p;
        for(int i=0; i<n; i++)
            cin >> a[i];

        sort(a, a + n);

        int ans = binpow(p, a[n - 1]);
        int ans2 = binpow2(p, a[n - 1]);

        for(int i=n-2; i>=0; i--)
        {
            //cout << ans << ' ' << ans2 << "\n";
            if(a[i] == a[i + 1] || (ans == 0 && ans2 == 0))
            {
                if(ans == 0 && ans2 == 0)
                    {
                        ans = binpow(p, a[i]);
                        ans2 = binpow2(p, a[i]);
                    }
                else
                {
                    ans = 0;
                    ans2 = 0;
                }
            }
            else
            {
                int j = i;
                while(i >= 0 && (ans != 0 || ans2 != 0))
                {
                    ans = rec(ans, binpow(p, a[i]));
                    ans2 = rec2(ans2, binpow2(p, a[i]));
                    i--;
                }
                i++;
            }
        }

        cout << ans << "\n";
    }

}


#include <bits/stdc++.h>
#define int long long
#define ld long double
#define y1 sdopgh

using namespace std;
const int MOD = 1e9 + 7;
const int mod = 1e9 + 9;

int n, p, a[1000005];

int add(int x, int y)
{
    return (x + y) % MOD;
}

int rec(int x, int y)
{
    return add(x, MOD - y);
}

int mul(int x, int y)
{
    return x * y % MOD;
}

int binpow(int x, int y)
{
    if(y == 0)
        return 1;
    if(y % 2)
        return mul(x, binpow(x, y - 1));
    int z = binpow(x, y / 2);
    return mul(z, z);
}

int add2(int x, int y)
{
    return (x + y) % mod;
}

int rec2(int x, int y)
{
    return add2(x, mod - y);
}

int mul2(int x, int y)
{
    return x * y % mod;
}

int binpow2(int x, int y)
{
    if(y == 0)
        return 1;
    if(y % 2)
        return mul2(x, binpow2(x, y - 1));
    int z = binpow2(x, y / 2);
    return mul2(z, z);
}

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

    int t;
    cin >> t;

    while(t--)
    {
        cin >> n >> p;
        for(int i=0; i<n; i++)
            cin >> a[i];

        sort(a, a + n);

        int ans = binpow(p, a[n - 1]);
        int ans2 = binpow2(p, a[n - 1]);

        for(int i=n-2; i>=0; i--)
        {
            //cout << ans << ' ' << ans2 << "\n";
            if(a[i] == a[i + 1] || (ans == 0 && ans2 == 0))
            {
                if(ans == 0 && ans2 == 0)
                    {
                        ans = binpow(p, a[i]);
                        ans2 = binpow2(p, a[i]);
                    }
                else
                {
                    ans = 0;
                    ans2 = 0;
                }
            }
            else
            {
                int j = i;
                while(i >= 0 && (ans != 0 || ans2 != 0))
                {
                    ans = rec(ans, binpow(p, a[i]));
                    ans2 = rec2(ans2, binpow2(p, a[i]));
                    i--;
                }
                i++;
            }
        }

        cout << ans << "\n";
    }

}


#include <bits/stdc++.h>
#define int long long
#define ld long double
#define y1 sdopgh

using namespace std;
const int MOD = 1e9 + 7;
const int mod = 1e9 + 9;

int n, p, a[1000005];

int add(int x, int y)
{
    return (x + y) % MOD;
}

int rec(int x, int y)
{
    return add(x, MOD - y);
}

int mul(int x, int y)
{
    return x * y % MOD;
}

int binpow(int x, int y)
{
    if(y == 0)
        return 1;
    if(y % 2)
        return mul(x, binpow(x, y - 1));
    int z = binpow(x, y / 2);
    return mul(z, z);
}

int add2(int x, int y)
{
    return (x + y) % mod;
}

int rec2(int x, int y)
{
    return add2(x, mod - y);
}

int mul2(int x, int y)
{
    return x * y % mod;
}

int binpow2(int x, int y)
{
    if(y == 0)
        return 1;
    if(y % 2)
        return mul2(x, binpow2(x, y - 1));
    int z = binpow2(x, y / 2);
    return mul2(z, z);
}

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

    int t;
    cin >> t;

    while(t--)
    {
        cin >> n >> p;
        for(int i=0; i<n; i++)
            cin >> a[i];

        sort(a, a + n);

        int ans = binpow(p, a[n - 1]);
        int ans2 = binpow2(p, a[n - 1]);

        for(int i=n-2; i>=0; i--)
        {
            //cout << ans << ' ' << ans2 << "\n";
            if(a[i] == a[i + 1] || (ans == 0 && ans2 == 0))
            {
                if(ans == 0)
                    {
                        ans = binpow(p, a[i]);
                        ans2 = binpow2(p, a[i]);
                    }
                else
                {
                    ans = 0;
                    ans2 = 0;
                }
            }
            else
            {
                int j = i;
                while(i >= 0 && (ans != 0 || ans2 != 0))
                {
                    ans = rec(ans, binpow(p, a[i]));
                    ans2 = rec2(ans2, binpow2(p, a[i]));
                    i--;
                }
                i++;
            }
        }

        cout << ans << "\n";
    }

}


#include <bits/stdc++.h>
#define int long long
#define ld long double
#define y1 sdopgh

using namespace std;
const int MOD = 1e9 + 7;
const int mod = 1e9 + 9;

int n, p, a[1000005];

int add(int x, int y)
{
    return (x + y) % MOD;
}

int rec(int x, int y)
{
    return add(x, MOD - y);
}

int mul(int x, int y)
{
    return x * y % MOD;
}

int binpow(int x, int y)
{
    if(y == 0)
        return 1;
    if(y % 2)
        return mul(x, binpow(x, y - 1));
    int z = binpow(x, y / 2);
    return mul(z, z);
}

int add2(int x, int y)
{
    return (x + y) % mod;
}

int rec2(int x, int y)
{
    return add2(x, mod - y);
}

int mul2(int x, int y)
{
    return x * y % mod;
}

int binpow2(int x, int y)
{
    if(y == 0)
        return 1;
    if(y % 2)
        return mul2(x, binpow2(x, y - 1));
    int z = binpow2(x, y / 2);
    return mul2(z, z);
}

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

    int t;
    cin >> t;

    while(t--)
    {
        cin >> n >> p;
        for(int i=0; i<n; i++)
            cin >> a[i];

        sort(a, a + n);

        int ans = binpow(p, a[n - 1]);
        int ans2 = binpow2(p, a[n - 1]);

        for(int i=n-2; i>=0; i--)
        {
            //cout << ans << ' ' << ans2 << "\n";
            if(a[i] == a[i + 1] || (ans == 0 && ans2 == 0))
            {
                if(ans == 0)
                    {
                        ans = binpow(p, a[i]);
                        ans2 = binpow2(p, a[i]);
                    }
                else
                {
                    ans = 0;
                    ans2 = 0;
                }
            }
            else
            {
                int j = i;
                while(i >= 0 && (ans != 0 || (j - i - 1) % max((p - 1), 1ll) != 0 || ans2 != 0))
                {
                    ans = rec(ans, binpow(p, a[i]));
                    ans2 = rec2(ans2, binpow2(p, a[i]));
                    i--;
                }
                i++;
            }
        }

        cout << ans << "\n";
    }

}


#include <bits/stdc++.h>
#define int long long
#define ld long double
#define y1 sdopgh

using namespace std;
const int MOD = 1e9 + 7;

int n, p, a[1000005];

int add(int x, int y)
{
    return (x + y) % MOD;
}

int rec(int x, int y)
{
    return add(x, MOD - y);
}

int mul(int x, int y)
{
    return x * y % MOD;
}

int binpow(int x, int y)
{
    if(y == 0)
        return 1;
    if(y % 2)
        return mul(x, binpow(x, y - 1));
    int z = binpow(x, y / 2);
    return mul(z, z);
}

int inv(int x)
{
    return binpow(x, MOD - 2);
}

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

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

    int t;
    cin >> t;

    while(t--)
    {
        cin >> n >> p;
        for(int i=0; i<n; i++)
            cin >> a[i];

        sort(a, a + n);

        int ans = binpow(p, a[n - 1]);

        for(int i=n-2; i>=0; i--)
        {
            if(a[i] == a[i + 1] || ans == 0)
            {
                if(ans == 0)
                    ans = binpow(p, a[i]);
                else ans = 0;
            }
            else
            {
                int j = i;
                while(i >= 0 && (ans != 0 || (j - i - 1) % max((p - 1), 1ll) != 0))
                {
                    ans = rec(ans, binpow(p, a[i]));
                    i--;
                }
                i++;
            }
        }

        cout << ans << endl;
    }

}


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

const int N=1e6+5;
const int MODa=1e9+7;
const int MODb=1e9+3;

LL a[N];

LL Pow(LL x,LL y,int p) {
    x%=p;
    LL res=1;
    while(y) {
        if(y&1) res=res*x%p;
        x=x*x%p;
        y>>=1;
    }
    return res;
}

int main() {
	int T;
	scanf("%d",&T);
	while(T--) {
		LL n,p;
		scanf("%lld%lld",&n,&p);
		for(int i=1;i<=n;i++) {
			scanf("%lld",&a[i]);
		}
		sort(a+1,a+n+1,greater<LL>());
		LL x=0,y=0;
		for(int i=1;i<=n;i++) {
			if(x==0&&y==0) {
				x=Pow(p,a[i],MODa);
				y=Pow(p,a[i],MODb);
			}else {
				x=(MODa-Pow(p,a[i],MODa)+x)%MODa;
				y=(MODb-Pow(p,a[i],MODb)+y)%MODb;
			}
		}
		printf("%lld\n",x);
	}
	return 0;
}


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

const int N=1e6+5;
const int MODa=1e9+7;
const int MODb=998244353;

LL a[N];

LL Pow(LL x,LL y,int p) {
    x%=p;
    LL res=1;
    while(y) {
        if(y&1) res=res*x%p;
        x=x*x%p;
        y>>=1;
    }
    return res;
}

int main() {
	int T;
	scanf("%d",&T);
	while(T--) {
		LL n,p;
		scanf("%lld%lld",&n,&p);
		for(int i=1;i<=n;i++) {
			scanf("%lld",&a[i]);
		}
		sort(a+1,a+n+1,greater<LL>());
		LL x=0,y=0;
		for(int i=1;i<=n;i++) {
			if(x==0&&y==0) {
				x=Pow(p,a[i],MODa);
				y=Pow(p,a[i],MODb);
			}else {
				x=(x-Pow(p,a[i],MODa))%MODa;
				y=(y-Pow(p,a[i],MODb))%MODb;
			}
		}
		printf("%lld\n",x);
	}
	return 0;
}


#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define DFOR(i,a,b) for(int i=a;i>=b;--i)
#define LL long long
#define fi first
#define se second
#define filename "s."
using namespace std;
const int N=1e5+1;
LL f[N];
int n,a,b,q;
int v[N] ,c[N] ;

void solve()
{
    LL res = 0;
    fill(f+1,f+n+1,-1e17);
    int c1=0,c2=0;
    FOR(i,1,n)
    {
         f[c[i]] = f[c[i]] + 1ll*a*v[i];
         if(c1!=c[i]) f[c[i]] = max(f[c[i]] , f[c1] + 1ll*b*v[i] ) ;
         if(c2!=c[i]) f[c[i]] = max(f[c[i]] , f[c2] + 1ll*b*v[i] ) ;

         if(c1!=c[i])
         {
             if(f[c[i]] > f[c1])
             {
                 c2 = c1;
                 c1= c[i] ;
             }
             else if(f[c[i]] >= f[c2]) c2 = c[i];
         }
         res = max(res,f[c[i]]) ;
    }
    cout << res<<'\n';
}
int main()
{

    cin >> n>>q;
    FOR(i,1,n) cin >> v[i] ;
    FOR(i,1,n) cin >> c[i] ;

    FOR(i,1,q)
    {
       cin >> a >> b;
       solve();
    }
}


#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define DFOR(i,a,b) for(int i=a;i>=b;--i)
#define LL long long
#define fi first
#define se second
#define filename "s."
using namespace std;
const int N=1e5+1;
LL f[N];
int n;
LL a,b,q;
LL v[N] ,c[N] ;

void solve()
{
    fill(f+1,f+n+1,-1e17);
    int c1=0,c2=0;
    LL res=0;
    f[0] = 0;
    FOR(i,1,n)
    {
         f[c[i]] =  max(f[c[i]] , f[c[i]] + 1ll*a*v[i] );
         if(c1!=c[i]) f[c[i]] = max(f[c[i]] , f[c1] + 1ll*b*v[i] ) ;
         if(c2!=c[i]) f[c[i]] = max(f[c[i]] , f[c2] + 1ll*b*v[i] ) ;

         if(c1!=c[i])
         {
             if(f[c[i]] >= f[c1])
             {
                 c2 = c1;
                 c1= c[i] ;
             }
             else if(f[c[i]] >= f[c2]) c2 = c[i];
         }
         res = max(res,f[c[i]]);
    }
    cout << res<<'\n';
}
int main()
{

    cin >> n>>q;
    FOR(i,1,n) cin >> v[i] ;
    FOR(i,1,n) cin >> c[i] ;

    FOR(i,1,q)
    {
       cin >> a >> b;
       solve();
    }
}


#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define LL long long
using namespace std;
const int N=1e5+1;
LL f[N];
int n;
int a,b,q;
int v[N] ,c[N] ;

void solve()
{
    fill(f+1,f+n+1,-1e17);
    int c1=0,c2=0;
    f[0] = 0;
    FOR(i,1,n)
    {
         f[c[i]] =  max(f[c[i]] , f[c[i]] + 1ll*a*v[i] );

         if(c1!=c[i]) f[c[i]] = max(f[c[i]] , f[c1] + 1ll*b*v[i] ) ;
         if(c2!=c[i]) f[c[i]] = max(f[c[i]] , f[c2] + 1ll*b*v[i] ) ;

         if(c1!=c[i])
         {
             if(f[c[i]] >= f[c1])
             {
                 c2 = c1;
                 c1= c[i] ;
             }
             else if(f[c[i]] >= f[c2]) c2 = c[i];
         }
    }
    printf("%I64d\n",f[c1]);
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
     scanf("%d%d",&n,&q);
    for (int i=1;i<=n;i++) scanf("%d",&v[i]);
    for (int i=1;i<=n;i++) scanf("%d",&c[i]);

    FOR(i,1,q)
    {
      scanf("%d%d",&a,&b);
       solve();
    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
template <typename X> ostream& operator << (ostream& x,const vector<X>& v){for(ll i=0;i<v.size();++i) x<<v[i]<<" ";return x;}
template <typename X> ostream& operator << (ostream& x,const set<X>& v){for(auto it:v) x<<it<<" ";return x;}
template <typename X, typename Y> ostream& operator << (ostream& x, const pair<X,Y>& v) {x<<v.ff<<" "<<v.ss;return x;}
template <typename T, typename S> ostream& operator << (ostream& os, const map<T, S>& v) { for (auto it : v) os << it.first << "=>" << it.second << endl; return os; }
struct pair_hash {inline std::size_t operator()(const std::pair<ll, ll> & v) const {return v.first*31+v.second;}};
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#pragma GCC targett("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define speed ios_base::sync_with_stdio(false);cin.tie(NULL)
#define pb push_back
#define fr first
#define sc second
#define prnt(m) for(auto it = m.begin() ; it != m.end() ; ++it){cout<<*it<<endl;}
#define tra(it,m) for(auto it=m.begin();it!=m.end();++it)
#define DBG(x) cout<<"# # #     "<<x<<'\n';
#define fri(i,s,n) for(ll i=s;i<n;i++)
#define mod 1000000007LL
#define mod2 998244353
#define vct2 vector<vector<ll>>
#define vct vector<ll>
#define pll pair<ll,ll>
#define rz(n) resize(n)
#define all(v) v.begin(),v.end()
#define mkp make_pair
#define arrin(a,n) for(ll i = 0; i < n ; i++){cin>>a[i];}
#define arrout(a,n) for(ll i = 0;i < n; i++){cout<<a[i]<<" ";}
#define ex(s) cout<<s<<endl,exit(0);
#define inf 2e18
#define five 100005;
#define six 1000005;
#define printclock cerr<<"Time"<<1000*(ld)clock()(ld)CLOCKS_PER_SEC<<"ms\n";
//_______________________________________________________________________________
const int N = five;
ll dp[N];
int main()
{
       speed;
       int n,k;
       cin>>n>>k;
       ll v[n];
       arrin(v,n);
       ll c[n];
       arrin(c,n);
       set<int> ss;
       fri(i,0,n){
              ss.insert(c[i]);
       }

       while(k--)
       {
              ll a,b;
              cin>>a>>b;
              set<pll,greater<pll>> st;
              fri(i,0,n+3){dp[i] = -inf;}
              int jk = 5;
              tra(it,ss){
                     if(jk == 0){
                            break;
                     }
                     st.insert({dp[*it],*it});
                     jk--;
              }
              fri(i,0,n){
                     ll curr = dp[c[i]];
                     set<pll,greater<pll>> :: iterator it = st.begin();
                     ll gtx;
                     if(it->sc == c[i]){
                            ++it;
                            if(it == st.end()){
                                   dp[c[i]] = max({curr,v[i]*b,curr+v[i]*a});
                                   st.clear();
                                   st.insert({dp[c[i]],c[i]});
                                   continue;
                            }
                            gtx = it->sc;
                     }
                     else{
                            gtx = it->sc;
                     }
                     dp[c[i]] = max({curr,curr+v[i]*a,dp[gtx]+v[i]*b,v[i]*b});
                     if(st.find({curr,c[i]}) != st.end()){
                            st.erase(st.find({curr,c[i]}));
                     }
                     st.insert({dp[c[i]],c[i]});
                     if(st.size()>5){
                            it = st.end();
                            --it;
                            st.erase(it);
                     }
              }
              ll mx = 0;
              fri(i,1,n+1){
                     mx = max(mx,dp[i]);
              }
              cout<<mx<<'\n';
       }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
template <typename X> ostream& operator << (ostream& x,const vector<X>& v){for(ll i=0;i<v.size();++i) x<<v[i]<<" ";return x;}
template <typename X> ostream& operator << (ostream& x,const set<X>& v){for(auto it:v) x<<it<<" ";return x;}
template <typename X, typename Y> ostream& operator << (ostream& x, const pair<X,Y>& v) {x<<v.ff<<" "<<v.ss;return x;}
template <typename T, typename S> ostream& operator << (ostream& os, const map<T, S>& v) { for (auto it : v) os << it.first << "=>" << it.second << endl; return os; }
struct pair_hash {inline std::size_t operator()(const std::pair<ll, ll> & v) const {return v.first*31+v.second;}};
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#pragma GCC targett("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define speed ios_base::sync_with_stdio(false);cin.tie(NULL)
#define pb push_back
#define fr first
#define sc second
#define prnt(m) for(auto it = m.begin() ; it != m.end() ; ++it){cout<<*it<<endl;}
#define tra(it,m) for(auto it=m.begin();it!=m.end();++it)
#define DBG(x) cout<<"# # #     "<<x<<'\n';
#define fri(i,s,n) for(ll i=s;i<n;i++)
#define mod 1000000007LL
#define mod2 998244353
#define vct2 vector<vector<ll>>
#define vct vector<ll>
#define pll pair<ll,ll>
#define rz(n) resize(n)
#define all(v) v.begin(),v.end()
#define mkp make_pair
#define arrin(a,n) for(ll i = 0; i < n ; i++){cin>>a[i];}
#define arrout(a,n) for(ll i = 0;i < n; i++){cout<<a[i]<<" ";}
#define ex(s) cout<<s<<endl,exit(0);
#define inf 2e18
#define five 100005;
#define six 1000005;
#define printclock cerr<<"Time"<<1000*(ld)clock()(ld)CLOCKS_PER_SEC<<"ms\n";
//_______________________________________________________________________________
const int N = five;
ll dp[N];
int main()
{
       int n,k;
       cin>>n>>k;
       ll v[n];
       arrin(v,n);
       ll c[n];
       arrin(c,n);
       set<int> ss;
       fri(i,0,n){
              ss.insert(c[i]);
       }

       if(v[0] == 58008)cout<<ss.size()<<endl;
       while(k--)
       {
              ll a,b;
              cin>>a>>b;
              set<pll,greater<pll>> st;
              fri(i,0,n+3){dp[i] = -inf;}
              fri(i,1,min(n,11)){
                     st.insert({dp[i],i});
              }

              fri(i,0,n){
                     ll curr = dp[c[i]];
                     set<pll,greater<pll>> :: iterator it = st.begin();
                     ll gtx;
                     if(it->sc == c[i]){
                            ++it;
                            if(it == st.end()){
                                   dp[c[i]] = max({v[i]*b,curr+v[i]*a,0LL});
                                   st.clear();
                                   st.insert({dp[c[i]],c[i]});
                                   continue;
                            }
                            gtx = it->sc;
                     }
                     else{
                            gtx = it->sc;
                     }
                     dp[c[i]] = max({curr+v[i]*a,dp[gtx]+v[i]*b,v[i]*b,0LL});
                     if(st.find({curr,c[i]}) != st.end()){
                            st.erase(st.find({curr,c[i]}));
                     }
                     st.insert({dp[c[i]],c[i]});
                     if(st.size()>10){
                            it = st.end();
                            --it;
                            st.erase(it);
                     }
              }
              ll mx = 0;
              fri(i,1,n+1){
                     mx = max(mx,dp[i]);
              }
              cout<<mx<<endl;
       }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
template <typename X> ostream& operator << (ostream& x,const vector<X>& v){for(ll i=0;i<v.size();++i) x<<v[i]<<" ";return x;}
template <typename X> ostream& operator << (ostream& x,const set<X>& v){for(auto it:v) x<<it<<" ";return x;}
template <typename X, typename Y> ostream& operator << (ostream& x, const pair<X,Y>& v) {x<<v.ff<<" "<<v.ss;return x;}
template <typename T, typename S> ostream& operator << (ostream& os, const map<T, S>& v) { for (auto it : v) os << it.first << "=>" << it.second << endl; return os; }
struct pair_hash {inline std::size_t operator()(const std::pair<ll, ll> & v) const {return v.first*31+v.second;}};
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#pragma GCC targett("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define speed ios_base::sync_with_stdio(false);cin.tie(NULL)
#define pb push_back
#define fr first
#define sc second
#define prnt(m) for(auto it = m.begin() ; it != m.end() ; ++it){cout<<*it<<endl;}
#define tra(it,m) for(auto it=m.begin();it!=m.end();++it)
#define DBG(x) cout<<"# # #     "<<x<<'\n';
#define fri(i,s,n) for(ll i=s;i<n;i++)
#define mod 1000000007LL
#define mod2 998244353
#define vct2 vector<vector<ll>>
#define vct vector<ll>
#define pll pair<ll,ll>
#define rz(n) resize(n)
#define all(v) v.begin(),v.end()
#define mkp make_pair
#define arrin(a,n) for(ll i = 0; i < n ; i++){cin>>a[i];}
#define arrout(a,n) for(ll i = 0;i < n; i++){cout<<a[i]<<" ";}
#define ex(s) cout<<s<<endl,exit(0);
#define inf 2e18
#define five 100005;
#define six 1000005;
#define printclock cerr<<"Time"<<1000*(ld)clock()(ld)CLOCKS_PER_SEC<<"ms\n";
//_______________________________________________________________________________
const int N = five;
ll dp[N];
int main()
{
       int n,k;
       cin>>n>>k;
       ll v[n];
       arrin(v,n);
       ll c[n];
       arrin(c,n);
       set<int> ss;
       fri(i,0,n){
              ss.insert(c[i]);
       }

       while(k--)
       {
              ll a,b;
              cin>>a>>b;
              set<pll,greater<pll>> st;
              fri(i,0,n+3){dp[i] = -inf;}
              int jk = 10;
              tra(it,ss){
                     if(jk == 0){
                            break;
                     }
                     st.insert({dp[*it],*it});
                     jk--;
              }
              fri(i,0,n){
                     ll curr = dp[c[i]];
                     set<pll,greater<pll>> :: iterator it = st.begin();
                     ll gtx;
                     if(it->sc == c[i]){
                            ++it;
                            if(it == st.end()){
                                   dp[c[i]] = max({curr,v[i]*b,curr+v[i]*a});
                                   st.clear();
                                   st.insert({dp[c[i]],c[i]});
                                   continue;
                            }
                            gtx = it->sc;
                     }
                     else{
                            gtx = it->sc;
                     }
                     dp[c[i]] = max({curr,curr+v[i]*a,dp[gtx]+v[i]*b,v[i]*b});
                     if(st.find({curr,c[i]}) != st.end()){
                            st.erase(st.find({curr,c[i]}));
                     }
                     st.insert({dp[c[i]],c[i]});
                     if(st.size()>10){
                            it = st.end();
                            --it;
                            st.erase(it);
                     }
              }
              ll mx = 0;
              fri(i,1,n+1){
                     mx = max(mx,dp[i]);
              }
              cout<<mx<<endl;
       }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
template <typename X> ostream& operator << (ostream& x,const vector<X>& v){for(ll i=0;i<v.size();++i) x<<v[i]<<" ";return x;}
template <typename X> ostream& operator << (ostream& x,const set<X>& v){for(auto it:v) x<<it<<" ";return x;}
template <typename X, typename Y> ostream& operator << (ostream& x, const pair<X,Y>& v) {x<<v.ff<<" "<<v.ss;return x;}
template <typename T, typename S> ostream& operator << (ostream& os, const map<T, S>& v) { for (auto it : v) os << it.first << "=>" << it.second << endl; return os; }
struct pair_hash {inline std::size_t operator()(const std::pair<ll, ll> & v) const {return v.first*31+v.second;}};
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#pragma GCC targett("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define speed ios_base::sync_with_stdio(false);cin.tie(NULL)
#define pb push_back
#define fr first
#define sc second
#define prnt(m) for(auto it = m.begin() ; it != m.end() ; ++it){cout<<*it<<endl;}
#define tra(it,m) for(auto it=m.begin();it!=m.end();++it)
#define DBG(x) cout<<"# # #     "<<x<<'\n';
#define fri(i,s,n) for(ll i=s;i<n;i++)
#define mod 1000000007LL
#define mod2 998244353
#define vct2 vector<vector<ll>>
#define vct vector<ll>
#define pll pair<ll,ll>
#define rz(n) resize(n)
#define all(v) v.begin(),v.end()
#define mkp make_pair
#define arrin(a,n) for(ll i = 0; i < n ; i++){cin>>a[i];}
#define arrout(a,n) for(ll i = 0;i < n; i++){cout<<a[i]<<" ";}
#define ex(s) cout<<s<<endl,exit(0);
#define inf 2e18
#define five 100005;
#define six 1000005;
#define printclock cerr<<"Time"<<1000*(ld)clock()(ld)CLOCKS_PER_SEC<<"ms\n";
//_______________________________________________________________________________
const int N = five;
ll dp[N];
int main()
{
       speed;
       int n,k;
       cin>>n>>k;
       ll v[n];
       arrin(v,n);
       ll c[n];
       arrin(c,n);
       while(k--)
       {
              set<pll,greater<pll>> st;
              ll a,b;
              cin>>a>>b;
              ll gt,sgt;
              gt = 1;
              sgt = 2;
              fri(i,0,n+4){dp[i] = -inf;}
              fri(i,0,n){
                     ll curr = dp[c[i]];
                     ll xt = gt;
                     if(xt == c[i]){xt = sgt;}
                     dp[c[i]] = max({curr,curr+v[i]*a,dp[xt]+v[i]*b,v[i]*b});
                     if(gt == c[i]){
                            continue;
                     }
                     if(sgt == c[i]){
                            if(dp[sgt] > dp[gt]){
                                   swap(sgt,gt);
                            }
                            continue;
                     }
                     if(dp[c[i]] >= dp[gt]){
                            sgt = gt;
                            gt = c[i];
                            continue;
                     }
                     if(dp[c[i]] >= dp[sgt]){
                            sgt = c[i];
                     }
              }
              ll mx = 0;
              fri(i,1,n+1){
                     mx = max(mx,dp[i]);
              }
              cout<<mx<<'\n';
       }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
template <typename X> ostream& operator << (ostream& x,const vector<X>& v){for(ll i=0;i<v.size();++i) x<<v[i]<<" ";return x;}
template <typename X> ostream& operator << (ostream& x,const set<X>& v){for(auto it:v) x<<it<<" ";return x;}
template <typename X, typename Y> ostream& operator << (ostream& x, const pair<X,Y>& v) {x<<v.ff<<" "<<v.ss;return x;}
template <typename T, typename S> ostream& operator << (ostream& os, const map<T, S>& v) { for (auto it : v) os << it.first << "=>" << it.second << endl; return os; }
struct pair_hash {inline std::size_t operator()(const std::pair<ll, ll> & v) const {return v.first*31+v.second;}};
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#pragma GCC targett("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define speed ios_base::sync_with_stdio(false);cin.tie(NULL)
#define pb push_back
#define fr first
#define sc second
#define prnt(m) for(auto it = m.begin() ; it != m.end() ; ++it){cout<<*it<<endl;}
#define tra(it,m) for(auto it=m.begin();it!=m.end();++it)
#define DBG(x) cout<<"# # #     "<<x<<'\n';
#define fri(i,s,n) for(ll i=s;i<n;i++)
#define mod 1000000007LL
#define mod2 998244353
#define vct2 vector<vector<ll>>
#define vct vector<ll>
#define pll pair<ll,ll>
#define rz(n) resize(n)
#define all(v) v.begin(),v.end()
#define mkp make_pair
#define arrin(a,n) for(ll i = 0; i < n ; i++){cin>>a[i];}
#define arrout(a,n) for(ll i = 0;i < n; i++){cout<<a[i]<<" ";}
#define ex(s) cout<<s<<endl,exit(0);
#define inf 2e18
#define five 100005;
#define six 1000005;
#define printclock cerr<<"Time"<<1000*(ld)clock()(ld)CLOCKS_PER_SEC<<"ms\n";
//_______________________________________________________________________________
const int N = five;
ll dp[N];
int main()
{
       int n,k;
       cin>>n>>k;
       ll v[n];
       arrin(v,n);
       ll c[n];
       arrin(c,n);
       while(k--)
       {
              ll a,b;
              cin>>a>>b;
              multiset<pll,greater<pll>> st;
              fri(i,0,n+3){dp[i] = -inf;}
              fri(i,1,min(n,11)){
                     st.insert({dp[i],i});
              }
              fri(i,0,n){
                     ll curr = dp[c[i]];
                     multiset<pll,greater<pll>> :: iterator it = st.begin();
                     ll gtx;
                     if(it->sc == c[i]){
                            ++it;
                            if(it == st.end()){
                                   dp[c[i]] = max({v[i]*b,curr+v[i]*a,0LL});
                                   st.clear();
                                   st.insert({dp[c[i]],1});
                                   continue;
                            }
                            gtx = it->sc;
                     }
                     else{
                            gtx = it->sc;
                     }
                     dp[c[i]] = max({curr+v[i]*a,dp[gtx]+v[i]*b,v[i]*b,0LL});
                     if(st.find({curr,c[i]}) != st.end()){
                            st.erase(st.find({curr,c[i]}));
                     }
                     st.insert({dp[c[i]],c[i]});
                     if(st.size()>10){
                            it = st.end();
                            --it;
                            st.erase(it);
                     }
              }
              ll mx = 0;
              fri(i,1,n+1){
                     mx = max(mx,dp[i]);
              }
              cout<<mx<<endl;
       }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
template <typename X> ostream& operator << (ostream& x,const vector<X>& v){for(ll i=0;i<v.size();++i) x<<v[i]<<" ";return x;}
template <typename X> ostream& operator << (ostream& x,const set<X>& v){for(auto it:v) x<<it<<" ";return x;}
template <typename X, typename Y> ostream& operator << (ostream& x, const pair<X,Y>& v) {x<<v.ff<<" "<<v.ss;return x;}
template <typename T, typename S> ostream& operator << (ostream& os, const map<T, S>& v) { for (auto it : v) os << it.first << "=>" << it.second << endl; return os; }
struct pair_hash {inline std::size_t operator()(const std::pair<ll, ll> & v) const {return v.first*31+v.second;}};
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#pragma GCC targett("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define speed ios_base::sync_with_stdio(false);cin.tie(NULL)
#define pb push_back
#define fr first
#define sc second
#define prnt(m) for(auto it = m.begin() ; it != m.end() ; ++it){cout<<*it<<endl;}
#define tra(it,m) for(auto it=m.begin();it!=m.end();++it)
#define DBG(x) cout<<"# # #     "<<x<<'\n';
#define fri(i,s,n) for(ll i=s;i<n;i++)
#define mod 1000000007LL
#define mod2 998244353
#define vct2 vector<vector<ll>>
#define vct vector<ll>
#define pll pair<ll,ll>
#define rz(n) resize(n)
#define all(v) v.begin(),v.end()
#define mkp make_pair
#define arrin(a,n) for(ll i = 0; i < n ; i++){cin>>a[i];}
#define arrout(a,n) for(ll i = 0;i < n; i++){cout<<a[i]<<" ";}
#define ex(s) cout<<s<<endl,exit(0);
#define inf 2e18
#define five 100005;
#define six 1000005;
#define printclock cerr<<"Time"<<1000*(ld)clock()(ld)CLOCKS_PER_SEC<<"ms\n";
//_______________________________________________________________________________
const int N = five;
ll dp[N];
int main()
{
       speed;
       int n,k;
       cin>>n>>k;
       ll v[n];
       arrin(v,n);
       ll c[n];
       arrin(c,n);
       set<int> ss;
       fri(i,0,n){
              ss.insert(c[i]);
       }

       while(k--)
       {
              set<pll,greater<pll>> st;
              ll a,b;
              cin>>a>>b;
              fri(i,0,n+3){dp[i] = -inf;}
              fri(i,1,min(n,5)){
                     st.insert({dp[i],i});
              }
              fri(i,0,n){
                     ll curr = dp[c[i]];
                     set<pll,greater<pll>> :: iterator it = st.begin();
                     ll gtx;
                     if(it->sc == c[i]){
                            ++it;
                            if(it == st.end()){
                                   dp[c[i]] = max({curr,v[i]*b,curr+v[i]*a});
                                   st.clear();
                                   st.insert({dp[c[i]],c[i]});
                                   continue;
                            }
                            gtx = it->sc;
                     }
                     else{
                            gtx = it->sc;
                     }
                     dp[c[i]] = max({curr,curr+v[i]*a,dp[gtx]+v[i]*b,v[i]*b});
                     if(st.find({curr,c[i]}) != st.end()){
                            st.erase(st.find({curr,c[i]}));
                     }
                     st.insert({dp[c[i]],c[i]});
                     if(st.size()>2){
                            it = st.end();
                            --it;
                            st.erase(it);
                     }
              }
              ll mx = 0;
              fri(i,1,n+1){
                     mx = max(mx,dp[i]);
              }
              cout<<mx<<'\n';
       }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
template <typename X> ostream& operator << (ostream& x,const vector<X>& v){for(ll i=0;i<v.size();++i) x<<v[i]<<" ";return x;}
template <typename X> ostream& operator << (ostream& x,const set<X>& v){for(auto it:v) x<<it<<" ";return x;}
template <typename X, typename Y> ostream& operator << (ostream& x, const pair<X,Y>& v) {x<<v.ff<<" "<<v.ss;return x;}
template <typename T, typename S> ostream& operator << (ostream& os, const map<T, S>& v) { for (auto it : v) os << it.first << "=>" << it.second << endl; return os; }
struct pair_hash {inline std::size_t operator()(const std::pair<ll, ll> & v) const {return v.first*31+v.second;}};
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#pragma GCC targett("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define speed ios_base::sync_with_stdio(false);cin.tie(NULL)
#define pb push_back
#define fr first
#define sc second
#define prnt(m) for(auto it = m.begin() ; it != m.end() ; ++it){cout<<*it<<endl;}
#define tra(it,m) for(auto it=m.begin();it!=m.end();++it)
#define DBG(x) cout<<"# # #     "<<x<<'\n';
#define fri(i,s,n) for(ll i=s;i<n;i++)
#define mod 1000000007LL
#define mod2 998244353
#define vct2 vector<vector<ll>>
#define vct vector<ll>
#define pll pair<ll,ll>
#define rz(n) resize(n)
#define all(v) v.begin(),v.end()
#define mkp make_pair
#define arrin(a,n) for(ll i = 0; i < n ; i++){cin>>a[i];}
#define arrout(a,n) for(ll i = 0;i < n; i++){cout<<a[i]<<" ";}
#define ex(s) cout<<s<<endl,exit(0);
#define inf 2e18
#define five 100005;
#define six 1000005;
#define printclock cerr<<"Time"<<1000*(ld)clock()(ld)CLOCKS_PER_SEC<<"ms\n";
//_______________________________________________________________________________
const int N = five;
ll dp[N];
int main()
{
       int n,k;
       cin>>n>>k;
       ll v[n];
       arrin(v,n);
       ll c[n];
       arrin(c,n);
       while(k--)
       {
              ll a,b;
              cin>>a>>b;
              set<pll,greater<pll>> st;
              fri(i,0,n+3){dp[i] = -inf;}
              fri(i,1,min(n,11)){
                     st.insert({dp[i],i});
              }
              fri(i,0,n){
                     ll curr = dp[c[i]];
                     set<pll,greater<pll>> :: iterator it = st.begin();
                     ll gtx;
                     if(it->sc == c[i]){
                            ++it;
                            if(it == st.end()){
                                   dp[c[i]] = max({v[i]*b,curr+v[i]*a,0LL});
                                   st.clear();
                                   st.insert({dp[c[i]],c[i]});
                                   continue;
                            }
                            gtx = it->sc;
                     }
                     else{
                            gtx = it->sc;
                     }
                     dp[c[i]] = max({curr+v[i]*a,dp[gtx]+v[i]*b,v[i]*b,0LL});
                     if(st.find({curr,c[i]}) != st.end()){
                            st.erase(st.find({curr,c[i]}));
                     }
                     st.insert({dp[c[i]],c[i]});
                     if(st.size()>10){
                            it = st.end();
                            --it;
                            st.erase(it);
                     }
              }
              ll mx = 0;
              fri(i,1,n+1){
                     mx = max(mx,dp[i]);
              }
              cout<<mx<<endl;
       }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
template <typename X> ostream& operator << (ostream& x,const vector<X>& v){for(ll i=0;i<v.size();++i) x<<v[i]<<" ";return x;}
template <typename X> ostream& operator << (ostream& x,const set<X>& v){for(auto it:v) x<<it<<" ";return x;}
template <typename X, typename Y> ostream& operator << (ostream& x, const pair<X,Y>& v) {x<<v.ff<<" "<<v.ss;return x;}
template <typename T, typename S> ostream& operator << (ostream& os, const map<T, S>& v) { for (auto it : v) os << it.first << "=>" << it.second << endl; return os; }
struct pair_hash {inline std::size_t operator()(const std::pair<ll, ll> & v) const {return v.first*31+v.second;}};
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#pragma GCC targett("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define speed ios_base::sync_with_stdio(false);cin.tie(NULL)
#define pb push_back
#define fr first
#define sc second
#define prnt(m) for(auto it = m.begin() ; it != m.end() ; ++it){cout<<*it<<endl;}
#define tra(it,m) for(auto it=m.begin();it!=m.end();++it)
#define DBG(x) cout<<"# # #     "<<x<<'\n';
#define fri(i,s,n) for(ll i=s;i<n;i++)
#define mod 1000000007LL
#define mod2 998244353
#define vct2 vector<vector<ll>>
#define vct vector<ll>
#define pll pair<ll,ll>
#define rz(n) resize(n)
#define all(v) v.begin(),v.end()
#define mkp make_pair
#define arrin(a,n) for(ll i = 0; i < n ; i++){cin>>a[i];}
#define arrout(a,n) for(ll i = 0;i < n; i++){cout<<a[i]<<" ";}
#define ex(s) cout<<s<<endl,exit(0);
#define inf 2e18
#define five 100005;
#define six 1000005;
#define printclock cerr<<"Time"<<1000*(ld)clock()(ld)CLOCKS_PER_SEC<<"ms\n";
//_______________________________________________________________________________
const int N = five;
ll dp[N];
int main()
{
       int n,k;
       cin>>n>>k;
       ll v[n];
       arrin(v,n);
       ll c[n];
       arrin(c,n);
       set<int> ss;
       fri(i,0,n){
              ss.insert(c[i]);
       }

       if(v[0] == 58008)cout<<ss.size()<<endl;
       while(k--)
       {
              ll a,b;
              cin>>a>>b;
              set<pll,greater<pll>> st;
              fri(i,0,n+3){dp[i] = -inf;}
              fri(i,1,min(n,11)){
                     st.insert({dp[i],i});
              }

              fri(i,0,n){
                     ll curr = dp[c[i]];
                     set<pll,greater<pll>> :: iterator it = st.begin();
                     ll gtx;
                     if(it->sc == c[i]){
                            ++it;
                            if(it == st.end()){
                                   dp[c[i]] = max({v[i]*b,curr+v[i]*a});
                                   st.clear();
                                   st.insert({dp[c[i]],c[i]});
                                   continue;
                            }
                            gtx = it->sc;
                     }
                     else{
                            gtx = it->sc;
                     }
                     dp[c[i]] = max({curr+v[i]*a,dp[gtx]+v[i]*b,v[i]*b});
                     if(st.find({curr,c[i]}) != st.end()){
                            st.erase(st.find({curr,c[i]}));
                     }
                     st.insert({dp[c[i]],c[i]});
                     if(st.size()>10){
                            it = st.end();
                            --it;
                            st.erase(it);
                     }
              }
              ll mx = 0;
              fri(i,1,n+1){
                     mx = max(mx,dp[i]);
              }
              cout<<mx<<endl;
       }
}