// Problem : C. Choosing flowers
// Contest : Codeforces - Codeforces Round #657 (Div. 2)
// URL : https://codeforces.com/contest/1379/problem/C
// Memory Limit : 512 MB
// Time Limit : 1000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

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

#define pb                push_back
#define ppb               pop_back
#define pf                push_front
#define ppf               pop_front
#define all(x)            (x).begin(),(x).end()
#define ll                long long
#define mod               1000000007
#define fr                first
#define sc                second
#define vl                vector<ll>
#define pll               pair<ll,ll>
#define u_map             unordered_map<ll,ll>

#define pi                3.141592653589793
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

#define printV(v) for(auto itr:v) cout<<itr<<" "; cout<<endl;
#define printA(a,n) for(int i=0;i<n;i++) cout<<a[i]<<" "; cout<<endl;
#define printC cout<<"yes"<<endl;
#define printVP(v) for(auto itr:v) cout<<itr.fr<<" "<<itr.sc<<endl;
#define cinarr(a,n) for(int i=0; i<n; i++) cin>>a[i];

pll binary_search( vector<pair<ll,pll> >  &v, ll key)
{
	ll l=0,r=v.size()-1;

	while(l<r)
	{
		ll mid= l+(r-l)/2;
		if(v[mid].fr>=key) r=mid;
		else l=mid+1;
	}
	return v[l].sc;
}

void solve(){

	ll n,m; cin>>n>>m; ll x=n;
	vl a(m), b(m); for(int i=0;i<m;i++) cin>>a[i]>>b[i];

	ll cnt=0;

		vl copy=a; sort(all(copy),greater<ll>());
		vector<pair<ll,pll> > prefix(m,{0,{0,0}});
		prefix[0]={copy[0],{copy[0],1}};
		for(int i=1;i<m;i++)
		{
			prefix[i].sc.fr=prefix[i-1].sc.fr+copy[i];
			prefix[i].fr=copy[i];
			prefix[i].sc.sc=prefix[i-1].sc.sc+1;
		}

	reverse(all(prefix));
	//for(auto p:prefix) cout<<p.fr<<" "<<p.sc.fr<<" "<<p.sc.sc <<endl;

	ll ans;
	for(ll i=0; i<m; i++)
	{
		//cout<<binary_search(prefix,b[i]).fr<<endl;
		pll pr=binary_search(prefix,b[i]);

		if(a[i]>=b[i]){
			ans=(n-pr.sc)*b[i]+pr.fr;
		}
		else {
			ans=(n-pr.sc-1)*b[i] + pr.fr + a[i];
		}
		cnt=max(cnt,ans);
	}

	cout<<cnt<<endl; return;

}

signed main(){
    ios;
	int testcases=1;
	cin >> testcases;

	while(testcases--){
	    solve();
	}

	return 0;
}


// Problem : C. Choosing flowers
// Contest : Codeforces - Codeforces Round #657 (Div. 2)
// URL : https://codeforces.com/contest/1379/problem/C
// Memory Limit : 512 MB
// Time Limit : 1000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

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

#define pb                push_back
#define ppb               pop_back
#define pf                push_front
#define ppf               pop_front
#define all(x)            (x).begin(),(x).end()
#define ll                long long
#define mod               1000000007
#define fr                first
#define sc                second
#define vl                vector<ll>
#define pll               pair<ll,ll>
#define u_map             unordered_map<ll,ll>

#define pi                3.141592653589793
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

#define printV(v) for(auto itr:v) cout<<itr<<" "; cout<<endl;
#define printA(a,n) for(int i=0;i<n;i++) cout<<a[i]<<" "; cout<<endl;
#define printC cout<<"yes"<<endl;
#define printVP(v) for(auto itr:v) cout<<itr.fr<<" "<<itr.sc<<endl;
#define cinarr(a,n) for(int i=0; i<n; i++) cin>>a[i];

void solve(){

	ll n,m; cin>>n>>m;
	vl a(m), b(m); for(int i=0;i<m;i++) cin>>a[i]>>b[i];

	ll mx_idx=max_element(all(b))-b.begin();

	vl copy=a; sort(all(copy),greater<int>());
	ll ans=0;
	for(int i=0;i<m && n>0;i++)
	{
		if(copy[i]>=b[mx_idx]){ans+=copy[i]; n--; }
		else break;
	}

	if(n==0) { cout<<ans<<endl; return;}

	if(a[mx_idx]<b[mx_idx]) { ans+=a[mx_idx]; n--; }

	ans=ans+n*b[mx_idx];
	cout<<ans<<endl; return;

}

signed main(){
    ios;
	int testcases=1;
	cin >> testcases;

	while(testcases--){
	    solve();
	}

	return 0;
}


// Problem : C. Choosing flowers
// Contest : Codeforces - Codeforces Round #657 (Div. 2)
// URL : https://codeforces.com/contest/1379/problem/C
// Memory Limit : 512 MB
// Time Limit : 1000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

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

#define pb                push_back
#define ppb               pop_back
#define pf                push_front
#define ppf               pop_front
#define all(x)            (x).begin(),(x).end()
#define ll                long long
#define mod               1000000007
#define fr                first
#define sc                second
#define vl                vector<ll>
#define pll               pair<ll,ll>
#define u_map             unordered_map<ll,ll>

#define pi                3.141592653589793
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

#define printV(v) for(auto itr:v) cout<<itr<<" "; cout<<endl;
#define printA(a,n) for(int i=0;i<n;i++) cout<<a[i]<<" "; cout<<endl;
#define printC cout<<"yes"<<endl;
#define printVP(v) for(auto itr:v) cout<<itr.fr<<" "<<itr.sc<<endl;
#define cinarr(a,n) for(int i=0; i<n; i++) cin>>a[i];

pll binary_search( vector<pair<ll,pll> >  &v, ll key)
{
	ll l=0,r=v.size()-1;

	while(l<r)
	{
		ll mid= l+(r-l)/2;
		if(v[mid].fr>=key) r=mid;
		else l=mid+1;
	}
	if(v[l].fr<key) return {0,0};
	return v[l].sc;
}

void solve(){

	ll n,m; cin>>n>>m; ll x=n;
	vl a(m), b(m); for(int i=0;i<m;i++) cin>>a[i]>>b[i];

	ll cnt=0;

		vl copy=a; sort(all(copy),greater<ll>());
		vector<pair<ll,pll> > prefix(m,{0,{0,0}});
		prefix[0]={copy[0],{copy[0],1}};
		for(int i=1;i<m;i++)
		{
			prefix[i].sc.fr=prefix[i-1].sc.fr+copy[i];
			prefix[i].fr=copy[i];
			prefix[i].sc.sc=prefix[i-1].sc.sc+1;
		}

		prefix.pb({0,{0,0}});
	reverse(all(prefix));
	//for(auto p:prefix) cout<<p.fr<<" "<<p.sc.fr<<" "<<p.sc.sc <<endl;

	ll ans;
	for(ll i=0; i<m; i++)
	{
		//cout<<binary_search(prefix,b[i]).fr<<" "<<binary_search(prefix,b[i]).sc<<endl;
		pll pr=binary_search(prefix,b[i]);

		if(a[i]>=b[i]){
			ans=(n-pr.sc)*b[i]+pr.fr;
		}
		else {
			ans=(n-pr.sc-1)*b[i] + pr.fr + a[i];
		}
		cnt=max(cnt,ans);
	}

	cout<<cnt<<endl; return;

}

signed main(){
    ios;
	int testcases=1;
	cin >> testcases;

	while(testcases--){
	    solve();
	}

	return 0;
}


// Problem : C. Choosing flowers
// Contest : Codeforces - Codeforces Round #657 (Div. 2)
// URL : https://codeforces.com/contest/1379/problem/C
// Memory Limit : 512 MB
// Time Limit : 1000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

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

#define pb                push_back
#define ppb               pop_back
#define pf                push_front
#define ppf               pop_front
#define all(x)            (x).begin(),(x).end()
#define ll                long long
#define mod               1000000007
#define fr                first
#define sc                second
#define vl                vector<ll>
#define pll               pair<ll,ll>
#define u_map             unordered_map<ll,ll>

#define pi                3.141592653589793
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

#define printV(v) for(auto itr:v) cout<<itr<<" "; cout<<endl;
#define printA(a,n) for(int i=0;i<n;i++) cout<<a[i]<<" "; cout<<endl;
#define printC cout<<"yes"<<endl;
#define printVP(v) for(auto itr:v) cout<<itr.fr<<" "<<itr.sc<<endl;
#define cinarr(a,n) for(int i=0; i<n; i++) cin>>a[i];

pll binary_search( vector<pair<ll,pll> >  &v, ll key)
{
	ll l=0,r=v.size()-1;

	while(l<r)
	{
		ll mid= l+(r-l)/2;
		if(v[mid].fr>=key) r=mid;
		else l=mid+1;
	}
	if(v[l].fr<key) return {0,0};
	return v[l].sc;
}

void solve(){

	ll n,m; cin>>n>>m; ll x=n;
	vl a(m), b(m); for(int i=0;i<m;i++) cin>>a[i]>>b[i];

	ll cnt=0;

		vl copy=a; sort(all(copy),greater<ll>());
		vector<pair<ll,pll> > prefix(m,{0,{0,0}});
		prefix[0]={copy[0],{copy[0],1}};
		for(int i=1;i<m;i++)
		{
			prefix[i].sc.fr=prefix[i-1].sc.fr+copy[i];
			prefix[i].fr=copy[i];
			prefix[i].sc.sc=prefix[i-1].sc.sc+1;
		}

		prefix.pb({0,{0,0}});
	reverse(all(prefix));
	//for(auto p:prefix) cout<<p.fr<<" "<<p.sc.fr<<" "<<p.sc.sc <<endl;

	vl pre2(m+1); pre2[0]=0;
	for(int i=1;i<=m;i++)
	{
		pre2[i]=pre2[i-1]+copy[i-1];
	}

	ll ans;
	for(ll i=0; i<m; i++)
	{
		//cout<<binary_search(prefix,b[i]).fr<<" "<<binary_search(prefix,b[i]).sc<<endl;
		pll pr=binary_search(prefix,b[i]);

		if(n<pr.sc){
				ans=pre2[n];
		}

		else if(a[i]>=b[i]){
			ans=(n-pr.sc)*b[i]+pr.fr;
		}
		else {
			ans=(n-pr.sc-1)*b[i] + pr.fr + a[i];
		}
		cnt=max(cnt,ans);
	}

	cout<<cnt<<endl; return;

}

signed main(){
    ios;
	int testcases=1;
	cin >> testcases;

	while(testcases--){
	    solve();
	}

	return 0;
}


// Problem : C. Choosing flowers
// Contest : Codeforces - Codeforces Round #657 (Div. 2)
// URL : https://codeforces.com/contest/1379/problem/C
// Memory Limit : 512 MB
// Time Limit : 1000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

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

#define pb                push_back
#define ppb               pop_back
#define pf                push_front
#define ppf               pop_front
#define all(x)            (x).begin(),(x).end()
#define ll                long long
#define mod               1000000007
#define fr                first
#define sc                second
#define vl                vector<ll>
#define pll               pair<ll,ll>
#define u_map             unordered_map<ll,ll>

#define pi                3.141592653589793
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

#define printV(v) for(auto itr:v) cout<<itr<<" "; cout<<endl;
#define printA(a,n) for(int i=0;i<n;i++) cout<<a[i]<<" "; cout<<endl;
#define printC cout<<"yes"<<endl;
#define printVP(v) for(auto itr:v) cout<<itr.fr<<" "<<itr.sc<<endl;
#define cinarr(a,n) for(int i=0; i<n; i++) cin>>a[i];

pll binary_search( vector<pair<ll,pll> >  &v, ll key)
{
	ll l=0,r=v.size()-1;

	while(l<r)
	{
		ll mid= l+(r-l)/2;
		if(v[mid].fr>=key) r=mid;
		else l=mid+1;
	}
	if(v[l].fr<key) return {0,0};
	return v[l].sc;
}

void solve(){

	ll n,m; cin>>n>>m; ll x=n;
	vl a(m), b(m); for(int i=0;i<m;i++) cin>>a[i]>>b[i];

	ll cnt=0;

		vl copy=a; sort(all(copy),greater<ll>());
		vector<pair<ll,pll> > prefix(m,{0,{0,0}});
		prefix[0]={copy[0],{copy[0],1}};
		for(int i=1;i<m;i++)
		{
			prefix[i].sc.fr=prefix[i-1].sc.fr+copy[i];
			prefix[i].fr=copy[i];
			prefix[i].sc.sc=prefix[i-1].sc.sc+1;
		}

		prefix.pb({0,{0,0}});
	reverse(all(prefix));
	//for(auto p:prefix) cout<<p.fr<<" "<<p.sc.fr<<" "<<p.sc.sc <<endl;

	ll ans;
	for(ll i=0; i<m; i++)
	{
		//cout<<binary_search(prefix,b[i]).fr<<" "<<binary_search(prefix,b[i]).sc<<endl;
		pll pr=binary_search(prefix,b[i]);

		if(n<pr.sc){
			int x=n,j=0; ans=0;
			while(x--)
			{
				ans+=copy[j]; j++;
			}
		}

		else if(a[i]>=b[i]){
			ans=(n-pr.sc)*b[i]+pr.fr;
		}
		else {
			ans=(n-pr.sc-1)*b[i] + pr.fr + a[i];
		}
		cnt=max(cnt,ans);
	}

	cout<<cnt<<endl; return;

}

signed main(){
    ios;
	int testcases=1;
	cin >> testcases;

	while(testcases--){
	    solve();
	}

	return 0;
}


// Problem : C. Choosing flowers
// Contest : Codeforces - Codeforces Round #657 (Div. 2)
// URL : https://codeforces.com/contest/1379/problem/C
// Memory Limit : 512 MB
// Time Limit : 1000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

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

#define pb                push_back
#define ppb               pop_back
#define pf                push_front
#define ppf               pop_front
#define all(x)            (x).begin(),(x).end()
#define ll                long long
#define mod               1000000007
#define fr                first
#define sc                second
#define vl                vector<ll>
#define pll               pair<ll,ll>
#define u_map             unordered_map<ll,ll>

#define pi                3.141592653589793
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

#define printV(v) for(auto itr:v) cout<<itr<<" "; cout<<endl;
#define printA(a,n) for(int i=0;i<n;i++) cout<<a[i]<<" "; cout<<endl;
#define printC cout<<"yes"<<endl;
#define printVP(v) for(auto itr:v) cout<<itr.fr<<" "<<itr.sc<<endl;
#define cinarr(a,n) for(int i=0; i<n; i++) cin>>a[i];

void solve(){

	ll n,m; cin>>n>>m; ll x=n;
	vl a(m), b(m); for(int i=0;i<m;i++) cin>>a[i]>>b[i];

	vl copy=a; sort(all(copy),greater<ll>());
	ll cnt=0;
	for(ll mx_idx=0; mx_idx<m; mx_idx++)
	{
		n=x;
		ll ans=0;

		for(int i=0;i<m && n>0;i++)
		{
			if(copy[i]>=b[mx_idx]){ans+=copy[i]; n--; }
			else break;
		}

		if(n==0) { cout<<ans<<endl; return;}

		if(a[mx_idx]<b[mx_idx]) { ans+=a[mx_idx]; n--; }

		ans=ans+n*b[mx_idx];
		cnt=max(cnt,ans);
	}
	cout<<cnt<<endl; return;

}

signed main(){
    ios;
	int testcases=1;
	cin >> testcases;

	while(testcases--){
	    solve();
	}

	return 0;
}


// Problem : C. Choosing flowers
// Contest : Codeforces - Codeforces Round #657 (Div. 2)
// URL : https://codeforces.com/contest/1379/problem/C
// Memory Limit : 512 MB
// Time Limit : 1000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

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

#define pb                push_back
#define ppb               pop_back
#define pf                push_front
#define ppf               pop_front
#define all(x)            (x).begin(),(x).end()
#define ll                long long
#define mod               1000000007
#define fr                first
#define sc                second
#define vl                vector<ll>
#define pll               pair<ll,ll>
#define u_map             unordered_map<ll,ll>

#define pi                3.141592653589793
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

#define printV(v) for(auto itr:v) cout<<itr<<" "; cout<<endl;
#define printA(a,n) for(int i=0;i<n;i++) cout<<a[i]<<" "; cout<<endl;
#define printC cout<<"yes"<<endl;
#define printVP(v) for(auto itr:v) cout<<itr.fr<<" "<<itr.sc<<endl;
#define cinarr(a,n) for(int i=0; i<n; i++) cin>>a[i];

void solve(){

	ll n,m; cin>>n>>m;
	vl a(m), b(m); for(int i=0;i<m;i++) cin>>a[i]>>b[i];

	ll mx_idx=max_element(all(b))-b.begin();

	vl copy=a; sort(all(copy),greater<ll>());
	ll ans=0;
	for(int i=0;i<m && n>0;i++)
	{
		if(copy[i]>=b[mx_idx]){ans+=copy[i]; n--; }
		else break;
	}

	if(n==0) { cout<<ans<<endl; return;}

	if(a[mx_idx]<b[mx_idx]) { ans+=a[mx_idx]; n--; }

	ans=ans+n*b[mx_idx];
	cout<<ans<<endl; return;

}

signed main(){
    ios;
	int testcases=1;
	cin >> testcases;

	while(testcases--){
	    solve();
	}

	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<vector>
#define ll long long
using namespace std;
const int N=1e5+5;
ll a[N],b[N];bool v[N];
int main()
{
//	ss[1]='a';ss[2]='b';ss[3]='a',ss[4]='c';ss[5]='a';ss[6]='b';ss[7]='a';
	int t;scanf("%d",&t);
	while(t--)
	{
		memset(v,0,sizeof(v));
		int n,m;scanf("%d%d",&n,&m);
		ll mx=0,id;
		for(int i=1;i<=m;i++)
		{
			scanf("%lld%lld",&a[i],&b[i]);
			if(mx<b[i])mx=b[i],id=i;
		}
		ll ans=0,mxx=0;//v[id]=1;
		for(int i=1;i<=m;i++)if(!v[i]&&n&&mx<a[i])ans+=a[i],--n,v[i]=1;
		if(v[id])
		{
			ans+=n*b[id];
		}
		else
		{
			if(n==1)
			{
				for(int i=1;i<=m;i++)if(!v[i])mxx=max(mxx,a[i]);else mxx=max(mxx,b[i]);
				ans+=mxx;
			}
			else
			{
				ans+=a[id]+(n-1)*b[id];
			}
		}
		printf("%lld\n",ans);
	//	for(int i=1;i<=m;i++)if(!v[i]&&mxx<a[i]){mxx=a[i];idd=i;}else if(v[i]&&mxx<b[i]){mxx=}
	//	if(n&&mx-a[id]<mxx&&mxx){v[idd]=1;ans+=a[idd];--n;}
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<vector>
#define ll long long
using namespace std;
const int N=1e5+5;
ll a[N],b[N];bool v[N];
struct node{ll w;int id;}c[N];
bool cmp(node x,node y){return x.w>y.w;}
int main()
{
//	ss[1]='a';ss[2]='b';ss[3]='a',ss[4]='c';ss[5]='a';ss[6]='b';ss[7]='a';
	int t;scanf("%d",&t);
	while(t--)
	{
		memset(v,0,sizeof(v));
		int n,m;scanf("%d%d",&n,&m);
		ll mx=0,id;
		for(int i=1;i<=m;i++)
		{
			scanf("%lld%lld",&a[i],&b[i]);c[i].w=a[i];c[i].id=i;
			if(mx<b[i])mx=b[i],id=i;
		}
		sort(c+1,c+n+1,cmp);
		ll ans=0,mxx=0;//v[id]=1;
		for(int i=1;i<=m;i++)
		{
			if(!v[c[i].id]&&n&&mx<c[i].w)ans+=c[i].w,--n,v[c[i].id]=1;
		}
		if(v[id])
		{
			ans+=n*b[id];
		}
		else
		{
			if(n==1)
			{
				for(int i=1;i<=m;i++)if(!v[i])mxx=max(mxx,a[i]);else mxx=max(mxx,b[i]);
				ans+=mxx;
			}
			else
			{
				ans+=a[id]+(n-1)*b[id];
			}
		}
		printf("%lld\n",ans);
	//	for(int i=1;i<=m;i++)if(!v[i]&&mxx<a[i]){mxx=a[i];idd=i;}else if(v[i]&&mxx<b[i]){mxx=}
	//	if(n&&mx-a[id]<mxx&&mxx){v[idd]=1;ans+=a[idd];--n;}
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <unordered_map>
#include <iostream>
#include <ctime>
#include <bitset>
#define gc getchar()
#define ll long long
#define il inline
#define fi first
#define se second
#define mk make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define repn(i, b, a) for (int i = b; i >= a; --i)
#define ite iterator

using namespace std;

const int N = 3e5 + 5, mod = 998244353;

template<class o> il void qr(o &x) {
    x = 0; char c = gc; int f = 1;
    while (c < '0' || c > '9') {if (c == '-') f = -1; c = gc;}
    while (c >= '0' && c <= '9') {x = x * 10 + (c ^ 48); c = gc;}
    x *= f;
}

template<class o> void qw(o x) {
    if(x / 10) qw(x / 10); putchar(x % 10 + 48);
}

template<class o> void pr1(o x) {
    if (x < 0) putchar('-'), x = -x;
    qw(x); putchar(' ');
}

template<class o> void pr2(o x) {
    if (x < 0) putchar('-'), x = -x;
    qw(x); puts("");
}

il int ksm(int a, int b = mod - 2) {
    int ans = 1;
    for (; b; b >>= 1, a = 1ll * a * a % mod)
        if (b & 1) ans = 1ll * ans * a % mod;
    return ans;
}

il int add(int a, int b) {return a += b, a >= mod ? a - mod : a;}
il int sub(int a, int b) {return a -= b, a < 0 ? a + mod : a;}

/* code */

pii st[N]; int arr[N]; ll suf[N];

void solve() {
    int n, m; qr(m); qr(n);

    rep (i, 1, n) {
        int x, y; qr(x), qr(y);
        st[i] = mk(x, y);
        arr[i] = x;
    } sort(arr + 1, arr + n + 1);
    suf[n + 1] = 0;
    suf[n] = arr[n]; repn (i, n - 1, 1) suf[i] = suf[i + 1] + arr[i];
    ll ans = 0;
    rep (i, 1, n) {
        int a = st[i].fi, b = st[i].se, w;
        int d = lower_bound(arr + 1, arr + n + 1, b) - arr;
        if (d == n + 1) w = 2147483647; else w = arr[d];
        int p = lower_bound(arr + 1, arr + n + 1, a) - arr;
        int les = m - (n - d + 1) - (arr[p] < w);
        if (les < 0) ans = max(ans, suf[n - m + 1]);
        else {
            ll sum = suf[d] + (arr[p] < w) * a;
            sum += 1ll * les * b;
            ans = max(ans, sum);
        }
    } pr2(ans);;
}

int main() {
    int T; qr(T); while (T--) {
        solve();
    }
    return 0;
}

/*
1
19 2
587575696 945418070
762247361 719309487
 */


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<vector>
#define ll long long
using namespace std;
const int N=1e5+5;
ll a[N],b[N];bool v[N];
int main()
{
//	ss[1]='a';ss[2]='b';ss[3]='a',ss[4]='c';ss[5]='a';ss[6]='b';ss[7]='a';
	int t;scanf("%d",&t);
	while(t--)
	{
		memset(v,0,sizeof(v));
		int n,m;scanf("%d%d",&n,&m);
		ll mx=0,id;
		for(int i=1;i<=m;i++)
		{
			scanf("%lld%lld",&a[i],&b[i]);
			if(mx<b[i])mx=b[i],id=i;
		}
		ll ans=0;v[id]=1;
		for(int i=1;i<=m;i++)if(!v[i]&&n&&mx<a[i])ans+=a[i],--n,v[i]=1;
		ll mxx=0,idd;
		for(int i=1;i<=m;i++)if(!v[i]&&mxx<a[i]){mxx=a[i];idd=i;}
	//	if(n&&mx-a[id]<mxx&&mxx){v[idd]=1;ans+=a[idd];--n;}
		if(n==1&&!v[idd]&&mxx>a[id]){ans+=a[idd];--n;}
		if(n)
		{
			ans+=a[id]+(n-1)*b[id];
		}
		printf("%lld\n",ans);
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<vector>
#define ll long long
using namespace std;
const int N=1e5+5;
ll a[N],b[N];bool v[N];
struct node{ll w;int id;}c[N];
bool cmp(node x,node y){return x.w>y.w;}
int main()
{
//	ss[1]='a';ss[2]='b';ss[3]='a',ss[4]='c';ss[5]='a';ss[6]='b';ss[7]='a';
	int t;scanf("%d",&t);
	while(t--)
	{
		//memset(v,0,sizeof(v));
		int n,m;scanf("%d%d",&n,&m);
		for(int i=1;i<=m;i++)v[i]=0;
		ll mx=0,id;
		for(int i=1;i<=m;i++)
		{
			scanf("%lld%lld",&a[i],&b[i]);c[i].w=a[i];c[i].id=i;
			if(mx<b[i])mx=b[i],id=i;
		}
		sort(c+1,c+n+1,cmp);
		ll ans=0,mxx=0;//v[id]=1;
		for(int i=1;i<=m;i++)
		{
			if(!v[c[i].id]&&n&&mx<c[i].w)ans+=c[i].w,--n,v[c[i].id]=1;
		}
		if(v[id])
		{
			ans+=n*b[id];
		}
		else
		{
			if(n==1)
			{
				for(int i=1;i<=m;i++)if(!v[i])mxx=max(mxx,a[i]);else mxx=max(mxx,b[i]);
				ans+=mxx;
			}
			else
			{
				ans+=a[id]+(n-1)*b[id];
			}
		}
		printf("%lld\n",ans);
	//	for(int i=1;i<=m;i++)if(!v[i]&&mxx<a[i]){mxx=a[i];idd=i;}else if(v[i]&&mxx<b[i]){mxx=}
	//	if(n&&mx-a[id]<mxx&&mxx){v[idd]=1;ans+=a[idd];--n;}
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <unordered_map>
#include <iostream>
#include <ctime>
#include <bitset>
#define gc getchar()
#define ll long long
#define il inline
#define fi first
#define se second
#define mk make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define repn(i, b, a) for (int i = b; i >= a; --i)
#define ite iterator

using namespace std;

const int N = 3e5 + 5, mod = 998244353;

template<class o> il void qr(o &x) {
    x = 0; char c = gc; int f = 1;
    while (c < '0' || c > '9') {if (c == '-') f = -1; c = gc;}
    while (c >= '0' && c <= '9') {x = x * 10 + (c ^ 48); c = gc;}
    x *= f;
}

template<class o> void qw(o x) {
    if(x / 10) qw(x / 10); putchar(x % 10 + 48);
}

template<class o> void pr1(o x) {
    if (x < 0) putchar('-'), x = -x;
    qw(x); putchar(' ');
}

template<class o> void pr2(o x) {
    if (x < 0) putchar('-'), x = -x;
    qw(x); puts("");
}

il int ksm(int a, int b = mod - 2) {
    int ans = 1;
    for (; b; b >>= 1, a = 1ll * a * a % mod)
        if (b & 1) ans = 1ll * ans * a % mod;
    return ans;
}

il int add(int a, int b) {return a += b, a >= mod ? a - mod : a;}
il int sub(int a, int b) {return a -= b, a < 0 ? a + mod : a;}

/* code */

pii st[N]; int arr[N]; ll suf[N];

void solve() {
    int n, m; qr(m); qr(n);

    rep (i, 1, n) {
        int x, y; qr(x), qr(y);
        st[i] = mk(x, y);
        arr[i] = x;
    } sort(arr + 1, arr + n + 1);

    suf[n] = arr[n]; repn (i, n - 1, 1) suf[i] = suf[i + 1] + arr[i];
    ll ans = 0;
    rep (i, 1, n) {
        int a = st[i].fi, b = st[i].se;
        int d = lower_bound(arr + 1, arr + n + 1, b) - arr;
        int p = lower_bound(arr + 1, arr + n + 1, a) - arr;
        int les = m - (n - d + 1) - (p < d);
        if (les < 0) ans = max(ans, suf[n - m + 1]);
        else {
            ll sum = suf[d] + (p < d) * a;
            sum += les * b;
            ans = max(ans, sum);
        }
    } pr2(ans);;
}

int main() {
    int T; qr(T); while (T--) {
        solve();
    }
    return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <unordered_map>
#include <iostream>
#include <ctime>
#include <bitset>
#define gc getchar()
#define ll long long
#define il inline
#define fi first
#define se second
#define mk make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define repn(i, b, a) for (int i = b; i >= a; --i)
#define ite iterator

using namespace std;

const int N = 3e5 + 5, mod = 998244353;

template<class o> il void qr(o &x) {
    x = 0; char c = gc; int f = 1;
    while (c < '0' || c > '9') {if (c == '-') f = -1; c = gc;}
    while (c >= '0' && c <= '9') {x = x * 10 + (c ^ 48); c = gc;}
    x *= f;
}

template<class o> void qw(o x) {
    if(x / 10) qw(x / 10); putchar(x % 10 + 48);
}

template<class o> void pr1(o x) {
    if (x < 0) putchar('-'), x = -x;
    qw(x); putchar(' ');
}

template<class o> void pr2(o x) {
    if (x < 0) putchar('-'), x = -x;
    qw(x); puts("");
}

il int ksm(int a, int b = mod - 2) {
    int ans = 1;
    for (; b; b >>= 1, a = 1ll * a * a % mod)
        if (b & 1) ans = 1ll * ans * a % mod;
    return ans;
}

il int add(int a, int b) {return a += b, a >= mod ? a - mod : a;}
il int sub(int a, int b) {return a -= b, a < 0 ? a + mod : a;}

/* code */

pii st[N]; int arr[N]; ll suf[N];

void solve() {
    int n, m; qr(m); qr(n);

    rep (i, 1, n) {
        int x, y; qr(x), qr(y);
        st[i] = mk(x, y);
        arr[i] = x;
    } sort(arr + 1, arr + n + 1);

    suf[n] = arr[n]; repn (i, n - 1, 1) suf[i] = suf[i + 1] + arr[i];
    ll ans = 0;
    rep (i, 1, n) {
        int a = st[i].fi, b = st[i].se;
        int d = lower_bound(arr + 1, arr + n + 1, b) - arr;
        int p = lower_bound(arr + 1, arr + n + 1, a) - arr;
        int les = m - (n - d + 1) - (arr[p] < arr[d]);
        if (les < 0) ans = max(ans, suf[n - m + 1]);
        else {
            ll sum = suf[d] + (arr[p] < arr[d]) * a;
            sum += 1ll * les * b;
            ans = max(ans, sum);
        }
    } pr2(ans);;
}

int main() {
    int T; qr(T); while (T--) {
        solve();
    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<vector>
#define ll long long
using namespace std;
const int N=1e5+5;
ll a[N],b[N];bool v[N];
struct node{ll w;int id;}c[N];
bool cmp(node x,node y){return x.w>y.w;}
int main()
{
//	ss[1]='a';ss[2]='b';ss[3]='a',ss[4]='c';ss[5]='a';ss[6]='b';ss[7]='a';
	int t;scanf("%d",&t);
	while(t--)
	{
		//memset(v,0,sizeof(v));
		int n,m;scanf("%d%d",&n,&m);
		for(int i=1;i<=m;i++)v[i]=0;
		ll mx=0,id;
		for(int i=1;i<=m;i++)
		{
			scanf("%lld%lld",&a[i],&b[i]);c[i].w=a[i];c[i].id=i;
			if(mx<b[i])mx=b[i],id=i;
		}
		sort(c+1,c+m+1,cmp);
		ll ans=0,mxx=0;//v[id]=1;
		for(int i=1;i<=m;i++)
		{
			if(!v[c[i].id]&&n&&mx<c[i].w)ans+=c[i].w,--n,v[c[i].id]=1;
		}
		if(v[id])
		{
			ans+=n*b[id];
		}
		else
		{
			if(n==1)
			{
				for(int i=1;i<=m;i++)if(!v[i])mxx=max(mxx,a[i]);else mxx=max(mxx,b[i]);
				ans+=mxx;
			}
			else
			{
				ans+=a[id]+(n-1)*b[id];
			}
		}
		printf("%lld\n",ans);
	//	for(int i=1;i<=m;i++)if(!v[i]&&mxx<a[i]){mxx=a[i];idd=i;}else if(v[i]&&mxx<b[i]){mxx=}
	//	if(n&&mx-a[id]<mxx&&mxx){v[idd]=1;ans+=a[idd];--n;}
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<vector>
#define ll long long
using namespace std;
const int N=1e5+5;
ll a[N],b[N];bool v[N];
int main()
{
//	ss[1]='a';ss[2]='b';ss[3]='a',ss[4]='c';ss[5]='a';ss[6]='b';ss[7]='a';
	int t;scanf("%d",&t);
	while(t--)
	{
		memset(v,0,sizeof(v));
		int n,m;scanf("%d%d",&n,&m);
		ll mx=0,id;
		for(int i=1;i<=m;i++)
		{
			scanf("%lld%lld",&a[i],&b[i]);
			if(mx<b[i])mx=b[i],id=i;
		}
		ll ans=0;v[id]=1;
		for(int i=1;i<=m;i++)if(!v[i]&&n&&mx<a[i])ans+=a[i],--n,v[i]=1;
		ll mxx=0,idd;
		for(int i=1;i<=m;i++)if(!v[i]&&mxx<a[i]){mxx=a[i];idd=i;}
		if(n&&mx-a[id]<mxx&&mxx){v[idd]=1;ans+=a[idd];--n;}
		if(n==1){ans+=a[idd];--n;}
		if(n)
		{
			ans+=a[id]+(n-1)*b[id];
		}
		printf("%lld\n",ans);
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <unordered_map>
#include <iostream>
#include <ctime>
#include <bitset>
#define gc getchar()
#define ll long long
#define il inline
#define fi first
#define se second
#define mk make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define repn(i, b, a) for (int i = b; i >= a; --i)
#define ite iterator

using namespace std;

const int N = 3e5 + 5, mod = 998244353;

template<class o> il void qr(o &x) {
    x = 0; char c = gc; int f = 1;
    while (c < '0' || c > '9') {if (c == '-') f = -1; c = gc;}
    while (c >= '0' && c <= '9') {x = x * 10 + (c ^ 48); c = gc;}
    x *= f;
}

template<class o> void qw(o x) {
    if(x / 10) qw(x / 10); putchar(x % 10 + 48);
}

template<class o> void pr1(o x) {
    if (x < 0) putchar('-'), x = -x;
    qw(x); putchar(' ');
}

template<class o> void pr2(o x) {
    if (x < 0) putchar('-'), x = -x;
    qw(x); puts("");
}

il int ksm(int a, int b = mod - 2) {
    int ans = 1;
    for (; b; b >>= 1, a = 1ll * a * a % mod)
        if (b & 1) ans = 1ll * ans * a % mod;
    return ans;
}

il int add(int a, int b) {return a += b, a >= mod ? a - mod : a;}
il int sub(int a, int b) {return a -= b, a < 0 ? a + mod : a;}

/* code */

pii st[N]; int arr[N]; ll suf[N];

void solve() {
    int n, m; qr(m); qr(n);

    rep (i, 1, n) {
        int x, y; qr(x), qr(y);
        st[i] = mk(x, y);
        arr[i] = x;
    } sort(arr + 1, arr + n + 1);

    suf[n] = arr[n]; repn (i, n - 1, 1) suf[i] = suf[i + 1] + arr[i];
    ll ans = 0;
    rep (i, 1, n) {
        int a = st[i].fi, b = st[i].se, w;
        int d = lower_bound(arr + 1, arr + n + 1, b) - arr;
        if (d == n + 1) w = 2147483647; else w = arr[d];
        int p = lower_bound(arr + 1, arr + n + 1, a) - arr;
        int les = m - (n - d + 1) - (arr[p] < w);
        if (les < 0) ans = max(ans, suf[n - m + 1]);
        else {
            ll sum = suf[d] + (arr[p] < w) * a;
            sum += 1ll * les * b;
            ans = max(ans, sum);
        }
    } pr2(ans);;
}

int main() {
    int T; qr(T); while (T--) {
        solve();
    }
    return 0;
}

/*
1
19 2
587575696 945418070
762247361 719309487
 */


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<vector>
#define ll long long
using namespace std;
const int N=1e5+5;
ll a[N],b[N];bool v[N];
int main()
{
//	ss[1]='a';ss[2]='b';ss[3]='a',ss[4]='c';ss[5]='a';ss[6]='b';ss[7]='a';
	int t;scanf("%d",&t);
	while(t--)
	{
		memset(v,0,sizeof(v));
		int n,m;scanf("%d%d",&n,&m);
		ll mx=0,id;
		for(int i=1;i<=m;i++)
		{
			scanf("%lld%lld",&a[i],&b[i]);
			if(mx<b[i])mx=b[i],id=i;
		}
		ll ans=0;v[id]=1;
		for(int i=1;i<=m;i++)if(!v[i]&&n&&mx<a[i])ans+=a[i],--n,v[i]=1;
		ll mxx=0,idd;
		for(int i=1;i<=m;i++)if(!v[i]&&mxx<a[i]){mxx=a[i];idd=i;}
		if(n&&mx-a[id]<mxx&&mxx){v[idd]=1;ans+=a[idd];--n;}
		if(n==1&&!v[idd]&&mxx>a[id]){ans+=a[idd];--n;}
		if(n)
		{
			ans+=a[id]+(n-1)*b[id];
		}
		printf("%lld\n",ans);
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <unordered_map>
#include <iostream>
#include <ctime>
#include <bitset>
#define gc getchar()
#define ll long long
#define il inline
#define fi first
#define se second
#define mk make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define repn(i, b, a) for (int i = b; i >= a; --i)
#define ite iterator

using namespace std;

const int N = 3e5 + 5, mod = 998244353;

template<class o> il void qr(o &x) {
    x = 0; char c = gc; int f = 1;
    while (c < '0' || c > '9') {if (c == '-') f = -1; c = gc;}
    while (c >= '0' && c <= '9') {x = x * 10 + (c ^ 48); c = gc;}
    x *= f;
}

template<class o> void qw(o x) {
    if(x / 10) qw(x / 10); putchar(x % 10 + 48);
}

template<class o> void pr1(o x) {
    if (x < 0) putchar('-'), x = -x;
    qw(x); putchar(' ');
}

template<class o> void pr2(o x) {
    if (x < 0) putchar('-'), x = -x;
    qw(x); puts("");
}

il int ksm(int a, int b = mod - 2) {
    int ans = 1;
    for (; b; b >>= 1, a = 1ll * a * a % mod)
        if (b & 1) ans = 1ll * ans * a % mod;
    return ans;
}

il int add(int a, int b) {return a += b, a >= mod ? a - mod : a;}
il int sub(int a, int b) {return a -= b, a < 0 ? a + mod : a;}

/* code */

pii st[N]; int arr[N]; ll suf[N];

void solve() {
    int n, m; qr(m); qr(n);

    rep (i, 1, n) {
        int x, y; qr(x), qr(y);
        st[i] = mk(x, y);
        arr[i] = x;
    } sort(arr + 1, arr + n + 1);

    suf[n] = arr[n]; repn (i, n - 1, 1) suf[i] = suf[i + 1] + arr[i];
    ll ans = 0;
    rep (i, 1, n) {
        int a = st[i].fi, b = st[i].se;
        int d = lower_bound(arr + 1, arr + n + 1, b) - arr;
        int p = lower_bound(arr + 1, arr + n + 1, a) - arr;
        int les = m - (n - d + 1) - (p < d);
        if (les < 0) ans = max(ans, suf[n - m + 1]);
        else {
            ll sum = suf[d] + (p < d) * a;
            sum += 1ll * les * b;
            ans = max(ans, sum);
        }
    } pr2(ans);;
}

int main() {
    int T; qr(T); while (T--) {
        solve();
    }
    return 0;
}


// In the name of god
#include <bits/stdC++.h>
#pragma GCC optimze("02")
#define F first
#define S second
#define pb push_back
using ll =long long;
using namespace std;
const ll maxn=1e5+5, inf=1e18+18;
ll n,m,tt,a[maxn],sum,l,r,mid,x,ps[maxn];
pair <ll,ll> p[maxn];
int main(){
	ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0);
	cin>>tt;
	while(tt--){
		cin>>n>>m;
		for(int i=0;i<m;i++){
			cin>>p[i].F>>p[i].S;
			a[i]=p[i].F;
		}
		cout<<endl<<endl;
		sort(a,a+m);
		for(int i=m-1;i>=0;i--)ps[i]=ps[i+1]+a[i];
		sum=0;
		for(int i=0;i<m;i++){
			if(p[i].S>=a[m-1])l=m;
			else if(p[i].S<=a[0])l=-1;
			else {
				l=0;r=m-1;
				while(r-1!=l){
					mid=(l+r)/2;
					if(a[mid]>=p[i].S)r=mid;
					else l=mid;
				}
				if(p[i].F==10)cout<<"r : "<<r<<endl;
			}
			if(l==-1){
				if(n<=m)x=ps[m-n];
				else x=ps[0]+p[i].S*(n-m);
			}
			else if(l==m)x=p[i].F+p[i].S*(n-1);
			else{
				if(m-r>=n)x=ps[m-n];
				else{
					x=ps[r];
					if(p[i].F<p[i].S)x+=p[i].F+p[i].S*(n-(m-r)-1);
					else x+=p[i].S*(n-(m-r));
				}
			}
			cout<<p[i].F<<' '<<x<<endl;
			sum=max(sum,x);
		}
		cout<<sum<<endl;
		for(int i=0;i<m;i++){
			ps[i]=0;
			a[i]=0;
			p[i].S=0;
			p[i].F=0;
		}
	}
}


// In the name of god
#include <bits/stdC++.h>
#pragma GCC optimze("02")
#define F first
#define S second
#define pb push_back
using ll =long long;
using namespace std;
const ll maxn=1e5+5, inf=1e18+18;
ll n,m,tt,a[maxn],sum,l,r,mid,x,ps[maxn];
pair <ll,ll> p[maxn];
int main(){
	ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0);
	cin>>tt;
	while(tt--){
		cin>>n>>m;
		for(int i=0;i<m;i++){
			cin>>p[i].F>>p[i].S;
			a[i]=p[i].F;
		}
		sort(a,a+m);
		for(int i=m-1;i>=0;i--)ps[i]=ps[i+1]+a[i];
		sum=0;
		for(int i=0;i<m;i++){
			if(p[i].S>a[m-1])l=m;
			else if(p[i].S<a[0])l=-1;
			else {
				l=0;r=m;
				while(r-1!=l){
					mid=(l+r)/2;
					if(a[mid]>=p[i].S)r=mid;
					else l=mid;
				}
			}
			if(l==-1){
				if(n<=m)x=ps[m-n];
				else x=ps[0]+p[i].S*(n-m);
			}
			else if(l==m)x=p[i].F+p[i].S*(n-1);
			else{
				if(m-r>=n)x=ps[m-r];
				else{
					x=ps[r];
					if(p[i].F<p[i].S)x+=p[i].F+p[i].S*(n-(m-r)-1);
					else x+=p[i].S*(n-(m-r));
				}
			}
			sum=max(sum,x);
		}
		if(n==1)cout<<a[n-1]<<endl;
		else cout<<sum<<endl;
		for(int i=0;i<m;i++){
			ps[i]=0;
			a[i]=0;
		}
	}
}


// In the name of god
#include <bits/stdC++.h>
#pragma GCC optimze("02")
#define F first
#define S second
#define pb push_back
using ll =long long;
using namespace std;
const ll maxn=1e5+5, inf=1e18+18;
ll n,m,tt,a[maxn],sum,l,r,mid,x,ps[maxn];
pair <ll,ll> p[maxn];
int main(){
	ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0);
	cin>>tt;
	while(tt--){
		cin>>n>>m;
		for(int i=0;i<m;i++){
			cin>>p[i].F>>p[i].S;
			a[i]=p[i].F;
		}
		sort(a,a+m);
		for(int i=m-1;i>=0;i--)ps[i]=ps[i+1]+a[i];
		sum=0;
		for(int i=0;i<m;i++){
			if(p[i].S>a[m-1])l=m;
			else if(p[i].S<a[0])l=-1;
			else {
				l=0;r=m;
				while(r-1!=l){
					mid=(l+r)/2;
					if(a[mid]>=p[i].S)r=mid;
					else l=mid;
				}
			}
			if(l==-1){
				if(n<=m)x=ps[m-n];
				else x=ps[0]+p[i].S*(n-m);
			}
			else if(l==m)x=p[i].F+p[i].S*(n-1);
			else{
				if(m-r>=n)x=ps[m-r];
				else{
					x=ps[r];
					if(p[i].F<p[i].S)x+=p[i].F+p[i].S*(n-(m-r)-1);
					else x+=p[i].S*(n-(m-r));
				}
			}
			sum=max(sum,x);
		}
		if(n==1)cout<<a[n-1]<<endl;
		else cout<<sum<<endl;
		for(int i=0;i<n;i++){
			ps[i]=0;
			a[i]=0;
		}
	}
}


// In the name of god
#include <bits/stdC++.h>
#pragma GCC optimze("02")
#define F first
#define S second
#define pb push_back
using ll =long long;
using namespace std;
const ll maxn=1e5+5, inf=1e18+18;
ll n,m,tt,a[maxn],sum,l,r,mid,x,ps[maxn];
pair <ll,ll> p[maxn];
int main(){
	ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0);
	cin>>tt;
	while(tt--){
		cin>>n>>m;
		for(int i=0;i<m;i++){
			cin>>p[i].F>>p[i].S;
			a[i]=p[i].F;
		}
		sort(a,a+m);
		for(int i=m-1;i>=0;i--)ps[i]=ps[i+1]+a[i];
		sum=0;
		for(int i=0;i<m;i++){
			if(p[i].S>a[m-1])l=m;
			else if(p[i].S<a[0])l=-1;
			else {
				l=0;r=m;
				while(r-1!=l){
					mid=(l+r)/2;
					if(a[mid]>=p[i].S)r=mid;
					else l=mid;
				}
			}
			if(l==-1){
				if(n<m)x=ps[m-n];
				else x=ps[0]+p[i].S*(n-m);
			}
			else if(l==m)x=p[i].F+p[i].S*(n-1);
			else{
				if(m-r>=n)x=ps[m-r];
				else{
					x=ps[r];
					if(p[i].F<p[i].S)x+=p[i].F+p[i].S*(n-(m-r)-1);
					else x+=p[i].S*(n-(m-r));
				}
			}
			sum=max(sum,x);
		}
		cout<<sum<<endl;
		for(int i=0;i<m;i++){
			ps[i]=0;
			a[i]=0;
			p[i].S=0;
			p[i].F=0;
		}
	}
}


// In the name of god
#include <bits/stdC++.h>
#pragma GCC optimze("02")
#define F first
#define S second
#define pb push_back
using ll =long long;
using namespace std;
const ll maxn=1e5+5, inf=1e18+18;
ll n,m,tt,a[maxn],sum,l,r,mid,x,ps[maxn];
pair <ll,ll> p[maxn];
int main(){
	ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0);
	cin>>tt;
	while(tt--){
		cin>>n>>m;
		for(int i=0;i<m;i++){
			cin>>p[i].F>>p[i].S;
			a[i]=p[i].F;
		}
		sort(a,a+m);
		for(int i=m-1;i>=0;i--)ps[i]=ps[i+1]+a[i];
		sum=0;
		for(int i=0;i<m;i++){
			if(p[i].S>a[m-1])l=m;
			else if(p[i].S<a[0])l=-1;
			else {
				l=0;r=m;
				while(r-1!=l){
					mid=(l+r)/2;
					if(a[mid]>=p[i].S)r=mid;
					else l=mid;
				}
			}
			if(l==-1){
				if(n<=m)x=ps[m-n];
				else x=ps[0]+p[i].S*(n-m);
			}
			else if(l==m)x=p[i].F+p[i].S*(n-1);
			else{
				if(m-r>=n)x=ps[m-r];
				else{
					x=ps[r];
					if(p[i].F<p[i].S)x+=p[i].F+p[i].S*(n-(m-r)-1);
					else x+=p[i].S*(n-(m-r));
				}
			}
			sum=max(sum,x);
		}
		if(n==1)cout<<a[m-1]<<endl;
		else cout<<sum<<endl;
		for(int i=0;i<m;i++){
			ps[i]=0;
			a[i]=0;
		}
	}
}


// In the name of god
#include <bits/stdC++.h>
#pragma GCC optimze("02")
#define F first
#define S second
#define pb push_back
using ll =long long;
using namespace std;
const ll maxn=1e5+5, inf=1e18+18;
ll n,m,tt,a[maxn],sum,l,r,mid,x,ps[maxn];
pair <ll,ll> p[maxn];
int main(){
	ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0);
	cin>>tt;
	while(tt--){
		cin>>n>>m;
		for(int i=0;i<m;i++){
			cin>>p[i].F>>p[i].S;
			a[i]=p[i].F;
		}
		sort(a,a+m);
		for(int i=m-1;i>=0;i--)ps[i]=ps[i+1]+a[i];
		sum=0;
		for(int i=0;i<m;i++){
			if(p[i].S>=a[m-1])l=m;
			else if(p[i].S<=a[0])l=-1;
			else {
				l=0;r=m;
				while(r-1!=l){
					mid=(l+r)/2;
					if(a[mid]>=p[i].S)r=mid;
					else l=mid;
				}
			}
			if(l==-1){
				if(n<m)x=ps[m-n];
				else x=ps[0]+p[i].S*(n-m);
			}
			else if(l==m)x=p[i].F+p[i].S*(n-1);
			else{
				if(m-r>=n)x=ps[m-r];
				else{
					x=ps[r];
					if(p[i].F<p[i].S)x+=p[i].F+p[i].S*(n-(m-r)-1);
					else x+=p[i].S*(n-(m-r));
				}
			}
			sum=max(sum,x);
		}
		cout<<sum<<endl;
		for(int i=0;i<m;i++){
			ps[i]=0;
			a[i]=0;
			p[i].S=0;
			p[i].F=0;
		}
	}
}


// In the name of god
#include <bits/stdC++.h>
#pragma GCC optimze("02")
#define F first
#define S second
#define pb push_back
using ll =long long;
using namespace std;
const ll maxn=1e5+5, inf=1e18+18;
ll n,m,tt,a[maxn],sum,l,r,mid,x,ps[maxn];
pair <ll,ll> p[maxn];
int main(){
	ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0);
	cin>>tt;
	while(tt--){
		cin>>n>>m;
		for(int i=0;i<m;i++){
			cin>>p[i].F>>p[i].S;
			a[i]=p[i].F;
		}
		cout<<endl<<endl;
		sort(a,a+m);
		for(int i=m-1;i>=0;i--)ps[i]=ps[i+1]+a[i];
		sum=0;
		for(int i=0;i<m;i++){
			if(p[i].S>=a[m-1])l=m;
			else if(p[i].S<=a[0])l=-1;
			else {
				l=0;r=m-1;
				while(r-1!=l){
					mid=(l+r)/2;
					if(a[mid]>=p[i].S)r=mid;
					else l=mid;
				}
			}
			if(l==-1){
				if(n<=m)x=ps[m-n];
				else x=ps[0]+p[i].S*(n-m);
			}
			else if(l==m)x=p[i].F+p[i].S*(n-1);
			else{
				if(m-r>=n)x=ps[m-n];
				else{
					x=ps[r];
					if(p[i].F<p[i].S)x+=p[i].F+p[i].S*(n-(m-r)-1);
					else x+=p[i].S*(n-(m-r));
				}
			}
			sum=max(sum,x);
		}
		cout<<sum<<endl;
		for(int i=0;i<m;i++){
			ps[i]=0;
			a[i]=0;
			p[i].S=0;
			p[i].F=0;
		}
	}
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
#define ll long long
using namespace std;
const ll N = 2e5 + 12;
map<ll, ll>f;
ll n, m, a[N], b[N], lsh[N], now;
ll res1, tree[N], res2, t[N];
void add(ll x, ll y) {
	for (; x <= now; x += x&-x) tree[x] += y, t[x]++;
}
void ask(ll x) {
	for (; x; x -= x&-x) res1 += tree[x], res2 += t[x];
}
int main() {
	ll T; scanf("%lld", &T);
	while (T--) {
		ll cnt = 0, ans = 0;
		memset(tree, 0, sizeof(tree));
		memset(t, 0, sizeof(t));
		scanf("%lld%lld", &n, &m);
		for (ll i = 1; i <= m; i++) {
			scanf("%lld%lld", &a[i], &b[i]);
			lsh[++cnt] = a[i];
			lsh[++cnt] = b[i];
		}
		sort(lsh + 1, lsh + 1 + cnt);
		now = 0;
		for (ll i = cnt; i >= 1; i--)
		if (lsh[i] != lsh[i + 1]) f[lsh[i]] = ++now;
		for (ll i = 1; i <= m; i++) add(f[a[i]], a[i]);
		for (ll i = 1; i <= m; i++) {
			res1 = res2 = 0;
			ask(f[b[i]] - 1);
			if (b[i] < a[i]) res1 -= a[i], res2--;
			if (res2 < n) ans = max(ans, res1 + a[i] + (n - res2 - 1) * b[i]);
		}
		sort(a + 1, a + 1 + m);
		now = 0;
		for (ll i = m; i <= max(m - n + 1, 1ll); i++) now += a[i];
		printf("%lld\n", max(ans, now));
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
#define ll long long
using namespace std;
const ll N = 1e5 + 12;
map<ll, ll>f;
ll n, m, a[N], b[N], lsh[N];
ll res1, tree[N], res2, t[N];
void add(ll x, ll y) {
	for (; x <= n; x += x&-x) tree[x] += y, t[x]++;
}
void ask(ll x) {
	for (; x; x -= x&-x) res1 += tree[x], res2 += t[x];
}
ll main() {
	ll T; scanf("%lld", &T);
	while (T--) {
		ll cnt = 0, ans = 0;
		memset(tree, 0, sizeof(tree));
		memset(t, 0, sizeof(t));
		scanf("%lld%lld", &n, &m);
		for (ll i = 1; i <= m; i++) {
			scanf("%lld%lld", &a[i], &b[i]);
			lsh[++cnt] = a[i];
			lsh[++cnt] = b[i];
		}
		sort(lsh + 1, lsh + 1 + cnt);
		ll now = 0;
		for (ll i = cnt; i >= 1; i--)
		if (lsh[i] != lsh[i + 1]) f[lsh[i]] = ++now;
		for (ll i = 1; i <= m; i++) add(f[a[i]], a[i]);
		for (ll i = 1; i <= m; i++) {
			res1 = res2 = 0;
			ask(f[b[i]] - 1);
			if (b[i] < a[i]) res1 -= a[i], res2--;
			if (res2 < n) ans = max(ans, res1 + a[i] + (n - res2 - 1) * b[i]);
		}
		sort(a + 1, a + 1 + m);
		now = 0;
		for (ll i = 1; i <= min(n, m); i++) now += a[i];
		printf("%lld\n", max(ans, now));
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
#define ll long long
using namespace std;
const ll N = 2e5 + 12;
map<ll, ll>f;
ll n, m, a[N], b[N], lsh[N];
ll res1, tree[N], res2, t[N];
void add(ll x, ll y) {
	for (; x <= n; x += x&-x) tree[x] += y, t[x]++;
}
void ask(ll x) {
	for (; x; x -= x&-x) res1 += tree[x], res2 += t[x];
}
int main() {
	ll T; scanf("%lld", &T);
	while (T--) {
		ll cnt = 0, ans = 0;
		memset(tree, 0, sizeof(tree));
		memset(t, 0, sizeof(t));
		scanf("%lld%lld", &n, &m);
		for (ll i = 1; i <= m; i++) {
			scanf("%lld%lld", &a[i], &b[i]);
			lsh[++cnt] = a[i];
			lsh[++cnt] = b[i];
		}
		sort(lsh + 1, lsh + 1 + cnt);
		ll now = 0;
		for (ll i = cnt; i >= 1; i--)
		if (lsh[i] != lsh[i + 1]) f[lsh[i]] = ++now;
		for (ll i = 1; i <= m; i++) add(f[a[i]], a[i]);
		for (ll i = 1; i <= m; i++) {
			res1 = res2 = 0;
			ask(f[b[i]] - 1);
			if (b[i] < a[i]) res1 -= a[i], res2--;
			if (res2 < n) ans = max(ans, res1 + a[i] + (n - res2 - 1) * b[i]);
		}
		sort(a + 1, a + 1 + m);
		now = 0;
		for (ll i = 1; i <= min(n, m); i++) now += a[i];
		printf("%lld\n", max(ans, now));
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
#define ll long long
using namespace std;
const ll N = 2e5 + 12;
map<ll, ll>f;
ll n, m, a[N], b[N], lsh[N], now;
ll res1, tree[N], res2, t[N];
void add(ll x, ll y) {
	for (; x <= now; x += x&-x) tree[x] += y, t[x]++;
}
void ask(ll x) {
	for (; x; x -= x&-x) res1 += tree[x], res2 += t[x];
}
int main() {
	ll T; scanf("%lld", &T);
	while (T--) {
		ll cnt = 0, ans = 0;
		memset(tree, 0, sizeof(tree));
		memset(t, 0, sizeof(t));
		scanf("%lld%lld", &n, &m);
		for (ll i = 1; i <= m; i++) {
			scanf("%lld%lld", &a[i], &b[i]);
			lsh[++cnt] = a[i];
			lsh[++cnt] = b[i];
		}
		sort(lsh + 1, lsh + 1 + cnt);
		now = 0;
		for (ll i = cnt; i >= 1; i--)
		if (lsh[i] != lsh[i + 1]) f[lsh[i]] = ++now;
		for (ll i = 1; i <= m; i++) add(f[a[i]], a[i]);
		for (ll i = 1; i <= m; i++) {
			res1 = res2 = 0;
			ask(f[b[i]] - 1);
			if (b[i] < a[i]) res1 -= a[i], res2--;
			if (res2 < n) ans = max(ans, res1 + a[i] + (n - res2 - 1) * b[i]);
		}
		sort(a + 1, a + 1 + m);
		now = 0;
		for (ll i = m; i >= max(m - n + 1, 1ll); i--) now += a[i];
		printf("%lld\n", max(ans, now));
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
#define ll long long
using namespace std;
const ll N = 2e5 + 12;
map<ll, ll>f;
ll n, m, a[N], b[N], lsh[N], now;
ll res1, tree[N], res2, t[N];
void add(ll x, ll y) {
	for (; x <= now; x += x&-x) tree[x] += y, t[x]++;
}
void ask(ll x) {
	for (; x; x -= x&-x) res1 += tree[x], res2 += t[x];
}
int main() {
	ll T; scanf("%lld", &T);
	while (T--) {
		ll cnt = 0, ans = 0;
		memset(tree, 0, sizeof(tree));
		memset(t, 0, sizeof(t));
		scanf("%lld%lld", &n, &m);
		for (ll i = 1; i <= m; i++) {
			scanf("%lld%lld", &a[i], &b[i]);
			lsh[++cnt] = a[i];
			lsh[++cnt] = b[i];
		}
		sort(lsh + 1, lsh + 1 + cnt);
		now = 0;
		lsh[cnt + 1] = 0;
		for (ll i = cnt; i >= 1; i--)
		if (lsh[i] != lsh[i + 1]) f[lsh[i]] = ++now;
		for (ll i = 1; i <= m; i++) add(f[a[i]], a[i]);
		for (ll i = 1; i <= m; i++) {
			res1 = res2 = 0;
			ask(f[b[i]] - 1);
			if (b[i] < a[i]) res1 -= a[i], res2--;
			if (res2 < n) ans = max(ans, res1 + a[i] + (n - res2 - 1) * b[i]);
		}
		sort(a + 1, a + 1 + m);
		now = 0;
		for (ll i = m; i >= max(m - n + 1, 1ll); i--) now += a[i];
		printf("%lld\n", max(ans, now));
	}
	return 0;
}
/*
4
4 6
2 10
6 4
2 5
5 6
9 9
1 8

6 1
1 2

7 1
4 3

3 4
8 7
8 7
4 7
3 4
*/


#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
using namespace std;
const ll N = 1e5 + 12;
map<ll, ll>f;
ll n, m, a[N], b[N], lsh[N];
ll res1, tree[N], res2, t[N];
void add(ll x, ll y) {
	for (; x <= n; x += x&-x) tree[x] += y, t[x]++;
}
void ask(ll x) {
	for (; x; x -= x&-x) res1 += tree[x], res2 += t[x];
}
ll main() {
	ll T; scanf("%lld", &T);
	while (T--) {
		ll cnt = 0, ans = 0;
		memset(tree, 0, sizeof(tree));
		memset(t, 0, sizeof(t));
		scanf("%lld%lld", &n, &m);
		for (ll i = 1; i <= m; i++) {
			scanf("%lld%lld", &a[i], &b[i]);
			lsh[++cnt] = a[i];
			lsh[++cnt] = b[i];
		}
		sort(lsh + 1, lsh + 1 + cnt);
		ll now = 0;
		for (ll i = cnt; i >= 1; i--)
		if (lsh[i] != lsh[i + 1]) f[lsh[i]] = ++now;
		for (ll i = 1; i <= m; i++) add(f[a[i]], a[i]);
		for (ll i = 1; i <= m; i++) {
			res1 = res2 = 0;
			ask(f[b[i]] - 1);
			if (b[i] < a[i]) res1 -= a[i], res2--;
			if (res2 < n) ans = max(ans, res1 + a[i] + (n - res2 - 1) * b[i]);
		}
		sort(a + 1, a + 1 + m);
		now = 0;
		for (ll i = 1; i <= min(n, m); i++) now += a[i];
		printf("%lld\n", max(ans, now));
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
#define ll long long
using namespace std;
const ll N = 2e5 + 12;
map<ll, ll>f;
ll n, m, a[N], b[N], lsh[N], now;
ll res1, tree[N], res2, t[N];
void add(ll x, ll y) {
	for (; x <= now; x += x&-x) tree[x] += y, t[x]++;
}
void ask(ll x) {
	for (; x; x -= x&-x) res1 += tree[x], res2 += t[x];
}
int main() {
	ll T; scanf("%lld", &T);
	while (T--) {
		ll cnt = 0, ans = 0;
		memset(tree, 0, sizeof(tree));
		memset(t, 0, sizeof(t));
		scanf("%lld%lld", &n, &m);
		for (ll i = 1; i <= m; i++) {
			scanf("%lld%lld", &a[i], &b[i]);
			lsh[++cnt] = a[i];
			lsh[++cnt] = b[i];
		}
		sort(lsh + 1, lsh + 1 + cnt);
		now = 0;
		for (ll i = cnt; i >= 1; i--)
		if (lsh[i] != lsh[i + 1]) f[lsh[i]] = ++now;
		for (ll i = 1; i <= m; i++) add(f[a[i]], a[i]);
		for (ll i = 1; i <= m; i++) {
			res1 = res2 = 0;
			ask(f[b[i]] - 1);
			if (b[i] < a[i]) res1 -= a[i], res2--;
			if (res2 < n) ans = max(ans, res1 + a[i] + (n - res2 - 1) * b[i]);
		}
		sort(a + 1, a + 1 + m);
		now = 0;
		for (ll i = 1; i <= min(n, m); i++) now += a[i];
		printf("%lld\n", max(ans, now));
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
#define ll long long
using namespace std;
const ll N = 1e5 + 12;
map<ll, ll>f;
ll n, m, a[N], b[N], lsh[N];
ll res1, tree[N], res2, t[N];
void add(ll x, ll y) {
	for (; x <= n; x += x&-x) tree[x] += y, t[x]++;
}
void ask(ll x) {
	for (; x; x -= x&-x) res1 += tree[x], res2 += t[x];
}
int main() {
	ll T; scanf("%lld", &T);
	while (T--) {
		ll cnt = 0, ans = 0;
		memset(tree, 0, sizeof(tree));
		memset(t, 0, sizeof(t));
		scanf("%lld%lld", &n, &m);
		for (ll i = 1; i <= m; i++) {
			scanf("%lld%lld", &a[i], &b[i]);
			lsh[++cnt] = a[i];
			lsh[++cnt] = b[i];
		}
		sort(lsh + 1, lsh + 1 + cnt);
		ll now = 0;
		for (ll i = cnt; i >= 1; i--)
		if (lsh[i] != lsh[i + 1]) f[lsh[i]] = ++now;
		for (ll i = 1; i <= m; i++) add(f[a[i]], a[i]);
		for (ll i = 1; i <= m; i++) {
			res1 = res2 = 0;
			ask(f[b[i]] - 1);
			if (b[i] < a[i]) res1 -= a[i], res2--;
			if (res2 < n) ans = max(ans, res1 + a[i] + (n - res2 - 1) * b[i]);
		}
		sort(a + 1, a + 1 + m);
		now = 0;
		for (ll i = 1; i <= min(n, m); i++) now += a[i];
		printf("%lld\n", max(ans, now));
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
typedef long long ll;
struct node{
	int a,b;
}a[100005];
bool cmp(node x1,node y1)
{
	if(x1.a==y1.a) return x1.b>y1.b;
	return x1.a>y1.a;
}
int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
	int t;cin>>t;
	while(t--)
	{
		int n,m;
		cin>>n>>m;
		for(int i=1;i<=m;i++)
		cin>>a[i].a>>a[i].b;
		sort(a+1,a+1+m,cmp);
		int id=0,ans=0;
		for(int i=1;i<=m;i++)
		if(a[i].b>ans) {
			ans=a[i].b;id=i;
		}
		int ans1=0;
		bool fl=false;
		for(int i=1;i<=m;i++)
		{
			if(id==i-1) fl=true;
			if(n==0) break;
			if(a[i].a>ans) {
				ans1+=a[i].a;
				n--;
			}
			else break;
		}
		if(n==0) cout<<ans1<<endl;
		else {
			if(!fl)
			cout<<ans1+a[id].a+(n-1)*a[id].b<<endl;
			else cout<<ans1+(n)*a[id].b<<endl;
		}
	}
    return 0;
}


#include<iostream>
#include<algorithm>
#include<map>
#include<cstring>
#include<set>
#include<vector>
#include<cstdio>
using namespace std;
long long b[100005];
struct node{
	long long a,b;
}a[100005];
bool cmp(node x1,node y1)
{
	if(x1.a==y1.a) return x1.b>y1.b;
	return x1.a>y1.a;
}
int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
	int t;
	cin>>t;
	while(t--)
	{
		int n,m;
		cin>>n>>m;
		long long ans=0,q=0;
		int q1=0;
		for(int i=1;i<=m;i++)
		{
			cin>>a[i].a>>a[i].b;
		}
		sort(a+1,a+m+1,cmp);
		for(int i=1;i<=m;i++)
		if(q<a[i].b){
				q=a[i].b;q1=i;

			}
			else if(q==a[i].b) {
				q1=min(i,q1);
			}
		if(n>=m) {
			int i;
			for(i=1;i<=m;i++)
			{
				if(q>=a[i].a) break;
				ans+=a[i].a;
			}
			if(i>q1) ans+=q*(n-i+1);
			else ans+=q*(n-i)+a[q1].a;
		}
		else {
			int i;
			bool fl=false;
			for(i=1;i<=n;i++)
			{
				if(q>=a[i].a) {
					fl=true;
					break;
				}
				ans+=a[i].a;
			}
			if(fl){
			if(i>q1) ans+=q*(n-i+1);
			else ans+=q*(n-i)+a[q1].a;
			}

		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<map>
#include<cstring>
#include<set>
#include<vector>
#include<cstdio>
using namespace std;
struct node{
	long long a,b;
}a[100005];
bool cmp(node x1,node y1)
{
	if(x1.a==y1.a) return x1.b>y1.b;
	return x1.a>y1.a;
}
int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
	int t;
	cin>>t;
	while(t--)
	{
		long long n,m;
		cin>>n>>m;
		long long ans=0,q=0;
		long long q1=0;
		for(long long i=1;i<=m;i++)
		{
			cin>>a[i].a>>a[i].b;
		}
		sort(a+1,a+m+1,cmp);
		for(long long i=1;i<=m;i++)
		if(q<a[i].b){
				q=a[i].b;q1=i;

			}
			else if(q==a[i].b) {
				q1=min(i,q1);
			}
		if(n>=m) {
			long long i;
			for(i=1;i<=m;i++)
			{
				if(q>a[i].a) break;
				ans+=a[i].a;
			}
			if(i>q1) ans+=q*(n-i+1);
			else ans+=q*(n-i)+a[q1].a;
		}
		else {
			long long i;
			bool fl=false;

			for(i=1;i<=n;i++)
			{
				if(q>a[i].a) {
					fl=true;
					break;
				}
				ans+=a[i].a;
			}
			if(fl){
			if(i>q1) ans+=q*(n-i+1);
			else ans+=q*(n-i)+a[q1].a;
			}
			long long anss=0;
			for(i=1;i<=n;i++)
			anss+=a[i].a;
            ans=max(ans,anss);
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<iostream>
#include<vector>
#include<queue>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<set>
#define int long long
using namespace std;
typedef long long ll;
struct node{
	int a,b;
}a[100005];
int b[100005],sum[100005];
signed main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int t;cin>>t;
    while(t--)
	{
		int n,m;cin>>n>>m;
		for(int i=1;i<=m;i++)
		{
			cin>>a[i].a>>a[i].b;
			b[i]=a[i].a;
		}
		sort(b+1,b+1+m);
		for(int i=1;i<=m;i++)
		sum[i]=sum[i-1]+b[i];
		int ans=0;
		for(int i=1;i<=m;i++)
		{
			int q=lower_bound(b+1,b+1+m,a[i].b)-b;
			int q1=min(n,m-q+1);
			int cc=n;
			int ans1=sum[m]-sum[m-q1];
			cc-=q1;
			if(cc==0) {
			ans=max(ans,ans1);continue;
			}
			if(q1==0||a[i].a<b[q])
			{
				cc--;ans1+=a[i].a;
			}
			ans1+=cc*a[i].b;
			ans=max(ans,ans1);
		}
		cout<<ans<<endl;
	}
 	return 0;
}


#include<iostream>
#include<algorithm>
#include<map>
#include<cstring>
#include<set>
#include<vector>
#include<cstdio>
using namespace std;
struct node{
	long long a,b;
}a[1000005];
long long b[1000005];
bool cmp(node x1,node y1)
{
	if(x1.a==y1.a) return x1.b>y1.b;
	return x1.a>y1.a;
}
int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
	int t;
	cin>>t;
	while(t--)
	{
		long long n,m;
		cin>>n>>m;
		long long ans=0,q=0;
		long long q1=0;
		for(long long i=1;i<=m;i++)
		{
			cin>>a[i].a>>a[i].b;
		}
		sort(a+1,a+m+1,cmp);
		for(long long i=1;i<=m;i++)
		if(q<a[i].b){
				q=a[i].b;q1=i;
			}
			else if(q==a[i].b) {
				if(q1==0) q1=i;
				q1=min(i,q1);
			}
		if(n>m) {
			long long i;
			bool fl=false;
			for(i=1;i<=m;i++)
			{
				if(q>a[i].a) {
					fl=true;
					break;
				}
				ans+=a[i].a;
			}
			if(fl) {
			if(i>q1) ans+=q*(n-i+1);
			else ans+=q*(n-i)+a[q1].a;
			}
		else {
			ans+=q*(n-m);
		}

		}
		else {
			long long i;
			bool fl=false;
			for(i=1;i<=n;i++)
			{
				if(q>a[i].a) {
					fl=true;
					break;
				}
				ans+=a[i].a;
			}
			if(fl){
			if(i>q1) ans+=q*(n-i+1);
			else ans+=q*(n-i)+a[q1].a;
			}
			long long anss=0;
			for(i=1;i<=n;i++)
			anss+=a[i].a;
            ans=max(ans,anss);
		}
		long long ans1;q=0;
		b[0]=0;
		for(long long i=1;i<=m;i++)
		b[i]=a[i].a+b[i-1];
		for(long long i=1;i<=m;i++)
		{
			if(i>n) break;
			q=max(q,a[i].b);
			ans1=b[i];
			ans1+=(n-i)*q;

			ans=max(ans,ans1);
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<map>
#include<cstring>
#include<set>
#include<vector>
#include<cstdio>
using namespace std;
struct node{
	long long a,b;
}a[100005];
bool cmp(node x1,node y1)
{
	if(x1.a==y1.a) return x1.b>y1.b;
	return x1.a>y1.a;
}
int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
	int t;
	cin>>t;
	while(t--)
	{
		long long n,m;
		cin>>n>>m;
		long long ans=0,q=0;
		long long q1=0;
		for(long long i=1;i<=m;i++)
		{
			cin>>a[i].a>>a[i].b;
		}
		sort(a+1,a+m+1,cmp);
		for(long long i=1;i<=m;i++)
		if(q<a[i].b){
				q=a[i].b;q1=i;

			}
			else if(q==a[i].b) {
				q1=min(i,q1);
			}
		if(n>=m) {
			long long i;
			for(i=1;i<=m;i++)
			{
				if(q>a[i].a) break;
				ans+=a[i].a;
			}
			if(i>q1) ans+=q*(n-i+1);
			else ans+=q*(n-i)+a[q1].a;
		}
		else {
			long long i;
			bool fl=false;
			for(i=1;i<=n;i++)
			{
				if(q>a[i].a) {
					fl=true;
					break;
				}
				ans+=a[i].a;
			}
			if(fl){
			if(i>q1) ans+=q*(n-i+1);
			else ans+=q*(n-i)+a[q1].a;
			}

		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<map>
#include<cstring>
#include<set>
#include<vector>
#include<cstdio>
using namespace std;
struct node{
	long long a,b;
}a[1000005];
bool cmp(node x1,node y1)
{
	if(x1.a==y1.a) return x1.b>y1.b;
	return x1.a>y1.a;
}
int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
	int t;
	cin>>t;
	while(t--)
	{
		long long n,m;
		cin>>n>>m;
		long long ans=0,q=0;
		long long q1=0;
		for(long long i=1;i<=m;i++)
		{
			cin>>a[i].a>>a[i].b;
		}
		sort(a+1,a+m+1,cmp);
		for(long long i=1;i<=m;i++)
		if(q<a[i].b){
				q=a[i].b;q1=i;

			}
			else if(q==a[i].b) {
				if(q1==0) q1=i;
				q1=min(i,q1);
			}
		if(n>m) {
			long long i;
			bool fl=false;
			for(i=1;i<=m;i++)
			{
				if(q>a[i].a) {
					fl=true;
					break;
				}
				ans+=a[i].a;
			}
			if(fl) {
			if(i>q1) ans+=q*(n-i+1);
			else ans+=q*(n-i)+a[q1].a;
			}
		else {
			ans+=q*(n-m);
		}

		}
		else {
			long long i;
			bool fl=false;
			for(i=1;i<=n;i++)
			{
				if(q>a[i].a) {
					fl=true;
					break;
				}
				ans+=a[i].a;
			}
			if(fl){
			if(i>q1) ans+=q*(n-i+1);
			else ans+=q*(n-i)+a[q1].a;
			}
			long long anss=0;
			for(i=1;i<=n;i++)
			anss+=a[i].a;
            ans=max(ans,anss);
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<map>
#include<cstring>
#include<set>
#include<vector>
#include<cstdio>
using namespace std;
struct node{
	long long a,b;
}a[100005];
bool cmp(node x1,node y1)
{
	if(x1.a==y1.a) return x1.b>y1.b;
	return x1.a>y1.a;
}
int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
	int t;
	cin>>t;
	while(t--)
	{
		long long n,m;
		cin>>n>>m;
		long long ans=0,q=0;
		long long q1=0;
		for(long long i=1;i<=m;i++)
		{
			cin>>a[i].a>>a[i].b;
		}
		sort(a+1,a+m+1,cmp);
		for(long long i=1;i<=m;i++)
		if(q<a[i].b){
				q=a[i].b;q1=i;

			}
			else if(q==a[i].b) {
				q1=min(i,q1);
			}
		if(n>m) {
			long long i;
			bool fl=false;
			for(i=1;i<=m;i++)
			{
				if(q>a[i].a) {
					fl=true;
					break;
				}
				ans+=a[i].a;
			}
			if(fl){
			if(i>q1) ans+=q*(n-i+1);
			else ans+=q*(n-i)+a[q1].a;
			}
		}
		else {
			long long i;
			bool fl=false;
			for(i=1;i<=n;i++)
			{
				if(q>a[i].a) {
					fl=true;
					break;
				}
				ans+=a[i].a;
			}
			if(fl){
			if(i>q1) ans+=q*(n-i+1);
			else ans+=q*(n-i)+a[q1].a;
			}
			long long anss=0;
			for(i=1;i<=n;i++)
			anss+=a[i].a;
            ans=max(ans,anss);
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
typedef long long ll;
struct node{
	ll a,b;
}a[100005];
bool cmp(node x1,node y1)
{
	if(x1.a==y1.a) return x1.b>y1.b;
	return x1.a>y1.a;
}
int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
	int t;cin>>t;
	while(t--)
	{
		int n,m;
		cin>>n>>m;
		for(int i=1;i<=m;i++)
		cin>>a[i].a>>a[i].b;
		sort(a+1,a+1+m,cmp);
		int id=0;ll ans=0;
		for(int i=1;i<=m;i++)
		if(a[i].b>ans) {
			ans=a[i].b;id=i;
		}
		ll ans1=0;
		bool fl=false;
		for(int i=1;i<=m;i++)
		{
			if(id==i-1) fl=true;
			if(n==0) break;
			if(a[i].a>ans) {
				ans1+=a[i].a;
				n--;
			}
			else break;
		}
		if(n==0) cout<<ans1<<endl;
		else {
			if(!fl)
			cout<<(ll)(ans1+a[id].a+(ll)(n-1)*a[id].b)<<endl;
			else cout<<(ll)(ans1+(ll)n*a[id].b)<<endl;
		}
	}
    return 0;
}


#include<iostream>
#include<algorithm>
#include<map>
#include<cstring>
#include<set>
#include<vector>
#include<cstdio>
using namespace std;
struct node{
	long long a,b;
}a[100005];
bool cmp(node x1,node y1)
{
	if(x1.a==y1.a) return x1.b>y1.b;
	return x1.a>y1.a;
}
int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
	int t;
	cin>>t;
	while(t--)
	{
		long long n,m;
		cin>>n>>m;
		long long ans=0,q=0;
		long long q1=0;
		for(long long i=1;i<=m;i++)
		{
			cin>>a[i].a>>a[i].b;
		}
		sort(a+1,a+m+1,cmp);
		for(long long i=1;i<=m;i++)
		if(q<a[i].b){
				q=a[i].b;q1=i;

			}
			else if(q==a[i].b) {
				q1=min(i,q1);
			}
		if(n>m) {
			long long i;
			bool fl=false;
			for(i=1;i<=m;i++)
			{
				if(q>a[i].a) {
					fl=true;
					break;
				}
				ans+=a[i].a;
			}
			if(fl) {
			if(i>q1) ans+=q*(n-i+1);
			else ans+=q*(n-i)+a[q1].a;
			}
		else {
			ans+=q*(n-m);
		}

		}
		else {
			long long i;
			bool fl=false;
			for(i=1;i<=n;i++)
			{
				if(q>a[i].a) {
					fl=true;
					break;
				}
				ans+=a[i].a;
			}
			if(fl){
			if(i>q1) ans+=q*(n-i+1);
			else ans+=q*(n-i)+a[q1].a;
			}
			long long anss=0;
			for(i=1;i<=n;i++)
			anss+=a[i].a;
            ans=max(ans,anss);
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<map>
#include<cstring>
#include<set>
#include<vector>
#include<cstdio>
using namespace std;
long long b[100005];
struct node{
	long long a,b;
}a[100005];
bool cmp(node x1,node y1)
{
	if(x1.a==y1.a) return x1.b>y1.b;
	return x1.a>y1.a;
}
int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
	int t;
	cin>>t;
	while(t--)
	{
		int n,m;
		cin>>n>>m;
		long long ans=0,q=0;
		int q1=0;
		for(int i=1;i<=m;i++)
		{
			cin>>a[i].a>>a[i].b;
		}
		sort(a+1,a+m+1,cmp);
		for(int i=1;i<=m;i++)
		if(q<a[i].b){
				q=a[i].b;q1=i;

			}
			else if(q==a[i].b) {
				q1=min(i,q1);
			}
		if(n>=m) {
			int i;
			for(i=1;i<=m;i++)
			{
				if(q>a[i].a) break;
				ans+=a[i].a;
			}
			if(i>q1) ans+=q*(n-i+1);
			else ans+=q*(n-i)+a[q1].a;
		}
		else {
			int i;
			bool fl=false;
			for(i=1;i<=n;i++)
			{
				if(q>a[i].a) {
					fl=true;
					break;
				}
				ans+=a[i].a;
			}
			if(fl){
			if(i>q1) ans+=q*(n-i+1);
			else ans+=q*(n-i)+a[q1].a;
			}

		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<map>
#include<cstring>
#include<set>
#include<vector>
#include<cstdio>
using namespace std;
struct node{
	long long a,b;
}a[1000005];
bool cmp(node x1,node y1)
{
	if(x1.a==y1.a) return x1.b>y1.b;
	return x1.a>y1.a;
}
int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
	int t;
	cin>>t;
	while(t--)
	{
		long long n,m;
		cin>>n>>m;
		long long ans=0,q=0;
		long long q1=0;
		for(long long i=1;i<=m;i++)
		{
			cin>>a[i].a>>a[i].b;
		}
		sort(a+1,a+m+1,cmp);
		for(long long i=1;i<=m;i++)
		if(q<a[i].b){
				q=a[i].b;q1=i;

			}
			else if(q==a[i].b) {
				q1=min(i,q1);
			}
		if(n>m) {
			long long i;
			bool fl=false;
			for(i=1;i<=m;i++)
			{
				if(q>a[i].a) {
					fl=true;
					break;
				}
				ans+=a[i].a;
			}
			if(fl) {
			if(i>q1) ans+=q*(n-i+1);
			else ans+=q*(n-i)+a[q1].a;
			}
		else {
			ans+=q*(n-m);
		}

		}
		else {
			long long i;
			bool fl=false;
			for(i=1;i<=n;i++)
			{
				if(q>a[i].a) {
					fl=true;
					break;
				}
				ans+=a[i].a;
			}
			if(fl){
			if(i>q1) ans+=q*(n-i+1);
			else ans+=q*(n-i)+a[q1].a;
			}
			long long anss=0;
			for(i=1;i<=n;i++)
			anss+=a[i].a;
            ans=max(ans,anss);
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
typedef long long ll;
struct node{
	int a,b;
}a[100005];
bool cmp(node x1,node y1)
{
	if(x1.a==y1.a) return x1.b>y1.b;
	return x1.a>y1.a;
}
int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
	int t;cin>>t;
	while(t--)
	{
		int n,m;
		cin>>n>>m;
		for(int i=1;i<=m;i++)
		cin>>a[i].a>>a[i].b;
		sort(a+1,a+1+n,cmp);
		int id=0,ans=0;
		for(int i=1;i<=m;i++)
		if(a[i].b>ans) {
			ans=a[i].b;id=i;
		}
		int ans1=0;
		bool fl=false;
		for(int i=1;i<=m;i++)
		{
			if(id==i-1) fl=true;
			if(n==0) break;
			if(a[i].a>ans) {
				ans1+=a[i].a;
				n--;
			}
			else break;
		}
		if(n==0) cout<<ans1<<endl;
		else {
			if(!fl)
			cout<<ans1+a[id].a+(n-1)*a[id].b<<endl;
			else cout<<ans1+(n)*a[id].b<<endl;
		}
	}
    return 0;
}


#include<iostream>
#include<vector>
#include<queue>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<set>
using namespace std;
typedef long long ll;
struct node{
	int a,b;
}a[100005];
int b[100005],sum[100005];
int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int t;cin>>t;
    while(t--)
	{
		int n,m;cin>>n>>m;
		for(int i=1;i<=m;i++)
		{
			cin>>a[i].a>>a[i].b;
			b[i]=a[i].a;
		}
		sort(b+1,b+1+m);
		for(int i=1;i<=m;i++)
		sum[i]=sum[i-1]+b[i];
		int ans=0;
		for(int i=1;i<=m;i++)
		{
			int q=lower_bound(b+1,b+1+m,a[i].b)-b;
			int q1=min(n,m-q+1);
			int cc=n;
			int ans1=sum[m]-sum[m-q1];
			cc-=q1;
			if(cc==0) {
			ans=max(ans,ans1);continue;
			}
			if(q1==0||a[i].a<b[q])
			{
				cc--;ans1+=a[i].a;
			}
			ans1+=cc*a[i].b;
			ans=max(ans,ans1);
		}
		cout<<ans<<endl;
	}
 	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>

namespace wxy{
    const int N = 1e5 + 5;
    int b[N],c[N];
    std::pair<int,int> a[N];
    void main(){
        int t;
        std::cin >> t;
        while (t--){
            int n,m;
            scanf("%d%d",&n,&m);
            for (int i = 1; i <= m; i++){
                scanf("%d%d",&a[i].first,&b[i]);
                c[i] = a[i].first;
                a[i].second = i;
            }
            std::sort(a + 1,a + 1 + m);
            std::reverse(a + 1,a + 1 + m);
            long long ans = 0;
            for (int i = 1; i <= m; i++){
                long long pos = b[i] * (n - 1) + c[i];
                int op = 0;
                for (int j = 1; j <= m; j++){
                    bool ck = false;
                    if (a[j].first <= b[i] && a[j].second != i) ck = true;
                    if (a[j].first > b[i] && a[j].second != i && op < n - 1){
                        pos = pos - b[i] + a[j].first;
                        op++;
                    }else if(a[j].first > c[i] && op == n - 1){
                        pos = pos - c[i] + a[j].first;
                        break;
                    }else if (ck)break;
                }
                ans = std::max(ans,pos);
            }
            printf("%d\n",ans);
        }
    }
}signed main(){wxy::main();return 0;}


#include<cstdio>
#define del(x) ((x)>=TT?(x)-=TT:(x))
using namespace std;
const int maxn=1<<21|5;
int n,m,cnt[maxn];
long long T,TT,a[maxn],num[maxn],b[25];
long long read(){
	long long ret=0;bool f=0;char ch=getchar();
	while(ch>'9'||ch<'0') f^=ch=='-',ch=getchar();
	while(ch<='9'&&ch>='0') ret=ret*10+ch-'0',ch=getchar();
	return f?-ret:ret;
}
long long ksc(long long x,long long y){
	return (x*y-(long long)((double)x/TT*y)*TT+TT)%TT;
}
long long ksm(long long x,long long y){
	long long ret=1,w=x;
	while(y){
		if(y&1) ret=ksc(ret,w);
		y>>=1;
		w=ksc(w,w);
	}
	return ret;
}
void FWT(long long*A,bool typ){
	for (int i=1;i<n;i<<=1)
	for (int j=0;i+j<n;j++){
		if(j/i&1) continue;
		long long A0=A[j],A1=A[i+j];
		A[j]=A0+A1,A[i+j]=A0-A1+TT;
		if(typ) A[j]>>=1,A[i+j]>>=1;
		del(A[j]),del(A[i+j]);
	}
}
int main(){
	n=1<<(m=read()),T=read(),TT=read()*n;
	for (int i=0;i<n;i++) a[i]=read()%TT;
	for (int i=0;i<=m;i++) b[i]=read()%TT;
	for (int i=0;i<n;i++) num[i]=b[cnt[i]=cnt[i^i&-i]+!!i];
	FWT(a,0),FWT(num,0);
	for (int i=0;i<n;i++) a[i]=ksc(a[i],ksm(num[i],T));
	FWT(a,1);
	TT/=n;
	for (int i=0;i<n;i++) printf("%lld\n",a[i]%TT);
	return 0;
}


#include<cstdio>
using namespace std;
const int maxn=1<<21|5;
int n,m,TT,cnt[maxn];
long long T,a[maxn],num[maxn],b[25];
long long read(){
	long long ret=0;bool f=0;char ch=getchar();
	while(ch>'9'||ch<'0') f^=ch=='-',ch=getchar();
	while(ch<='9'&&ch>='0') ret=ret*10+ch-'0',ch=getchar();
	return f?-ret:ret;
}
long long ksm(int x,long long y){
	long long ret=1,w=x;
	while(y){
		if(y&1) ret=ret*w%TT;
		y>>=1;
		w=w*w%TT;
	}
	return ret;
}
void FWT(long long*A,bool typ){
	for (int i=1;i<n;i<<=1)
	for (int j=0;i+j<n;j++){
		if(j/i&1) continue;
		long long A0=A[j],A1=A[i+j];
		A[j]=A0+A1,A[i+j]=A0-A1;
		if(typ) if(!(TT&1)) A[j]>>=1,A[i+j]>>=1;
		else A[j]*=TT+1>>1,A[i+j]*=TT+1>>1;
		A[j]%=TT,A[i+j]%=TT;
	}
}
int main(){
	n=1<<(m=read()),T=read(),TT=read();
	for (int i=0;i<n;i++) a[i]=read();
	for (int i=0;i<=m;i++) b[i]=read();
	for (int i=0;i<n;i++) num[i]=b[cnt[i]=cnt[i^i&-i]+!!i];
	FWT(a,0),FWT(num,0);
	for (int i=0;i<n;i++) a[i]=a[i]*ksm(num[i],T)%TT;
	FWT(a,1);
	for (int i=0;i<n;i++) printf("%lld\n",a[i]+TT*(a[i]<0));
	return 0;
}


#include<cstdio>
using namespace std;
const int maxn=1<<21|5;
int n,m,TT,cnt[maxn];
long long T,a[maxn],num[maxn],b[25];
long long read(){
	long long ret=0;bool f=0;char ch=getchar();
	while(ch>'9'||ch<'0') f^=ch=='-',ch=getchar();
	while(ch<='9'&&ch>='0') ret=ret*10+ch-'0',ch=getchar();
	return f?-ret:ret;
}
long long ksm(int x,int y){
	long long ret=1,w=x;
	while(y){
		if(y&1) ret=ret*w%TT;
		y>>=1;
		w=w*w%TT;
	}
	return ret;
}
void FWT(long long*A,bool typ){
	for (int i=1;i<n;i<<=1)
	for (int j=0;i+j<n;j++){
		if(j/i&1) continue;
		int A0=A[j],A1=A[i+j];
		A[j]=A0+A1,A[i+j]=A0-A1;
		if(typ) if(!(TT&1)) A[j]>>=1,A[i+j]>>=1;
		else A[j]*=TT+1>>1,A[i+j]*=TT+1>>1;
		A[j]%=TT,A[i+j]%=TT;
	}
}
int main(){
	n=1<<(m=read()),T=read(),TT=read();
	for (int i=0;i<n;i++) a[i]=read();
	for (int i=0;i<=m;i++) b[i]=read();
	for (int i=0;i<n;i++) num[i]=b[cnt[i]=cnt[i^i&-i]+!!i];
	FWT(a,0),FWT(num,0);
	for (int i=0;i<n;i++) a[i]=a[i]*ksm(num[i],T);
	FWT(a,1);
	for (int i=0;i<n;i++) printf("%lld\n",a[i]+TT*(a[i]<0));
	return 0;
}


#include<cstdio>
#define del(x) ((x)>=TT?(x)-=TT:(x))
using namespace std;
const int maxn=1<<21|5;
int n,m,cnt[maxn];
long long T,TT,a[maxn],num[maxn],b[25];
long long read(){
	long long ret=0;bool f=0;char ch=getchar();
	while(ch>'9'||ch<'0') f^=ch=='-',ch=getchar();
	while(ch<='9'&&ch>='0') ret=ret*10+ch-'0',ch=getchar();
	return f?-ret:ret;
}
long long ksc(long long x,long long y){
	long long ret=0,w=x;
	while(y){
		if(y&1) ret+=w,del(ret);
		y>>=1;
		w+=w,del(w);
	}
	return ret;
}
long long ksm(long long x,long long y){
	long long ret=1,w=x;
	while(y){
		if(y&1) ret=ksc(ret,w);
		y>>=1;
		w=ksc(w,w);
	}
	return ret;
}
void FWT(long long*A,bool typ){
	for (int i=1;i<n;i<<=1)
	for (int j=0;i+j<n;j++){
		if(j/i&1) continue;
		long long A0=A[j],A1=A[i+j];
		A[j]=A0+A1,A[i+j]=A0-A1;
		if(A[i+j]<0) A[i+j]+=TT;
		if(typ) A[j]>>=1,A[i+j]>>=1;
		del(A[j]),del(A[i+j]);
	}
}
int main(){
	n=1<<(m=read()),T=read(),TT=read()*n;
	for (int i=0;i<n;i++) a[i]=read()%TT;
	for (int i=0;i<=m;i++) b[i]=read()%TT;
	for (int i=0;i<n;i++) num[i]=b[cnt[i]=cnt[i^i&-i]+!!i];
	FWT(a,0),FWT(num,0);
	for (int i=0;i<n;i++) a[i]=ksc(a[i],ksm(num[i],T));
	FWT(a,1);
	TT/=n;
	for (int i=0;i<n;i++) printf("%lld\n",(a[i]%TT+TT)%TT);
	return 0;
}


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

const int Log = 25;
const int Max = 1.1e6 + 10;
int Mod = 1e9 + 7;

template<int sz> struct Matrix
{
	ll A[sz][sz];
	friend Matrix operator * (Matrix a , Matrix b)
	{
		Matrix res;
		for(int i = 0; i < sz ; i++)
			for(int j = 0; j < sz ; j++)
				res.A[i][j] = 0;
		for(int i = 0; i < sz ; i++)
			for(int k = 0; k < sz; k++)
				for(int j = 0; j < sz ; j++)
					res.A[i][j] = (res.A[i][j] + a.A[i][k] * b.A[k][j]) % Mod;
		return res;
	}
};

/*template<int sz> Matrix<sz> pw(Matrix<sz> a, ll b)
{
	if(b == 1)
		return a;
	Matrix<sz> half = pw(a , b / 2);
	if(b % 2)
		return half * half * a;
	return half * half;
}*/
template<int sz> Matrix<sz> pw(Matrix<sz> a , ll b)
{
	while(!(b & 1))
	{
		b = b >> 1;
		a = a * a;
	}
	Matrix<sz> c = a;
	b = b >> 1;
	a = a * a;
	while(b)
	{
		if(b & 1)
			c = a * c;
		b = b >> 1;
		a = a * a;
	}
	return c;
}

ll C[Log][Log];
ll A[Max];
int B[Max];
Matrix<22> upd;

bool sw;
int dp[2][Max][Log];

int main()
{
	ll m , l; cin >> m >> l >> Mod;
	int n = (1 << m);
	// entekhab!
	for(int i = 0; i <= m ; i++)
	{
		C[i][i] = 1;
		C[0][i] = 1;
	}
	for(int i = 2 ; i <= m ; i++)
		for(int j = 1 ; j < i ; j++)
			C[j][i] = (C[j][i - 1] + C[j - 1][i - 1]) % Mod;

	for(int i = 0 ; i < n ; i++)
		cin >> A[i];
	for(int i = 0; i <= m ; i++)
		cin >> B[i];

	for(int i = 0; i <= m; i++)
		for(int j = 0 ; j <= m ; j++)
			for(int k = 0; k <= m ; k++)
			{
				int b = k + i - j;
				if(b % 2 || b < 0)
					continue;
				b /= 2;
				int a = k - b;
				if(a < 0)
					continue;
				ll numofway = C[a][j] * C[b][m - j] % Mod;
				upd.A[i][j] = (upd.A[i][j] + numofway * B[k]) % Mod;
			}
	/*
	cout << '\n';
	for(int i = 0; i <= m ; i++)
	{
		for(int j = 0; j <= m ; j++)
			cout << upd.A[i][j] << " " ;
		cout << '\n';
	}
	cout << '\n';
	*/

	upd = pw(upd , l);

	for(int i = 1; i <= m ; i++)
	{
		sw = !sw;
		for(int d = 0; d <= m ; d++)
			for(int v = 0; v < n ; v++)
			{
				if(d > 1)
					dp[sw][v][d] = ((ll)dp[!sw][v][d] + (ll)dp[!sw][v ^ (1 << (i - 1))][d - 1]) % Mod;
				else if(d == 1)
					dp[sw][v][1] = ((ll)dp[!sw][v][d] + (ll)A[v ^ (1 << (i - 1))]) % Mod;
				else
					dp[sw][v][0] = A[v] % Mod;
			}
	}

	for(int v = 0 ; v < n ; v++)
	{
		ll ansv = 0;
		for(int i = 0; i <= m ; i++)
			ansv = (ansv + (ll)dp[sw][v][i] * upd.A[0][i]) % Mod;
		cout << ansv << '\n';
	}

}


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

const int Log = 25;
const int Max = 1.1e6 + 10;
int Mod = 1e9 + 7;

template<int sz> struct Matrix
{
	ll A[sz][sz];
	friend Matrix operator * (Matrix a , Matrix b)
	{
		Matrix res;
		for(int i = 0; i < sz ; i++)
			for(int j = 0; j < sz ; j++)
				res.A[i][j] = 0;
		for(int i = 0; i < sz ; i++)
			for(int k = 0; k < sz; k++)
				for(int j = 0; j < sz ; j++)
					res.A[i][j] = (res.A[i][j] + a.A[i][k] * b.A[k][j]) % Mod;
		return res;
	}
};

/*template<int sz> Matrix<sz> pw(Matrix<sz> a, ll b)
{
	if(b == 1)
		return a;
	Matrix<sz> half = pw(a , b / 2);
	if(b % 2)
		return half * half * a;
	return half * half;
}*/
template<int sz> Matrix<sz> pw(Matrix<sz> a , ll b)
{
	while(!(b & 1))
	{
		b = b >> 1;
		a = a * a;
	}
	Matrix<sz> c = a;
	b = b >> 1;
	a = a * a;
	while(b)
	{
		if(b & 1)
			c = a * c;
		b = b >> 1;
		a = a * a;
	}
	return c;
}

ll C[Log][Log];
ll A[Max];
int B[Max];
Matrix<22> upd;

bool sw;
int dp[2][Log][Max];

int main()
{
	ll m , l; cin >> m >> l >> Mod;
	int n = (1 << m);
	// entekhab!
	for(int i = 0; i <= m ; i++)
	{
		C[i][i] = 1;
		C[0][i] = 1;
	}
	for(int i = 2 ; i <= m ; i++)
		for(int j = 1 ; j < i ; j++)
			C[j][i] = (C[j][i - 1] + C[j - 1][i - 1]) % Mod;

	for(int i = 0 ; i < n ; i++)
		cin >> A[i];
	for(int i = 0; i <= m ; i++)
		cin >> B[i];

	for(int i = 0; i <= m; i++)
		for(int j = 0 ; j <= m ; j++)
			for(int k = 0; k <= m ; k++)
			{
				int b = k + i - j;
				if(b % 2 || b < 0)
					continue;
				b /= 2;
				int a = k - b;
				if(a < 0)
					continue;
				ll numofway = C[a][j] * C[b][m - j] % Mod;
				upd.A[i][j] = (upd.A[i][j] + numofway * B[k]) % Mod;
			}
	/*
	cout << '\n';
	for(int i = 0; i <= m ; i++)
	{
		for(int j = 0; j <= m ; j++)
			cout << upd.A[i][j] << " " ;
		cout << '\n';
	}
	cout << '\n';
	*/

	upd = pw(upd , l);

	for(int i = 1; i <= m ; i++)
	{
		sw = !sw;
		for(int d = 0; d <= m ; d++)
			for(int v = 0; v < n ; v++)
			{
				if(d > 1)
					dp[sw][d][v] = ((ll)dp[!sw][d][v] + (ll)dp[!sw][d - 1][v ^ (1 << (i - 1))]) % Mod;
				else if(d == 1)
					dp[sw][1][v] = ((ll)dp[!sw][d][v] + (ll)A[v ^ (1 << (i - 1))]) % Mod;
				else
					dp[sw][0][v] = A[v] % Mod;
			}
	}

	for(int v = 0 ; v < n ; v++)
	{
		ll ansv = 0;
		for(int i = 0; i <= m ; i++)
			ansv = (ansv + (ll)dp[sw][i][v] * upd.A[0][i]) % Mod;
		cout << ansv << '\n';
	}

}


/**
 *　　　　　　  　　┏┓　　 　┏┓
 * 　　　　　  　　┏┛┗━━━━━━━┛┗━━━┓
 * 　　　　　  　　┃　　　　　　　┃
 * 　　　　　  　　┃　　　━　　 　┃
 * 　　　　　  　　┃　＞　　　＜　┃
 * 　　　　　  　　┃　　　　　　　┃
 * 　　　　　  　　┃...　⌒　... 　┃
 * 　　　　　　  　┃              ┃
 * 　　　　　　  　┗━┓          ┏━┛
 * 　　　　　　　　　┃          ┃　Code is far away from bug with the animal protecting
 * 　　　　　　　　　┃          ┃   神兽保佑,代码无bug
 * 　　　　　　　　　┃          ┃
 * 　　　　　　　　　┃          ┃
 * 　　　　　　　　　┃          ┃
 * 　　　　　　　　　┃          ┃
 * 　　　　　　　　　┃          ┗━━━┓
 * 　　　　　　　　　┃              ┣┓
 * 　　　　　　　　　┃              ┏┛
 * 　　　　　　　　　┗┓┓┏━━━━━━━━┳┓┏┛
 * 　　　　　　　　　　┃┫┫       ┃┫┫
 * 　　　　　　　　　　┗┻┛       ┗┻┛
 */
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<map>
#include<stack>
#include<cmath>
#include<set>
#include<bitset>
#include<complex>
#include<cstdlib>
#include<assert.h>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-8
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define mid (x+y>>1)
#define sqr(x) ((x)*(x))
#define NM 1100005
#define nm 400005
using namespace std;
const double pi=acos(-1);
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}

int n,m;
ll _t,inf;
ll a[NM],b[NM],c[NM];

ll plu(ll x,ll y){
    ll s=0;
    while(y){
	if(y&1)s+=x,s%=inf;
	x<<=1;x%=inf;
	y>>=1;
    }
    return s;
}

ll qpow(ll x,ll t){
    ll s=1;
    for(;t;t>>=1,x=plu(x,x))if(t&1)s=plu(s,x);
    return s;
}

void fwt(ll*a){
    for(int k=1;k<n;k<<=1)
	for(int i=0;i<n;i+=k<<1)
	    for(int j=0;j<k;j++){
		ll x=a[i+j],y=a[i+j+k];
		a[i+j]=(x+y)%inf;
		a[i+j+k]=(x-y+inf)%inf;
	    }
}

int main(){
    m=read();n=succ(m);_t=read();inf=read()*n;
    inc(i,0,n-1)a[i]=read()%inf;
    inc(i,0,m)b[i]=read()%inf;
    inc(i,0,n-1)c[i]=b[__builtin_popcount(i)];
    fwt(c);fwt(a);
    inc(i,0,n-1)a[i]=a[i]*qpow(c[i],_t)%inf;
    fwt(a);
    inc(i,0,n-1)printf("%lld\n",a[i]/n);
    return 0;
}


/**
 *　　　　　　  　　┏┓　　 　┏┓
 * 　　　　　  　　┏┛┗━━━━━━━┛┗━━━┓
 * 　　　　　  　　┃　　　　　　　┃
 * 　　　　　  　　┃　　　━　　 　┃
 * 　　　　　  　　┃　＞　　　＜　┃
 * 　　　　　  　　┃　　　　　　　┃
 * 　　　　　  　　┃...　⌒　... 　┃
 * 　　　　　　  　┃              ┃
 * 　　　　　　  　┗━┓          ┏━┛
 * 　　　　　　　　　┃          ┃　Code is far away from bug with the animal protecting
 * 　　　　　　　　　┃          ┃   神兽保佑,代码无bug
 * 　　　　　　　　　┃          ┃
 * 　　　　　　　　　┃          ┃
 * 　　　　　　　　　┃          ┃
 * 　　　　　　　　　┃          ┃
 * 　　　　　　　　　┃          ┗━━━┓
 * 　　　　　　　　　┃              ┣┓
 * 　　　　　　　　　┃              ┏┛
 * 　　　　　　　　　┗┓┓┏━━━━━━━━┳┓┏┛
 * 　　　　　　　　　　┃┫┫       ┃┫┫
 * 　　　　　　　　　　┗┻┛       ┗┻┛
 */
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<map>
#include<stack>
#include<cmath>
#include<set>
#include<bitset>
#include<complex>
#include<cstdlib>
#include<assert.h>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-8
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define mid (x+y>>1)
#define sqr(x) ((x)*(x))
#define NM 1100005
#define nm 400005
using namespace std;
const double pi=acos(-1);
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}

int n,m;
ll _t,inf;
ll a[NM],b[NM],c[NM];

ll plu(ll x,ll y){
    ll s=0;
    while(y){
	if(y&1)s+=x,s%=inf;
	x<<=1;x%=inf;
	y>>=1;
    }
    return s;
}

ll qpow(ll x,ll t){
    ll s=1;
    for(;t;t>>=1,x=plu(x,x))if(t&1)s=plu(s,x);
    return s;
}

void fwt(ll*a){
    for(int k=1;k<n;k<<=1)
	for(int i=0;i<n;i+=k<<1)
	    for(int j=0;j<k;j++){
		ll x=a[i+j],y=a[i+j+k];
		a[i+j]=(x+y)%inf;
		a[i+j+k]=(x-y+inf)%inf;
	    }
}

int main(){
    m=read();n=succ(m);_t=read();inf=read()*n;
    inc(i,0,n-1)a[i]=read();
    inc(i,0,m)b[i]=read();
    inc(i,0,n-1)c[i]=b[__builtin_popcount(i)];
    fwt(c);fwt(a);
    inc(i,0,n-1)a[i]=a[i]*qpow(c[i],_t)%inf;
    fwt(a);
    inc(i,0,n-1)printf("%lld\n",a[i]/n);
    return 0;
}


/**
 *　　　　　　  　　┏┓　　 　┏┓
 * 　　　　　  　　┏┛┗━━━━━━━┛┗━━━┓
 * 　　　　　  　　┃　　　　　　　┃
 * 　　　　　  　　┃　　　━　　 　┃
 * 　　　　　  　　┃　＞　　　＜　┃
 * 　　　　　  　　┃　　　　　　　┃
 * 　　　　　  　　┃...　⌒　... 　┃
 * 　　　　　　  　┃              ┃
 * 　　　　　　  　┗━┓          ┏━┛
 * 　　　　　　　　　┃          ┃　Code is far away from bug with the animal protecting
 * 　　　　　　　　　┃          ┃   神兽保佑,代码无bug
 * 　　　　　　　　　┃          ┃
 * 　　　　　　　　　┃          ┃
 * 　　　　　　　　　┃          ┃
 * 　　　　　　　　　┃          ┃
 * 　　　　　　　　　┃          ┗━━━┓
 * 　　　　　　　　　┃              ┣┓
 * 　　　　　　　　　┃              ┏┛
 * 　　　　　　　　　┗┓┓┏━━━━━━━━┳┓┏┛
 * 　　　　　　　　　　┃┫┫       ┃┫┫
 * 　　　　　　　　　　┗┻┛       ┗┻┛
 */
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<map>
#include<stack>
#include<cmath>
#include<set>
#include<bitset>
#include<complex>
#include<cstdlib>
#include<assert.h>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-8
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define mid (x+y>>1)
#define sqr(x) ((x)*(x))
#define NM 1100005
#define nm 400005
using namespace std;
const double pi=acos(-1);
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}

int n,m;
ll _t,inf;
ll a[NM],b[NM],c[NM];
inline ll plu(long long x,long long y){
    ll tmp=(x*y-(ll)((long double)x/inf*y+1.0e-8)*inf);
    return tmp<0 ? tmp+inf : tmp;
}

ll qpow(ll x,ll t){
    ll s=1;
    for(;t;t>>=1,x=plu(x,x))if(t&1)s=plu(s,x);
    return s;
}

void fwt(ll*a){
    for(int k=1;k<n;k<<=1)
	for(int i=0;i<n;i+=k<<1)
	    for(int j=0;j<k;j++){
		ll x=a[i+j],y=a[i+j+k];
		a[i+j]=(x+y)%inf;
		a[i+j+k]=(x-y+inf)%inf;
	    }
}

int main(){
    m=read();n=succ(m);_t=read();inf=read()*n;
    inc(i,0,n-1)a[i]=read()%inf;
    inc(i,0,m)b[i]=read()%inf;
    inc(i,0,n-1)c[i]=b[__builtin_popcount(i)];
    fwt(c);fwt(a);
    inc(i,0,n-1)a[i]=plu(a[i],qpow(c[i],_t));
    fwt(a);
    inc(i,0,n-1)printf("%lld\n",a[i]/n);
    return 0;
}


#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>
using namespace std;

template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { os << '{'; string sep; for (const auto &x : v) os << sep << x, sep = ", "; return os << '}'; }
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }

void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }

#ifdef NEAL_DEBUG
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

template<const int64_t &MOD>
struct _m_int {
    int64_t val;

    _m_int(int64_t v = 0) {
        if (v < 0) v = v % MOD + MOD;
        if (v >= MOD) v %= MOD;
        val = v;
    }

    static int64_t inv_mod(int64_t a, int64_t m = MOD) {
        // https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm#Example
        int64_t g = m, r = a, x = 0, y = 1;

        while (r != 0) {
            int64_t q = g / r;
            g %= r; swap(g, r);
            x -= q * y; swap(x, y);
        }

        return x < 0 ? x + m : x;
    }

    explicit operator int() const { return val; }
    explicit operator int64_t() const { return val; }

    _m_int& operator+=(const _m_int &other) {
        val -= MOD - other.val;
        if (val < 0) val += MOD;
        return *this;
    }

    _m_int& operator-=(const _m_int &other) {
        val -= other.val;
        if (val < 0) val += MOD;
        return *this;
    }

    static uint64_t fast_mod(uint64_t x, uint64_t m = MOD) {
#if !defined(_WIN32) || defined(_WIN64)
        return x % m;
#endif
        // Optimized mod for Codeforces 32-bit machines.
        // x must be less than 2^32 * m for this to work, so that x / m fits in a 32-bit integer.
        unsigned x_high = x >> 32, x_low = (unsigned) x;
        unsigned quot, rem;
        asm("divl %4\n"
            : "=a" (quot), "=d" (rem)
            : "d" (x_high), "a" (x_low), "r" (m));
        return rem;
    }

    _m_int& operator*=(const _m_int &other) {
        val = fast_mod((uint64_t) val * other.val);
        return *this;
    }

    _m_int& operator/=(const _m_int &other) {
        return *this *= other.inv();
    }

    friend _m_int operator+(const _m_int &a, const _m_int &b) { return _m_int(a) += b; }
    friend _m_int operator-(const _m_int &a, const _m_int &b) { return _m_int(a) -= b; }
    friend _m_int operator*(const _m_int &a, const _m_int &b) { return _m_int(a) *= b; }
    friend _m_int operator/(const _m_int &a, const _m_int &b) { return _m_int(a) /= b; }

    _m_int& operator++() {
        val = val == MOD - 1 ? 0 : val + 1;
        return *this;
    }

    _m_int& operator--() {
        val = val == 0 ? MOD - 1 : val - 1;
        return *this;
    }

    _m_int operator++(int) { _m_int before = *this; ++*this; return before; }
    _m_int operator--(int) { _m_int before = *this; --*this; return before; }

    _m_int operator-() const {
        return val == 0 ? 0 : MOD - val;
    }

    bool operator==(const _m_int &other) const { return val == other.val; }
    bool operator!=(const _m_int &other) const { return val != other.val; }

    _m_int inv() const {
        return inv_mod(val);
    }

    _m_int pow(int64_t p) const {
        if (p < 0)
            return inv().pow(-p);

        _m_int a = *this, result = 1;

        while (p > 0) {
            if (p & 1)
                result *= a;

            a *= a;
            p >>= 1;
        }

        return result;
    }

    friend ostream& operator<<(ostream &os, const _m_int &m) {
        return os << m.val;
    }
};

int64_t EVEN_MOD = 1 << 20;
using even_int = _m_int<EVEN_MOD>;

int64_t ODD_MOD = EVEN_MOD + 1;
using odd_int = _m_int<ODD_MOD>;

// https://csacademy.com/blog/fast-fourier-transform-and-variations-of-it
namespace walsh_hadamard {
    bool is_power_of_two(int n) {
        return (n & (n - 1)) == 0;
    }

    int round_up_power_two(int n) {
        while (n & (n - 1))
            n = (n | (n - 1)) + 1;

        return max(n, 1);
    }

    template<typename T>
    typename enable_if<is_integral<T>::value>::type divide_all(int n, vector<T> &values) {
        for (int i = 0; i < n; i++)
            values[i] >>= __builtin_ctz(n);
    }

    template<typename T>
    typename enable_if<!is_integral<T>::value>::type divide_all(int n, vector<T> &values) {
        T inv_n = T(1) / n;

        for (int i = 0; i < n; i++)
            values[i] *= inv_n;
    }

    template<typename T>
    void fwht_xor(int n, vector<T> &values, bool inverse = false) {
        assert(is_power_of_two(n));
        assert((int) values.size() >= n);

        for (int len = 1; len < n; len *= 2)
            for (int start = 0; start < n; start += 2 * len)
                for (int i = start; i < start + len; i++) {
                    T u = values[i];
                    const T &v = values[i + len];
                    values[i] = u + v;
                    values[i + len] = u - v;
                }

        if (inverse)
            divide_all(n, values);
    }

    // Warning: make sure to select the type `T_out` so that it can contain N^2 * max(left) * max(right).
    // One option is to use doubles and round at the end, since we are simply dividing by a power of two to finish.
    template<typename T_out, typename T_in>
    vector<T_out> xor_convolution(const vector<T_in> &_left, const vector<T_in> &_right) {
        if (_left.empty() || _right.empty())
            return {};

        vector<T_out> left(_left.begin(), _left.end());
        vector<T_out> right(_right.begin(), _right.end());

        int n = left.size();
        int m = right.size();

        int N = round_up_power_two(max(n, m));
        left.resize(N, 0);
        right.resize(N, 0);

        if (left == right) {
            fwht_xor(N, left);
            right = left;
        } else {
            fwht_xor(N, left);
            fwht_xor(N, right);
        }

        for (int i = 0; i < N; i++)
            left[i] *= right[i];

        fwht_xor(N, left, true);
        return left;
    }
}

int64_t inv_mod(int64_t a, int64_t m) {
    // https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm#Example
    int64_t g = m, r = a, x = 0, y = 1;

    while (r != 0) {
        int64_t q = g / r;
        g %= r; swap(g, r);
        x -= q * y; swap(x, y);
    }

    assert(g == 1);
    assert(y == m || y == -m);
    return x < 0 ? x + m : x;
}

// Returns a number that is a1 mod m1 and a2 mod m2. Assumes m1 and m2 are relatively prime.
int64_t chinese_remainder_theorem(int64_t a1, int64_t m1, int64_t a2, int64_t m2) {
    // assert(__gcd(m1, m2) == 1);
    int64_t k = (a2 - a1) % m2 * inv_mod(m1, m2) % m2;
    int64_t result = a1 + k * m1;

    if (result < 0)
        result += m1 * m2;

    assert(0 <= result && result < m1 * m2);
    assert(result % m1 == a1 && result % m2 == a2);
    return result;
}

int main() {
    ios::sync_with_stdio(false);
#ifndef NEAL_DEBUG
    cin.tie(nullptr);
#endif

    int N, M;
    int64_t T;
    cin >> M >> T >> ODD_MOD;
    N = 1 << M;
    EVEN_MOD = N;

    while (ODD_MOD % 2 == 0) {
        ODD_MOD /= 2;
        EVEN_MOD *= 2;
    }

    vector<even_int> E_even(N);
    vector<odd_int> E_odd(N);

    for (int i = 0; i < N; i++) {
        int e;
        cin >> e;
        E_even[i] = e;
        E_odd[i] = e;
    }

    vector<int> B(M + 1);

    for (int i = 0; i <= M; i++)
        cin >> B[i];

    vector<even_int> transition_even(N, 0);
    vector<odd_int> transition_odd(N, 0);

    for (int i = 0; i < N; i++) {
        transition_even[i] = B[__builtin_popcount(i)];
        transition_odd[i] = B[__builtin_popcount(i)];
    }

    walsh_hadamard::fwht_xor(N, E_even);
    walsh_hadamard::fwht_xor(N, E_odd);
    walsh_hadamard::fwht_xor(N, transition_even);
    walsh_hadamard::fwht_xor(N, transition_odd);

    for (int i = 0; i < N; i++) {
        E_even[i] *= transition_even[i].pow(T);
        E_odd[i] *= transition_odd[i].pow(T);
    }

    walsh_hadamard::fwht_xor(N, E_even);
    walsh_hadamard::fwht_xor(N, E_odd, true);

    dbg(EVEN_MOD, ODD_MOD);

    for (int i = 0; i < N; i++) {
        assert(E_even[i].val % N == 0);
        dbg(i, E_even[i], E_odd[i]);
        E_even[i].val /= N;
        dbg(i, E_even[i], E_odd[i]);
        int64_t answer = chinese_remainder_theorem(E_even[i].val, EVEN_MOD / N, E_odd[i].val, ODD_MOD);
        cout << answer << '\n';
    }
}


#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>
using namespace std;

template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { os << '{'; string sep; for (const auto &x : v) os << sep << x, sep = ", "; return os << '}'; }
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }

void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }

#ifdef NEAL_DEBUG
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

template<const int64_t &MOD>
struct _m_int {
    int64_t val;

    _m_int(int64_t v = 0) {
        if (v < 0) v = v % MOD + MOD;
        if (v >= MOD) v %= MOD;
        val = v;
    }

    static int inv_mod(int a, int m = MOD) {
        // https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm#Example
        int g = m, r = a, x = 0, y = 1;

        while (r != 0) {
            int q = g / r;
            g %= r; swap(g, r);
            x -= q * y; swap(x, y);
        }

        return x < 0 ? x + m : x;
    }

    explicit operator int() const { return val; }
    explicit operator int64_t() const { return val; }

    _m_int& operator+=(const _m_int &other) {
        val -= MOD - other.val;
        if (val < 0) val += MOD;
        return *this;
    }

    _m_int& operator-=(const _m_int &other) {
        val -= other.val;
        if (val < 0) val += MOD;
        return *this;
    }

    static unsigned fast_mod(uint64_t x, unsigned m = MOD) {
#if !defined(_WIN32) || defined(_WIN64)
        return x % m;
#endif
        // Optimized mod for Codeforces 32-bit machines.
        // x must be less than 2^32 * m for this to work, so that x / m fits in a 32-bit integer.
        unsigned x_high = x >> 32, x_low = (unsigned) x;
        unsigned quot, rem;
        asm("divl %4\n"
            : "=a" (quot), "=d" (rem)
            : "d" (x_high), "a" (x_low), "r" (m));
        return rem;
    }

    _m_int& operator*=(const _m_int &other) {
        val = fast_mod((uint64_t) val * other.val);
        return *this;
    }

    _m_int& operator/=(const _m_int &other) {
        return *this *= other.inv();
    }

    friend _m_int operator+(const _m_int &a, const _m_int &b) { return _m_int(a) += b; }
    friend _m_int operator-(const _m_int &a, const _m_int &b) { return _m_int(a) -= b; }
    friend _m_int operator*(const _m_int &a, const _m_int &b) { return _m_int(a) *= b; }
    friend _m_int operator/(const _m_int &a, const _m_int &b) { return _m_int(a) /= b; }

    _m_int& operator++() {
        val = val == MOD - 1 ? 0 : val + 1;
        return *this;
    }

    _m_int& operator--() {
        val = val == 0 ? MOD - 1 : val - 1;
        return *this;
    }

    _m_int operator++(int) { _m_int before = *this; ++*this; return before; }
    _m_int operator--(int) { _m_int before = *this; --*this; return before; }

    _m_int operator-() const {
        return val == 0 ? 0 : MOD - val;
    }

    bool operator==(const _m_int &other) const { return val == other.val; }
    bool operator!=(const _m_int &other) const { return val != other.val; }

    _m_int inv() const {
        return inv_mod(val);
    }

    _m_int pow(int64_t p) const {
        if (p < 0)
            return inv().pow(-p);

        _m_int a = *this, result = 1;

        while (p > 0) {
            if (p & 1)
                result *= a;

            a *= a;
            p >>= 1;
        }

        return result;
    }

    friend ostream& operator<<(ostream &os, const _m_int &m) {
        return os << m.val;
    }
};

int64_t MOD = 998244353;
using mod_int = _m_int<MOD>;

// https://csacademy.com/blog/fast-fourier-transform-and-variations-of-it
namespace walsh_hadamard {
    bool is_power_of_two(int n) {
        return (n & (n - 1)) == 0;
    }

    int round_up_power_two(int n) {
        while (n & (n - 1))
            n = (n | (n - 1)) + 1;

        return max(n, 1);
    }

    template<typename T>
    typename enable_if<is_integral<T>::value>::type divide_all(int n, vector<T> &values) {
        for (int i = 0; i < n; i++)
            values[i] >>= __builtin_ctz(n);
    }

    template<typename T>
    typename enable_if<!is_integral<T>::value>::type divide_all(int n, vector<T> &values) {
        T inv_n = T(1) / n;

        for (int i = 0; i < n; i++)
            values[i] *= inv_n;
    }

    template<typename T>
    void fwht_xor(int n, vector<T> &values, bool inverse = false) {
        assert(is_power_of_two(n));
        assert((int) values.size() >= n);

        for (int len = 1; len < n; len *= 2)
            for (int start = 0; start < n; start += 2 * len)
                for (int i = start; i < start + len; i++) {
                    T u = values[i];
                    const T &v = values[i + len];
                    values[i] = u + v;
                    values[i + len] = u - v;
                }

        if (inverse)
            divide_all(n, values);
    }

    // Warning: make sure to select the type `T_out` so that it can contain N^2 * max(left) * max(right).
    // One option is to use doubles and round at the end, since we are simply dividing by a power of two to finish.
    template<typename T_out, typename T_in>
    vector<T_out> xor_convolution(const vector<T_in> &_left, const vector<T_in> &_right) {
        if (_left.empty() || _right.empty())
            return {};

        vector<T_out> left(_left.begin(), _left.end());
        vector<T_out> right(_right.begin(), _right.end());

        int n = left.size();
        int m = right.size();

        int N = round_up_power_two(max(n, m));
        left.resize(N, 0);
        right.resize(N, 0);

        if (left == right) {
            fwht_xor(N, left);
            right = left;
        } else {
            fwht_xor(N, left);
            fwht_xor(N, right);
        }

        for (int i = 0; i < N; i++)
            left[i] *= right[i];

        fwht_xor(N, left, true);
        return left;
    }
}

int main() {
    ios::sync_with_stdio(false);
#ifndef NEAL_DEBUG
    cin.tie(nullptr);
#endif

    int N, M;
    int64_t T;
    cin >> M >> T >> MOD;
    N = 1 << M;
    MOD *= N;
    vector<mod_int> E(N);

    for (int i = 0; i < N; i++) {
        int e;
        cin >> e;
        E[i] = e;
    }

    vector<mod_int> B(M + 1);

    for (int i = 0; i <= M; i++) {
        int b;
        cin >> b;
        B[i] = b;
    }

    vector<mod_int> transition(N, 0);

    for (int i = 0; i < N; i++)
        transition[i] = B[__builtin_popcount(i)];

    walsh_hadamard::fwht_xor(N, E);
    walsh_hadamard::fwht_xor(N, transition);

    for (int i = 0; i < N; i++) {
        transition[i] = transition[i].pow(T);
        E[i] *= transition[i];
    }

    walsh_hadamard::fwht_xor(N, E);

    for (int i = 0; i < N; i++)
        cout << E[i].val / N << '\n';
}