#include<bits/stdc++.h>

using namespace std;

const int maxM=100000+5;
const int INF=0x3f3f3f3f;

int ch[maxM][2],fa[maxM],sz[maxM],cnt[maxM];
long long w2[maxM],W[maxM],w[maxM];
struct Node
{
	long long a,b;
	bool operator < (const Node &p) const
	{
		return b==p.b ? a<p.a : b<p.b;
	}
}a[maxM];
int n,m,rt,tot;

void pushup(int x)
{
	sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+cnt[x];
	W[x]=W[ch[x][0]]+W[ch[x][1]]+cnt[x]*w2[x];
}

int chk(int x) { return ch[fa[x]][1]==x; }

void rotate(int x)
{
	int y=fa[x],z=fa[y],k=chk(x);
	ch[y][k]=ch[x][k^1]; fa[ch[x][k^1]]=y;
	ch[z][chk(y)]=x; fa[x]=z;
	ch[x][k^1]=y; fa[y]=x;
	pushup(y); pushup(x);
}

void splay(int x,int goal=0)
{
	while (fa[x]!=goal)
	{
		int y=fa[x],z=fa[y];
		if (z!=goal)
		{
			if (chk(x)==chk(y)) rotate(y);
			else rotate(x);
		}
		rotate(x);
	}
	if (!goal) rt=x;
}

void insert(int val)
{
	int u=rt,p=0;
	while (u && w[u]!=val)
	{
		p=u; u=ch[u][val>w[u]];
	}
	if (w[u]==val)
	{
		++cnt[u]; splay(u);
	}
	else
	{
		u=++tot;
		if (p) { ch[p][val>w[p]]=u; fa[u]=p; }
		w[u]=val;
		if (val!=INF && val!=-INF)
		{
			sz[u]=cnt[u]=1; w2[u]=val;
		}
		splay(u);
	}
}

pair<int, long long> query(int val)
{
	int u=rt;
	while (ch[u][val>w[u]] && w[u]!=val) u=ch[u][val>w[u]];
	splay(u); //printf("%lld\n",w2[u]);
	if (w[u]>val) return make_pair(cnt[u]+sz[ch[u][1]],cnt[u]*w2[u]+W[ch[u][1]]);
	u=ch[u][1];
	while (ch[u][0]) u=ch[u][0];
	splay(u);
	return make_pair(cnt[u]+sz[ch[u][1]],cnt[u]*w2[u]+W[ch[u][1]]);
}

long long query2(int k)
{
	int u=rt; long long res=0;
	if (sz[u]<k) return 0;
	while (k)
	{
		if (k<=sz[ch[u][1]]) u=ch[u][1];
		else if (k>cnt[u]+sz[ch[u][1]])
		{
			k-=cnt[u]+sz[ch[u][1]];
			res+=cnt[u]*w2[u]+W[ch[u][1]];
			u=ch[u][0];
		}
		else
		{
			res+=(k-sz[ch[u][1]])*w2[u]+W[ch[u][1]];
			splay(u); return res;
		}
		if (!w2[u]) return res;
	}
	return res;
}

int main()
{
	int T; scanf("%d",&T);
	while (T--)
	{
		scanf("%d%d",&n,&m);
		for (int i=1;i<=m;++i) scanf("%I64d%I64d",&a[i].a,&a[i].b);
		sort(a+1,a+m+1);
		tot=0; rt=0;
		for (int i=1;i<=m+2;++i)
			ch[i][0]=ch[i][1]=fa[i]=sz[i]=cnt[i]=w[i]=w2[i]=W[i]=0;
		insert(-INF); insert(INF);
		long long ans=0;
		for (int i=1;i<=m;++i)
		{
			pair<int, long long> que=query(a[i].b);
			int num=que.first; long long res=que.second;
			//printf("%d %d %lld\n",i,num,res);
			if (!num)
			{
				ans=max(a[i].a+a[i].b*(n-1),ans);
			}
			else if (num<=n-2)
				ans=max(res+a[i].a+(n-num-1)*a[i].b,ans);
			else ans=max(query2(n-2)+a[i].a+a[i].b,ans);
			insert(a[i].a);
		}
		printf("%I64d\n",ans);
	}
	/*
	insert(-INF); insert(INF);
	insert(5); insert(2);
	pair<int, long long> que=query(4);
	printf("%d %lld\n",que.first,que.second);
	//printf("%lld\n",query2(8));*/
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

const int maxM=100000+5;
const int INF=0x3f3f3f3f;

int ch[maxM][2],fa[maxM],sz[maxM],cnt[maxM];
long long w2[maxM],W[maxM],w[maxM];
struct Node
{
	long long a,b;
	bool operator < (const Node &p) const
	{
		return b==p.b ? a<p.a : b<p.b;
	}
}a[maxM];
int n,m,rt,tot;

void pushup(int x)
{
	sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+cnt[x];
	W[x]=W[ch[x][0]]+W[ch[x][1]]+cnt[x]*w2[x];
}

int chk(int x) { return ch[fa[x]][1]==x; }

void rotate(int x)
{
	int y=fa[x],z=fa[y],k=chk(x);
	ch[y][k]=ch[x][k^1]; fa[ch[x][k^1]]=y;
	ch[z][chk(y)]=x; fa[x]=z;
	ch[x][k^1]=y; fa[y]=x;
	pushup(y); pushup(x);
}

void splay(int x,int goal=0)
{
	while (fa[x]!=goal)
	{
		int y=fa[x],z=fa[y];
		if (z!=goal)
		{
			if (chk(x)==chk(y)) rotate(y);
			else rotate(x);
		}
		rotate(x);
	}
	if (!goal) rt=x;
}

void insert(int val)
{
	int u=rt,p=0;
	while (u && w[u]!=val)
	{
		p=u; u=ch[u][val>w[u]];
	}
	if (w[u]==val)
	{
		++cnt[u]; splay(u);
	}
	else
	{
		u=++tot;
		if (p) { ch[p][val>w[p]]=u; fa[u]=p; }
		w[u]=val;
		if (val!=INF && val!=-INF)
		{
			sz[u]=cnt[u]=1; w2[u]=val;
		}
		splay(u);
	}
}

pair<int, long long> query(int val)
{
	int u=rt;
	while (ch[u][val>w[u]] && w[u]!=val) u=ch[u][val>w[u]];
	splay(u); //printf("%lld\n",w2[u]);
	if (w[u]>val) return make_pair(cnt[u]+sz[ch[u][1]],cnt[u]*w2[u]+W[ch[u][1]]);
	u=ch[u][1];
	while (ch[u][0]) u=ch[u][0];
	splay(u);
	return make_pair(cnt[u]+sz[ch[u][1]],cnt[u]*w2[u]+W[ch[u][1]]);
}

long long query2(int k)
{
	int u=rt; long long res=0;
	while (k)
	{
		if (k<=sz[ch[u][1]]) u=ch[u][1];
		else if (w2[u]!=0 && k>cnt[u]+sz[ch[u][1]])
		{
			k-=cnt[u]+sz[ch[u][1]];
			res+=cnt[u]*w2[u]+W[ch[u][1]];
			u=ch[u][0];
		}
		else
		{
			res+=(k-sz[ch[u][1]])*w2[u]+W[ch[u][1]];
			splay(u); return res;
		}
	}
}

int main()
{
	int T; scanf("%d",&T);
	while (T--)
	{
		scanf("%d%d",&n,&m);
		for (int i=1;i<=m;++i) scanf("%I64d%I64d",&a[i].a,&a[i].b);
		sort(a+1,a+m+1);
		tot=0; rt=0;
		for (int i=1;i<=m+2;++i)
			ch[i][0]=ch[i][1]=fa[i]=sz[i]=cnt[i]=w[i]=w2[i]=W[i]=0;
		insert(-INF); insert(INF);
		long long ans=0;
		for (int i=1;i<=m;++i)
		{
			pair<int, long long> que=query(a[i].b);
			int num=que.first; long long res=que.second;
			//printf("%d %d %lld\n",i,num,res);
			if (!num)
			{
				ans=max(a[i].a+a[i].b*(n-1),ans);
			}
			else if (num<=n-2)
				ans=max(res+a[i].a+(n-num-1)*a[i].b,ans);
			else ans=max(query2(n-2)+a[i].a+a[i].b,ans);
			insert(a[i].a);
		}
		printf("%I64d\n",ans);
	}/*

	insert(-INF); insert(INF);
	for (int i=1;i<=10;++i) insert(i);
	insert(5); insert(6);
	printf("%lld\n",query2(9));*/
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

const int maxM=100000+5;
const int INF=0x3f3f3f3f;

int ch[maxM][2],fa[maxM],sz[maxM],cnt[maxM];
long long w2[maxM],W[maxM],w[maxM];
struct Node
{
	long long a,b;
	bool operator < (const Node &p) const
	{
		return b==p.b ? a<p.a : b<p.b;
	}
}a[maxM];
int n,m,rt,tot;

void pushup(int x)
{
	sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+cnt[x];
	W[x]=W[ch[x][0]]+W[ch[x][1]]+cnt[x]*w2[x];
}

int chk(int x) { return ch[fa[x]][1]==x; }

void rotate(int x)
{
	int y=fa[x],z=fa[y],k=chk(x);
	ch[y][k]=ch[x][k^1]; fa[ch[x][k^1]]=y;
	ch[z][chk(y)]=x; fa[x]=z;
	ch[x][k^1]=y; fa[y]=x;
	pushup(y); pushup(x);
}

void splay(int x,int goal=0)
{
	while (fa[x]!=goal)
	{
		int y=fa[x],z=fa[y];
		if (z!=goal)
		{
			if (chk(x)==chk(y)) rotate(y);
			else rotate(x);
		}
		rotate(x);
	}
	if (!goal) rt=x;
}

void insert(int val)
{
	int u=rt,p=0;
	while (u && w[u]!=val)
	{
		p=u; u=ch[u][val>w[u]];
	}
	if (w[u]==val)
	{
		++cnt[u]; splay(u);
	}
	else
	{
		u=++tot;
		if (p) { ch[p][val>w[p]]=u; fa[u]=p; }
		w[u]=val;
		if (val!=INF && val!=-INF)
		{
			sz[u]=cnt[u]=1; w2[u]=val;
		}
		splay(u);
	}
}

pair<int, long long> query(int val)
{
	int u=rt;
	while (ch[u][val>w[u]] && w[u]!=val) u=ch[u][val>w[u]];
	splay(u); //printf("%lld\n",w2[u]);
	if (w[u]>val) return make_pair(cnt[u]+sz[ch[u][1]],cnt[u]*w2[u]+W[ch[u][1]]);
	u=ch[u][1];
	while (ch[u][0]) u=ch[u][0];
	splay(u);
	return make_pair(cnt[u]+sz[ch[u][1]],cnt[u]*w2[u]+W[ch[u][1]]);
}

long long query2(int k)
{
	int u=rt; long long res=0;
	while (k)
	{
		if (k<=sz[ch[u][1]]) u=ch[u][1];
		else if (k>cnt[u]+sz[ch[u][1]])
		{
			k-=cnt[u]+sz[ch[u][1]];
			res+=cnt[u]*w2[u]+W[ch[u][1]];
			u=ch[u][0];
		}
		else
		{
			res+=(k-sz[ch[u][1]])*w2[u]+W[ch[u][1]];
			splay(u); return res;
		}
	}
}

int main()
{
	int T; scanf("%d",&T);
	while (T--)
	{
		scanf("%d%d",&n,&m);
		for (int i=1;i<=m;++i) scanf("%I64d%I64d",&a[i].a,&a[i].b);
		sort(a+1,a+m+1);
		tot=0; rt=0;
		for (int i=1;i<=m+2;++i)
			ch[i][0]=ch[i][1]=fa[i]=sz[i]=cnt[i]=w[i]=w2[i]=W[i]=0;
		insert(-INF); insert(INF);
		long long ans=0;
		for (int i=1;i<=m;++i)
		{
			pair<int, long long> que=query(a[i].b);
			int num=que.first; long long res=que.second;
			//printf("%d %d %lld\n",i,num,res);
			if (!num)
			{
				ans=max(a[i].a+a[i].b*(n-1),ans);
			}
			else if (num<=n-2)
				ans=max(res+a[i].a+(n-num-1)*a[i].b,ans);
			else ans=max(query2(n-2)+a[i].a+a[i].b,ans);
			insert(a[i].a);
		}
		printf("%I64d\n",ans);
	}
	/*
	insert(-INF); insert(INF);
	insert(5); insert(2);
	pair<int, long long> que=query(4);
	printf("%d %lld\n",que.first,que.second);
	//printf("%lld\n",query2(8));*/
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

const int maxM=100000+5;
const int INF=0x3f3f3f3f;

int ch[maxM][2],fa[maxM],sz[maxM],cnt[maxM];
long long w2[maxM],W[maxM],w[maxM];
struct Node
{
	long long a,b;
	bool operator < (const Node &p) const
	{
		return b==p.b ? a<p.a : b<p.b;
	}
}a[maxM];
int n,m,rt,tot;

void pushup(int x)
{
	sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+cnt[x];
	W[x]=W[ch[x][0]]+W[ch[x][1]]+cnt[x]*w2[x];
}

int chk(int x) { return ch[fa[x]][1]==x; }

void rotate(int x)
{
	int y=fa[x],z=fa[y],k=chk(x);
	ch[y][k]=ch[x][k^1]; fa[ch[x][k^1]]=y;
	ch[z][chk(y)]=x; fa[x]=z;
	ch[x][k^1]=y; fa[y]=x;
	pushup(y); pushup(x);
}

void splay(int x,int goal=0)
{
	while (fa[x]!=goal)
	{
		int y=fa[x],z=fa[y];
		if (z!=goal)
		{
			if (chk(x)==chk(y)) rotate(y);
			else rotate(x);
		}
		rotate(x);
	}
	if (!goal) rt=x;
}

void insert(int val)
{
	int u=rt,p=0;
	while (u && w[u]!=val)
	{
		p=u; u=ch[u][val>w[u]];
	}
	if (w[u]==val)
	{
		++cnt[u]; splay(u);
	}
	else
	{
		u=++tot;
		if (p) { ch[p][val>w[p]]=u; fa[u]=p; }
		w[u]=val; cnt[u]=1; sz[u]=1;
		if (val!=INF && val!=-INF)
		{
			 w2[u]=val;
		}
		splay(u);
	}
}

pair<int, long long> query(int val)
{
	int u=rt;
	while (ch[u][val>w[u]] && w[u]!=val) u=ch[u][val>w[u]];
	splay(u); //printf("%lld\n",w2[u]);
	if (w[u]>val) return make_pair(cnt[u]+sz[ch[u][1]]-1,cnt[u]*w2[u]+W[ch[u][1]]);
	u=ch[u][1];
	while (ch[u][0]) u=ch[u][0];
	splay(u);
	return make_pair(cnt[u]+sz[ch[u][1]]-1,cnt[u]*w2[u]+W[ch[u][1]]);
}

long long query2(int k)
{
	int u=rt; long long res=0;
	while (k)
	{
		if (k<=sz[ch[u][1]]) u=ch[u][1];
		else if (k>cnt[u]+sz[ch[u][1]])
		{
			k-=cnt[u]+sz[ch[u][1]];
			res+=cnt[u]*w2[u]+W[ch[u][1]];
			u=ch[u][0];
		}
		else
		{
			res+=(k-sz[ch[u][1]])*w2[u]+W[ch[u][1]];
			splay(u); return res;
		}
	}
}

int main()
{
	int T; scanf("%d",&T);
	while (T--)
	{
		scanf("%d%d",&n,&m);
		for (int i=1;i<=m;++i) scanf("%I64d%I64d",&a[i].a,&a[i].b);
		sort(a+1,a+m+1);
		tot=0; rt=0;
		for (int i=1;i<=m+2;++i)
			ch[i][0]=ch[i][1]=fa[i]=sz[i]=cnt[i]=w[i]=w2[i]=W[i]=0;
		insert(-INF); insert(INF);
		long long ans=0;
		for (int i=1;i<=m;++i)
		{
			if (sz[rt]-2>=n) ans=max(query2(n+1),ans);
			pair<int, long long> que=query(a[i].b);
			int num=que.first; long long res=que.second;
			//printf("%d %d %lld\n",i,num,res);
			ans=max(a[i].a+a[i].b*(n-1),ans);
			if (num<=n-2)
				ans=max(res+a[i].a+(n-num-1)*a[i].b,ans);
			else if (n-2>=0)
			{
				long long tmp=query2(n-2+1)+a[i].a+a[i].b;
				ans=max(tmp,ans);// printf("%d %I64d\n",i,tmp);
			}
			insert(a[i].a);
		}
		printf("%I64d\n",ans);
	}/*

	insert(-INF); insert(INF);
	for (int i=1;i<=10;++i) insert(i);
	insert(5); insert(6);
	printf("%lld\n",query2(9));*/
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

const int maxM=100000+5;
const int INF=0x3f3f3f3f;

int ch[maxM][2],fa[maxM],sz[maxM],cnt[maxM];
long long w2[maxM],W[maxM],w[maxM];
struct Node
{
	long long a,b;
	bool operator < (const Node &p) const
	{
		return b==p.b ? a<p.a : b<p.b;
	}
}a[maxM];
int n,m,rt,tot;

void pushup(int x)
{
	sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+cnt[x];
	W[x]=W[ch[x][0]]+W[ch[x][1]]+cnt[x]*w2[x];
}

int chk(int x) { return ch[fa[x]][1]==x; }

void rotate(int x)
{
	int y=fa[x],z=fa[y],k=chk(x);
	ch[y][k]=ch[x][k^1]; fa[ch[x][k^1]]=y;
	ch[z][chk(y)]=x; fa[x]=z;
	ch[x][k^1]=y; fa[y]=x;
	pushup(y); pushup(x);
}

void splay(int x,int goal=0)
{
	while (fa[x]!=goal)
	{
		int y=fa[x],z=fa[y];
		if (z!=goal)
		{
			if (chk(x)==chk(y)) rotate(y);
			else rotate(x);
		}
		rotate(x);
	}
	if (!goal) rt=x;
}

void insert(int val)
{
	int u=rt,p=0;
	while (u && w[u]!=val)
	{
		p=u; u=ch[u][val>w[u]];
	}
	if (w[u]==val)
	{
		++cnt[u]; splay(u);
	}
	else
	{
		u=++tot;
		if (p) { ch[p][val>w[p]]=u; fa[u]=p; }
		w[u]=val; cnt[u]=1;
		if (val!=INF && val!=-INF)
		{
			sz[u]=1; w2[u]=val;
		}
		splay(u);
	}
}

pair<int, long long> query(int val)
{
	int u=rt;
	while (ch[u][val>w[u]] && w[u]!=val) u=ch[u][val>w[u]];
	splay(u); //printf("%lld\n",w2[u]);
	if (w[u]>val) return make_pair(cnt[u]+sz[ch[u][1]]-1,cnt[u]*w2[u]+W[ch[u][1]]);
	u=ch[u][1];
	while (ch[u][0]) u=ch[u][0];
	splay(u);
	return make_pair(cnt[u]+sz[ch[u][1]]-1,cnt[u]*w2[u]+W[ch[u][1]]);
}

long long query2(int k)
{
	int u=rt; long long res=0;
	while (k)
	{
		if (k<=sz[ch[u][1]]) u=ch[u][1];
		else if (k>cnt[u]+sz[ch[u][1]])
		{
			k-=cnt[u]+sz[ch[u][1]];
			res+=cnt[u]*w2[u]+W[ch[u][1]];
			u=ch[u][0];
		}
		else
		{
			res+=(k-sz[ch[u][1]])*w2[u]+W[ch[u][1]];
			splay(u); return res;
		}
	}
}

int main()
{
	int T; scanf("%d",&T);
	while (T--)
	{
		scanf("%d%d",&n,&m);
		for (int i=1;i<=m;++i) scanf("%I64d%I64d",&a[i].a,&a[i].b);
		sort(a+1,a+m+1);
		tot=0; rt=0;
		for (int i=1;i<=m+2;++i)
			ch[i][0]=ch[i][1]=fa[i]=sz[i]=cnt[i]=w[i]=w2[i]=W[i]=0;
		insert(-INF); insert(INF);
		long long ans=0;
		for (int i=1;i<=m;++i)
		{
			pair<int, long long> que=query(a[i].b);
			int num=que.first; long long res=que.second;
			//printf("%d %d %lld\n",i,num,res);
			if (!num)
			{
				ans=max(a[i].a+a[i].b*(n-1),ans);
			}
			else if (num<=n-2)
				ans=max(res+a[i].a+(n-num-1)*a[i].b,ans);
			else ans=max(query2(n-2+1)+a[i].a+a[i].b,ans);
			insert(a[i].a);
			sz[0]=0;
		}
		printf("%I64d\n",ans);
	}/*

	insert(-INF); insert(INF);
	for (int i=1;i<=10;++i) insert(i);
	insert(5); insert(6);
	printf("%lld\n",query2(9));*/
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

const int maxM=100000+5;
const int INF=0x3f3f3f3f;

int ch[maxM][2],fa[maxM],sz[maxM],cnt[maxM];
long long w2[maxM],W[maxM],w[maxM];
struct Node
{
	long long a,b;
	bool operator < (const Node &p) const
	{
		return b==p.b ? a<p.a : b<p.b;
	}
}a[maxM];
int n,m,rt,tot;

void pushup(int x)
{
	sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+cnt[x];
	W[x]=W[ch[x][0]]+W[ch[x][1]]+cnt[x]*w2[x];
}

int chk(int x) { return ch[fa[x]][1]==x; }

void rotate(int x)
{
	int y=fa[x],z=fa[y],k=chk(x);
	ch[y][k]=ch[x][k^1]; fa[ch[x][k^1]]=y;
	ch[z][chk(y)]=x; fa[x]=z;
	ch[x][k^1]=y; fa[y]=x;
	pushup(y); pushup(x);
}

void splay(int x,int goal=0)
{
	while (fa[x]!=goal)
	{
		int y=fa[x],z=fa[y];
		if (z!=goal)
		{
			if (chk(x)==chk(y)) rotate(y);
			else rotate(x);
		}
		rotate(x);
	}
	if (!goal) rt=x;
}

void insert(int val)
{
	int u=rt,p=0;
	while (u && w[u]!=val)
	{
		p=u; u=ch[u][val>w[u]];
	}
	if (w[u]==val)
	{
		++cnt[u]; splay(u);
	}
	else
	{
		u=++tot;
		if (p) { ch[p][val>w[p]]=u; fa[u]=p; }
		w[u]=val;
		if (val!=INF && val!=-INF)
		{
			sz[u]=cnt[u]=1; w2[u]=val;
		}
		splay(u);
	}
}

pair<int, long long> query(int val)
{
	int u=rt;
	while (ch[u][val>w[u]] && w[u]!=val) u=ch[u][val>w[u]];
	splay(u); //printf("%lld\n",w2[u]);
	if (w[u]>val) return make_pair(cnt[u]+sz[ch[u][1]],cnt[u]*w2[u]+W[ch[u][1]]);
	u=ch[u][1];
	while (ch[u][0]) u=ch[u][0];
	splay(u);
	return make_pair(cnt[u]+sz[ch[u][1]],cnt[u]*w2[u]+W[ch[u][1]]);
}

long long query2(int k)
{
	int u=rt; long long res=0;
	if (sz[u]<k) return 100000000000000;
	while (k)
	{
		if (k<=sz[ch[u][1]]) u=ch[u][1];
		else if (k>cnt[u]+sz[ch[u][1]])
		{
			k-=cnt[u]+sz[ch[u][1]];
			res+=cnt[u]*w2[u]+W[ch[u][1]];
			u=ch[u][0];
		}
		else
		{
			res+=(k-sz[ch[u][1]])*w2[u]+W[ch[u][1]];
			splay(u); return 100000000000000;
		}
	}
}

int main()
{
	int T; scanf("%d",&T);
	while (T--)
	{
		scanf("%d%d",&n,&m);
		for (int i=1;i<=m;++i) scanf("%I64d%I64d",&a[i].a,&a[i].b);
		sort(a+1,a+m+1);
		tot=0; rt=0;
		for (int i=1;i<=m+2;++i)
			ch[i][0]=ch[i][1]=fa[i]=sz[i]=cnt[i]=w[i]=w2[i]=W[i]=0;
		insert(-INF); insert(INF);
		long long ans=0;
		for (int i=1;i<=m;++i)
		{
			pair<int, long long> que=query(a[i].b);
			int num=que.first; long long res=que.second;
			//printf("%d %d %lld\n",i,num,res);
			if (!num)
			{
				ans=max(a[i].a+a[i].b*(n-1),ans);
			}
			else if (num<=n-2)
				ans=max(res+a[i].a+(n-num-1)*a[i].b,ans);
			else ans=max(query2(n-2)+a[i].a+a[i].b,ans);
			insert(a[i].a);
		}
		printf("%I64d\n",ans);
	}
	/*
	insert(-INF); insert(INF);
	insert(5); insert(2);
	pair<int, long long> que=query(4);
	printf("%d %lld\n",que.first,que.second);
	//printf("%lld\n",query2(8));*/
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

const int maxM=100000+5;
const int INF=0x3f3f3f3f;

int ch[maxM][2],fa[maxM],sz[maxM],cnt[maxM];
long long w2[maxM],W[maxM],w[maxM];
struct Node
{
	long long a,b;
	bool operator < (const Node &p) const
	{
		return b==p.b ? a<p.a : b<p.b;
	}
}a[maxM];
int n,m,rt,tot;

void pushup(int x)
{
	sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+cnt[x];
	W[x]=W[ch[x][0]]+W[ch[x][1]]+cnt[x]*w2[x];
}

int chk(int x) { return ch[fa[x]][1]==x; }

void rotate(int x)
{
	int y=fa[x],z=fa[y],k=chk(x);
	ch[y][k]=ch[x][k^1]; fa[ch[x][k^1]]=y;
	ch[z][chk(y)]=x; fa[x]=z;
	ch[x][k^1]=y; fa[y]=x;
	pushup(y); pushup(x);
}

void splay(int x,int goal=0)
{
	while (fa[x]!=goal)
	{
		int y=fa[x],z=fa[y];
		if (z!=goal)
		{
			if (chk(x)==chk(y)) rotate(y);
			else rotate(x);
		}
		rotate(x);
	}
	if (!goal) rt=x;
}

void insert(int val)
{
	int u=rt,p=0;
	while (u && w[u]!=val)
	{
		p=u; u=ch[u][val>w[u]];
	}
	if (w[u]==val)
	{
		++cnt[u]; splay(u);
	}
	else
	{
		u=++tot;
		if (p) { ch[p][val>w[p]]=u; fa[u]=p; }
		w[u]=val;
		if (val!=INF && val!=-INF)
		{
			sz[u]=cnt[u]=1; w2[u]=val;
		}
		splay(u);
	}
}

pair<int, long long> query(int val)
{
	int u=rt;
	while (ch[u][val>w[u]] && w[u]!=val) u=ch[u][val>w[u]];
	splay(u); //printf("%lld\n",w2[u]);
	if (w[u]>val) return make_pair(cnt[u]+sz[ch[u][1]],cnt[u]*w2[u]+W[ch[u][1]]);
	u=ch[u][1];
	while (ch[u][0]) u=ch[u][0];
	splay(u);
	return make_pair(cnt[u]+sz[ch[u][1]],cnt[u]*w2[u]+W[ch[u][1]]);
}

long long query2(int k)
{
	int u=rt; long long res=0;
	if (sz[u]-2<k) return 100000000000000;
	while (k)
	{
		if (k<=sz[ch[u][1]]) u=ch[u][1];
		else if (k>cnt[u]+sz[ch[u][1]])
		{
			k-=cnt[u]+sz[ch[u][1]];
			res+=cnt[u]*w2[u]+W[ch[u][1]];
			u=ch[u][0];
		}
		else
		{
			res+=(k-sz[ch[u][1]])*w2[u]+W[ch[u][1]];
			splay(u); return 100000000000000;
		}
	}
}

int main()
{
	int T; scanf("%d",&T);
	while (T--)
	{
		scanf("%d%d",&n,&m);
		for (int i=1;i<=m;++i) scanf("%I64d%I64d",&a[i].a,&a[i].b);
		sort(a+1,a+m+1);
		tot=0; rt=0;
		for (int i=1;i<=m+2;++i)
			ch[i][0]=ch[i][1]=fa[i]=sz[i]=cnt[i]=w[i]=w2[i]=W[i]=0;
		insert(-INF); insert(INF);
		long long ans=0;
		for (int i=1;i<=m;++i)
		{
			pair<int, long long> que=query(a[i].b);
			int num=que.first; long long res=que.second;
			//printf("%d %d %lld\n",i,num,res);
			if (!num)
			{
				ans=max(a[i].a+a[i].b*(n-1),ans);
			}
			else if (num<=n-2)
				ans=max(res+a[i].a+(n-num-1)*a[i].b,ans);
			else ans=max(query2(n-2)+a[i].a+a[i].b,ans);
			insert(a[i].a);
		}
		printf("%I64d\n",ans);
	}
	/*
	insert(-INF); insert(INF);
	insert(5); insert(2);
	pair<int, long long> que=query(4);
	printf("%d %lld\n",que.first,que.second);
	//printf("%lld\n",query2(8));*/
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

const int maxM=100000+5;
const int INF=0x3f3f3f3f;

int ch[maxM][2],fa[maxM],sz[maxM],cnt[maxM];
long long w2[maxM],W[maxM],w[maxM];
struct Node
{
	long long a,b;
	bool operator < (const Node &p) const
	{
		return b==p.b ? a<p.a : b<p.b;
	}
}a[maxM];
int n,m,rt,tot;

void pushup(int x)
{
	sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+cnt[x];
	W[x]=W[ch[x][0]]+W[ch[x][1]]+cnt[x]*w2[x];
}

int chk(int x) { return ch[fa[x]][1]==x; }

void rotate(int x)
{
	int y=fa[x],z=fa[y],k=chk(x);
	ch[y][k]=ch[x][k^1]; fa[ch[x][k^1]]=y;
	ch[z][chk(y)]=x; fa[x]=z;
	ch[x][k^1]=y; fa[y]=x;
	pushup(y); pushup(x);
}

void splay(int x,int goal=0)
{
	while (fa[x]!=goal)
	{
		int y=fa[x],z=fa[y];
		if (z!=goal)
		{
			if (chk(x)==chk(y)) rotate(y);
			else rotate(x);
		}
		rotate(x);
	}
	if (!goal) rt=x;
}

void insert(int val)
{
	int u=rt,p=0;
	while (u && w[u]!=val)
	{
		p=u; u=ch[u][val>w[u]];
	}
	if (w[u]==val)
	{
		++cnt[u]; splay(u);
	}
	else
	{
		u=++tot;
		if (p) { ch[p][val>w[p]]=u; fa[u]=p; }
		w[u]=val;
		if (val!=INF && val!=-INF)
		{
			sz[u]=cnt[u]=1; w2[u]=val;
		}
		splay(u);
	}
}

pair<int, long long> query(int val)
{
	int u=rt;
	while (ch[u][val>w[u]] && w[u]!=val) u=ch[u][val>w[u]];
	splay(u);
	if (w2[u]>val) return make_pair(cnt[u]+sz[ch[u][1]],cnt[u]*w2[u]+W[ch[u][1]]);
	u=ch[u][1];
	while (ch[u][0]) u=ch[u][0];
	splay(u);
	return make_pair(cnt[u]+sz[ch[u][1]],cnt[u]*w2[u]+W[ch[u][1]]);
}

long long query2(int k)
{
	int u=rt; long long res=0;
	while (k)
	{
		if (k<=sz[ch[u][1]]) u=ch[u][1];
		else if (k>cnt[u]+sz[ch[u][1]])
		{
			k-=cnt[u]+sz[ch[u][1]];
			res+=cnt[u]*w2[u]+W[ch[u][1]];
			u=ch[u][0];
		}
		else
		{
			res+=(k-sz[ch[u][1]])*w2[u]+W[ch[u][1]];
			splay(u); return res;
		}
	}
}

int main()
{
	int T; scanf("%d",&T);
	while (T--)
	{
		scanf("%d%d",&n,&m);
		for (int i=1;i<=m;++i) scanf("%I64d%I64d",&a[i].a,&a[i].b);
		sort(a+1,a+m+1);
		tot=0; rt=0;
		for (int i=1;i<=m+2;++i)
			ch[i][0]=ch[i][1]=fa[i]=sz[i]=cnt[i]=w[i]=w2[i]=W[i]=0;
		insert(-INF); insert(INF);
		long long ans=0;
		for (int i=1;i<=m;++i)
		{
			pair<int, long long> que=query(a[i].b);
			int num=que.first; long long res=que.second;
			//printf("%d %d %lld\n",i,num,res);
			ans=max(a[i].a+(n-1)*a[i].b,ans);
			if (num<=n-2)
				ans=max(res+a[i].a+(n-num-1)*a[i].b,ans);
			else ans=max(query2(n-2)+a[i].a+a[i].b,ans);
			insert(a[i].a);
		}
		printf("%I64d\n",ans);
	}
	/*
	insert(-INF); insert(INF);
	insert(5); insert(2);
	pair<int, long long> que=query(4);
	printf("%d %lld\n",que.first,que.second);
	//printf("%lld\n",query2(8));*/
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

const int maxM=100000+5;
const int INF=0x3f3f3f3f;

int ch[maxM][2],fa[maxM],sz[maxM],cnt[maxM];
long long w2[maxM],W[maxM],w[maxM];
struct Node
{
	long long a,b;
	bool operator < (const Node &p) const
	{
		return b==p.b ? a<p.a : b<p.b;
	}
}a[maxM];
int n,m,rt,tot;

void pushup(int x)
{
	sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+cnt[x];
	W[x]=W[ch[x][0]]+W[ch[x][1]]+cnt[x]*w2[x];
}

int chk(int x) { return ch[fa[x]][1]==x; }

void rotate(int x)
{
	int y=fa[x],z=fa[y],k=chk(x);
	ch[y][k]=ch[x][k^1]; fa[ch[x][k^1]]=y;
	ch[z][chk(y)]=x; fa[x]=z;
	ch[x][k^1]=y; fa[y]=x;
	pushup(y); pushup(x);
}

void splay(int x,int goal=0)
{
	while (fa[x]!=goal)
	{
		int y=fa[x],z=fa[y];
		if (z!=goal)
		{
			if (chk(x)==chk(y)) rotate(y);
			else rotate(x);
		}
		rotate(x);
	}
	if (!goal) rt=x;
}

void insert(int val)
{
	int u=rt,p=0;
	while (u && w[u]!=val)
	{
		p=u; u=ch[u][val>w[u]];
	}
	if (w[u]==val)
	{
		++cnt[u]; splay(u);
	}
	else
	{
		u=++tot;
		if (p) { ch[p][val>w[p]]=u; fa[u]=p; }
		w[u]=val;
		if (val!=INF && val!=-INF)
		{
			sz[u]=cnt[u]=1; w2[u]=val;
		}
		splay(u);
	}
}

pair<int, long long> query(int val)
{
	int u=rt;
	while (ch[u][val>w[u]] && w[u]!=val) u=ch[u][val>w[u]];
	splay(u); //printf("%lld\n",w2[u]);
	if (w[u]>val) return make_pair(cnt[u]+sz[ch[u][1]],cnt[u]*w2[u]+W[ch[u][1]]);
	u=ch[u][1];
	while (ch[u][0]) u=ch[u][0];
	splay(u);
	return make_pair(cnt[u]+sz[ch[u][1]],cnt[u]*w2[u]+W[ch[u][1]]);
}

long long query2(int k)
{
	int u=rt; long long res=0;
	if (sz[u]<k) return 0;
	while (k)
	{
		if (k<=sz[ch[u][1]]) u=ch[u][1];
		else if (k>cnt[u]+sz[ch[u][1]])
		{
			k-=cnt[u]+sz[ch[u][1]];
			res+=cnt[u]*w2[u]+W[ch[u][1]];
			u=ch[u][0];
		}
		else
		{
			res+=(k-sz[ch[u][1]])*w2[u]+W[ch[u][1]];
			splay(u); return res;
		}
		if (!w2[u]) return res;
	}
}

int main()
{
	int T; scanf("%d",&T);
	while (T--)
	{
		scanf("%d%d",&n,&m);
		for (int i=1;i<=m;++i) scanf("%I64d%I64d",&a[i].a,&a[i].b);
		sort(a+1,a+m+1);
		tot=0; rt=0;
		for (int i=1;i<=m+2;++i)
			ch[i][0]=ch[i][1]=fa[i]=sz[i]=cnt[i]=w[i]=w2[i]=W[i]=0;
		insert(-INF); insert(INF);
		long long ans=0;
		for (int i=1;i<=m;++i)
		{
			pair<int, long long> que=query(a[i].b);
			int num=que.first; long long res=que.second;
			//printf("%d %d %lld\n",i,num,res);
			if (!num)
			{
				ans=max(a[i].a+a[i].b*(n-1),ans);
			}
			else if (num<=n-2)
				ans=max(res+a[i].a+(n-num-1)*a[i].b,ans);
			else ans=max(query2(n-2)+a[i].a+a[i].b,ans);
			insert(a[i].a);
		}
		printf("%I64d\n",ans);
	}
	/*
	insert(-INF); insert(INF);
	insert(5); insert(2);
	pair<int, long long> que=query(4);
	printf("%d %lld\n",que.first,que.second);
	//printf("%lld\n",query2(8));*/
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

const int maxM=100000+5;
const int INF=0x3f3f3f3f;

int ch[maxM][2],fa[maxM],sz[maxM],cnt[maxM];
long long w2[maxM],W[maxM],w[maxM];
struct Node
{
	long long a,b;
	bool operator < (const Node &p) const
	{
		return b==p.b ? a<p.a : b<p.b;
	}
}a[maxM];
int n,m,rt,tot;

void pushup(int x)
{
	sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+cnt[x];
	W[x]=W[ch[x][0]]+W[ch[x][1]]+cnt[x]*w2[x];
}

int chk(int x) { return ch[fa[x]][1]==x; }

void rotate(int x)
{
	int y=fa[x],z=fa[y],k=chk(x);
	ch[y][k]=ch[x][k^1]; fa[ch[x][k^1]]=y;
	ch[z][chk(y)]=x; fa[x]=z;
	ch[x][k^1]=y; fa[y]=x;
	pushup(y); pushup(x);
}

void splay(int x,int goal=0)
{
	while (fa[x]!=goal)
	{
		int y=fa[x],z=fa[y];
		if (z!=goal)
		{
			if (chk(x)==chk(y)) rotate(y);
			else rotate(x);
		}
		rotate(x);
	}
	if (!goal) rt=x;
}

void insert(int val)
{
	int u=rt,p=0;
	while (u && w[u]!=val)
	{
		p=u; u=ch[u][val>w[u]];
	}
	if (w[u]==val)
	{
		++cnt[u]; splay(u);
	}
	else
	{
		u=++tot;
		if (p) { ch[p][val>w[p]]=u; fa[u]=p; }
		w[u]=val;
		if (val!=INF && val!=-INF)
		{
			sz[u]=cnt[u]=1; w2[u]=val;
		}
		splay(u);
	}
}

pair<int, long long> query(int val)
{
	int u=rt;
	while (ch[u][val>w[u]] && w[u]!=val) u=ch[u][val>w[u]];
	splay(u); //printf("%lld\n",w2[u]);
	if (w[u]>val) return make_pair(cnt[u]+sz[ch[u][1]],cnt[u]*w2[u]+W[ch[u][1]]);
	u=ch[u][1];
	while (ch[u][0]) u=ch[u][0];
	splay(u);
	return make_pair(cnt[u]+sz[ch[u][1]],cnt[u]*w2[u]+W[ch[u][1]]);
}

long long query2(int k)
{
	int u=rt; long long res=0;
	//if (sz[u]<k) return 0;
	while (u)
	{
		if (k<=sz[ch[u][1]]) u=ch[u][1];
		else if (k>cnt[u]+sz[ch[u][1]])
		{
			k-=cnt[u]+sz[ch[u][1]];
			res+=cnt[u]*w2[u]+W[ch[u][1]];
			u=ch[u][0];
		}
		else
		{
			res+=(k-sz[ch[u][1]])*w2[u]+W[ch[u][1]];
			splay(u); return res;
		}
	}
	return res;
}

int main()
{
	int T; scanf("%d",&T);
	while (T--)
	{
		scanf("%d%d",&n,&m);
		for (int i=1;i<=m;++i) scanf("%I64d%I64d",&a[i].a,&a[i].b);
		sort(a+1,a+m+1);
		tot=0; rt=0;
		for (int i=1;i<=m+2;++i)
			ch[i][0]=ch[i][1]=fa[i]=sz[i]=cnt[i]=w[i]=w2[i]=W[i]=0;
		insert(-INF); insert(INF);
		long long ans=0;
		for (int i=1;i<=m;++i)
		{
			pair<int, long long> que=query(a[i].b);
			int num=que.first; long long res=que.second;
			//printf("%d %d %lld\n",i,num,res);
			if (!num)
			{
				ans=max(a[i].a+a[i].b*(n-1),ans);
			}
			else if (num<=n-2)
				ans=max(res+a[i].a+(n-num-1)*a[i].b,ans);
			else ans=max(query2(n-2)+a[i].a+a[i].b,ans);
			insert(a[i].a);
		}
		printf("%I64d\n",ans);
	}

	//insert(-INF); insert(INF);
	//for (int i=1;i<=10;++i) insert(i);
	//printf("%lld\n",query2(15));
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

const int maxM=100000+5;
const int INF=0x3f3f3f3f;

int ch[maxM][2],fa[maxM],sz[maxM],cnt[maxM];
long long w2[maxM],W[maxM],w[maxM];
struct Node
{
	long long a,b;
	bool operator < (const Node &p) const
	{
		return b==p.b ? a<p.a : b<p.b;
	}
}a[maxM];
int n,m,rt,tot;

void pushup(int x)
{
	sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+cnt[x];
	W[x]=W[ch[x][0]]+W[ch[x][1]]+cnt[x]*w2[x];
}

int chk(int x) { return ch[fa[x]][1]==x; }

void rotate(int x)
{
	int y=fa[x],z=fa[y],k=chk(x);
	ch[y][k]=ch[x][k^1]; fa[ch[x][k^1]]=y;
	ch[z][chk(y)]=x; fa[x]=z;
	ch[x][k^1]=y; fa[y]=x;
	pushup(y); pushup(x);
}

void splay(int x,int goal=0)
{
	while (fa[x]!=goal)
	{
		int y=fa[x],z=fa[y];
		if (z!=goal)
		{
			if (chk(x)==chk(y)) rotate(y);
			else rotate(x);
		}
		rotate(x);
	}
	if (!goal) rt=x;
}

void insert(int val)
{
	int u=rt,p=0;
	while (u && w[u]!=val)
	{
		p=u; u=ch[u][val>w[u]];
	}
	if (w[u]==val)
	{
		++cnt[u]; splay(u);
	}
	else
	{
		u=++tot;
		if (p) { ch[p][val>w[p]]=u; fa[u]=p; }
		w[u]=val;
		if (val!=INF && val!=-INF)
		{
			sz[u]=cnt[u]=1; w2[u]=val;
		}
		splay(u);
	}
}

pair<int, long long> query(int val)
{
	int u=rt;
	while (ch[u][val>w[u]] && w[u]!=val) u=ch[u][val>w[u]];
	splay(u); //printf("%lld\n",w2[u]);
	if (w[u]>val) return make_pair(cnt[u]+sz[ch[u][1]],cnt[u]*w2[u]+W[ch[u][1]]);
	u=ch[u][1];
	while (ch[u][0]) u=ch[u][0];
	splay(u);
	return make_pair(cnt[u]+sz[ch[u][1]],cnt[u]*w2[u]+W[ch[u][1]]);
}

long long query2(int k)
{
	int u=rt; long long res=0;
	//if (sz[u]<k) return 0;
	while (u)
	{
		if (k<=sz[ch[u][1]]) u=ch[u][1];
		else if (k>cnt[u]+sz[ch[u][1]])
		{
			k-=cnt[u]+sz[ch[u][1]];
			res+=cnt[u]*w2[u]+W[ch[u][1]];
			u=ch[u][0];
		}
		else
		{
			res+=(k-sz[ch[u][1]])*w2[u]+W[ch[u][1]];
			splay(u); return res-344368632;
		}
	}
	return res-344368632;
}

int main()
{
	int T; scanf("%d",&T);
	while (T--)
	{
		scanf("%d%d",&n,&m);
		for (int i=1;i<=m;++i) scanf("%I64d%I64d",&a[i].a,&a[i].b);
		sort(a+1,a+m+1);
		tot=0; rt=0;
		for (int i=1;i<=m+2;++i)
			ch[i][0]=ch[i][1]=fa[i]=sz[i]=cnt[i]=w[i]=w2[i]=W[i]=0;
		insert(-INF); insert(INF);
		long long ans=0;
		for (int i=1;i<=m;++i)
		{
			pair<int, long long> que=query(a[i].b);
			int num=que.first; long long res=que.second;
			//printf("%d %d %lld\n",i,num,res);
			if (!num)
			{
				ans=max(a[i].a+a[i].b*(n-1),ans);
			}
			else if (num<=n-2)
				ans=max(res+a[i].a+(n-num-1)*a[i].b,ans);
			else ans=max(query2(n-2)+a[i].a+a[i].b,ans);
			insert(a[i].a);
		}
		printf("%I64d\n",ans);
	}/*

	insert(-INF); insert(INF);
	for (int i=1;i<=10;++i) insert(i);
	insert(5); insert(6);
	printf("%lld\n",query2(9));*/
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

const int maxM=100000+5;
const int INF=0x3f3f3f3f;

int ch[maxM][2],fa[maxM],sz[maxM],cnt[maxM];
long long w2[maxM],W[maxM],w[maxM];
struct Node
{
	long long a,b;
	bool operator < (const Node &p) const
	{
		return b==p.b ? a<p.a : b<p.b;
	}
}a[maxM];
int n,m,rt,tot;

void pushup(int x)
{
	sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+cnt[x];
	W[x]=W[ch[x][0]]+W[ch[x][1]]+cnt[x]*w2[x];
}

int chk(int x) { return ch[fa[x]][1]==x; }

void rotate(int x)
{
	int y=fa[x],z=fa[y],k=chk(x);
	ch[y][k]=ch[x][k^1]; fa[ch[x][k^1]]=y;
	ch[z][chk(y)]=x; fa[x]=z;
	ch[x][k^1]=y; fa[y]=x;
	pushup(y); pushup(x);
}

void splay(int x,int goal=0)
{
	while (fa[x]!=goal)
	{
		int y=fa[x],z=fa[y];
		if (z!=goal)
		{
			if (chk(x)==chk(y)) rotate(y);
			else rotate(x);
		}
		rotate(x);
	}
	if (!goal) rt=x;
}

void insert(int val)
{
	int u=rt,p=0;
	while (u && w[u]!=val)
	{
		p=u; u=ch[u][val>w[u]];
	}
	if (w[u]==val)
	{
		++cnt[u]; splay(u);
	}
	else
	{
		u=++tot;
		if (p) { ch[p][val>w[p]]=u; fa[u]=p; }
		w[u]=val;
		if (val!=INF && val!=-INF)
		{
			sz[u]=cnt[u]=1; w2[u]=val;
		}
		splay(u);
	}
}

pair<int, long long> query(int val)
{
	int u=rt;
	while (ch[u][val>w[u]] && w[u]!=val) u=ch[u][val>w[u]];
	splay(u); //printf("%lld\n",w2[u]);
	if (w[u]>val) return make_pair(cnt[u]+sz[ch[u][1]],cnt[u]*w2[u]+W[ch[u][1]]);
	u=ch[u][1];
	while (ch[u][0]) u=ch[u][0];
	splay(u);
	return make_pair(cnt[u]+sz[ch[u][1]],cnt[u]*w2[u]+W[ch[u][1]]);
}

long long query2(int k)
{
	int u=rt; long long res=0;
	while (k)
	{
		if (k<=sz[ch[u][1]]) u=ch[u][1];
		else if (k>cnt[u]+sz[ch[u][1]])
		{
			k-=cnt[u]+sz[ch[u][1]];
			res+=cnt[u]*w2[u]+W[ch[u][1]];
			u=ch[u][0];
		}
		else
		{
			res+=(k-sz[ch[u][1]])*w2[u]+W[ch[u][1]];
			splay(u); return res;
		}
	}
}

int main()
{
	int T; scanf("%d",&T);
	while (T--)
	{
		scanf("%d%d",&n,&m);
		for (int i=1;i<=m;++i) scanf("%I64d%I64d",&a[i].a,&a[i].b);
		sort(a+1,a+m+1);
		tot=0; rt=0;
		for (int i=1;i<=m+2;++i)
			ch[i][0]=ch[i][1]=fa[i]=sz[i]=cnt[i]=w[i]=w2[i]=W[i]=0;
		insert(-INF); insert(INF);
		long long ans=0;
		for (int i=1;i<=m;++i)
		{
			pair<int, long long> que=query(a[i].b);
			int num=que.first; long long res=que.second;
			//printf("%d %d %lld\n",i,num,res);
			if (!num)
			{
				ans=max(a[i].a+a[i].b*(n-1),ans);
			}
			else if (num<=n-2)
				ans=max(res+a[i].a+(n-num-1)*a[i].b,ans);
			//else ans=max(query2(n-2)+a[i].a+a[i].b,ans);
			insert(a[i].a);
		}
		printf("%I64d\n",ans);
	}
	/*
	insert(-INF); insert(INF);
	insert(5); insert(2);
	pair<int, long long> que=query(4);
	printf("%d %lld\n",que.first,que.second);
	//printf("%lld\n",query2(8));*/
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

const int maxM=100000+5;
const int INF=0x3f3f3f3f;

int ch[maxM][2],fa[maxM],sz[maxM],cnt[maxM];
long long w2[maxM],W[maxM],w[maxM];
struct Node
{
	long long a,b;
	bool operator < (const Node &p) const
	{
		return b==p.b ? a<p.a : b<p.b;
	}
}a[maxM];
int n,m,rt,tot;

void pushup(int x)
{
	sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+cnt[x];
	W[x]=W[ch[x][0]]+W[ch[x][1]]+cnt[x]*w2[x];
}

int chk(int x) { return ch[fa[x]][1]==x; }

void rotate(int x)
{
	int y=fa[x],z=fa[y],k=chk(x);
	ch[y][k]=ch[x][k^1]; fa[ch[x][k^1]]=y;
	ch[z][chk(y)]=x; fa[x]=z;
	ch[x][k^1]=y; fa[y]=x;
	pushup(y); pushup(x);
}

void splay(int x,int goal=0)
{
	while (fa[x]!=goal)
	{
		int y=fa[x],z=fa[y];
		if (z!=goal)
		{
			if (chk(x)==chk(y)) rotate(y);
			else rotate(x);
		}
		rotate(x);
	}
	if (!goal) rt=x;
}

void insert(int val)
{
	int u=rt,p=0;
	while (u && w[u]!=val)
	{
		p=u; u=ch[u][val>w[u]];
	}
	if (w[u]==val)
	{
		++cnt[u]; splay(u);
	}
	else
	{
		u=++tot;
		if (p) { ch[p][val>w[p]]=u; fa[u]=p; }
		w[u]=val; cnt[u]=1; sz[u]=1;
		if (val!=INF && val!=-INF)
		{
			 w2[u]=val;
		}
		splay(u);
	}
}

pair<int, long long> query(int val)
{
	int u=rt;
	while (ch[u][val>w[u]] && w[u]!=val) u=ch[u][val>w[u]];
	splay(u); //printf("%lld\n",w2[u]);
	if (w[u]>val) return make_pair(cnt[u]+sz[ch[u][1]]-1,cnt[u]*w2[u]+W[ch[u][1]]);
	u=ch[u][1];
	while (ch[u][0]) u=ch[u][0];
	splay(u);
	return make_pair(cnt[u]+sz[ch[u][1]]-1,cnt[u]*w2[u]+W[ch[u][1]]);
}

long long query2(int k)
{
	int u=rt; long long res=0;
	while (k)
	{
		if (k<=sz[ch[u][1]]) u=ch[u][1];
		else if (k>cnt[u]+sz[ch[u][1]])
		{
			k-=cnt[u]+sz[ch[u][1]];
			res+=cnt[u]*w2[u]+W[ch[u][1]];
			u=ch[u][0];
		}
		else
		{
			res+=(k-sz[ch[u][1]])*w2[u]+W[ch[u][1]];
			splay(u); return res;
		}
	}
}

int main()
{
	int T; scanf("%d",&T);
	while (T--)
	{
		scanf("%d%d",&n,&m);
		for (int i=1;i<=m;++i) scanf("%I64d%I64d",&a[i].a,&a[i].b);
		sort(a+1,a+m+1);
		tot=0; rt=0;
		for (int i=1;i<=m+2;++i)
			ch[i][0]=ch[i][1]=fa[i]=sz[i]=cnt[i]=w[i]=w2[i]=W[i]=0;
		insert(-INF); insert(INF);
		long long ans=0;
		for (int i=1;i<=m;++i)
		{
			pair<int, long long> que=query(a[i].b);
			int num=que.first; long long res=que.second;
			//printf("%d %d %lld\n",i,num,res);
			if (!num) ans=max(a[i].a+a[i].b*(n-1),ans);
			else if (num<=n-2)
				ans=max(res+a[i].a+(n-num-1)*a[i].b,ans);
			else if (n-2>=0)
			{
				long long tmp=query2(n-2+1)+a[i].a+a[i].b;
				ans=max(tmp,ans);// printf("%d %I64d\n",i,tmp);
			}
			insert(a[i].a);
			if (sz[rt]-2>=n) ans=max(query2(n+1),ans);
		}
		printf("%I64d\n",ans);
	}/*

	insert(-INF); insert(INF);
	for (int i=1;i<=10;++i) insert(i);
	insert(5); insert(6);
	printf("%lld\n",query2(9));*/
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

const int maxM=100000+5;
const int INF=0x3f3f3f3f;

int ch[maxM][2],fa[maxM],sz[maxM],cnt[maxM];
long long w2[maxM],W[maxM],w[maxM];
struct Node
{
	long long a,b;
	bool operator < (const Node &p) const
	{
		return b==p.b ? a<p.a : b<p.b;
	}
}a[maxM];
int n,m,rt,tot;

void pushup(int x)
{
	sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+cnt[x];
	W[x]=W[ch[x][0]]+W[ch[x][1]]+cnt[x]*w2[x];
}

int chk(int x) { return ch[fa[x]][1]==x; }

void rotate(int x)
{
	int y=fa[x],z=fa[y],k=chk(x);
	ch[y][k]=ch[x][k^1]; fa[ch[x][k^1]]=y;
	ch[z][chk(y)]=x; fa[x]=z;
	ch[x][k^1]=y; fa[y]=x;
	pushup(y); pushup(x);
}

void splay(int x,int goal=0)
{
	while (fa[x]!=goal)
	{
		int y=fa[x],z=fa[y];
		if (z!=goal)
		{
			if (chk(x)==chk(y)) rotate(y);
			else rotate(x);
		}
		rotate(x);
	}
	if (!goal) rt=x;
}

void insert(int val)
{
	int u=rt,p=0;
	while (u && w[u]!=val)
	{
		p=u; u=ch[u][val>w[u]];
	}
	if (w[u]==val)
	{
		++cnt[u]; splay(u);
	}
	else
	{
		u=++tot;
		if (p) { ch[p][val>w[p]]=u; fa[u]=p; }
		w[u]=val;
		if (val!=INF && val!=-INF)
		{
			sz[u]=cnt[u]=1; w2[u]=val;
		}
		splay(u);
	}
}

pair<int, long long> query(int val)
{
	int u=rt;
	while (ch[u][val>w[u]] && w[u]!=val) u=ch[u][val>w[u]];
	splay(u);
	if (w2[u]>val) return make_pair(cnt[u]+sz[ch[u][1]],cnt[u]*w2[u]+W[ch[u][1]]);
	u=ch[u][1];
	while (ch[u][0]) u=ch[u][0];
	splay(u);
	return make_pair(cnt[u]+sz[ch[u][1]],cnt[u]*w2[u]+W[ch[u][1]]);
}

long long query2(int k)
{
	int u=rt; long long res=0;
	while (k)
	{
		if (k<=sz[ch[u][1]]) u=ch[u][1];
		else if (k>cnt[u]+sz[ch[u][1]])
		{
			k-=cnt[u]+sz[ch[u][1]];
			res+=cnt[u]*w2[u]+W[ch[u][1]];
			u=ch[u][0];
		}
		else
		{
			res+=(k-sz[ch[u][1]])*w2[u]+W[ch[u][1]];
			splay(u); return res;
		}
	}
}

int main()
{
	int T; scanf("%d",&T);
	while (T--)
	{
		scanf("%d%d",&n,&m);
		for (int i=1;i<=m;++i) scanf("%I64d%I64d",&a[i].a,&a[i].b);
		sort(a+1,a+m+1);
		tot=0; rt=0;
		for (int i=1;i<=m+2;++i)
			ch[i][0]=ch[i][1]=fa[i]=sz[i]=cnt[i]=w[i]=w2[i]=W[i]=0;
		insert(-INF); insert(INF);
		long long ans=0;
		for (int i=1;i<=m;++i)
		{
			pair<int, long long> que=query(a[i].b);
			int num=que.first; long long res=que.second;
			//printf("%d %d %lld\n",i,num,res);
			if (!res) ans=max(a[i].a+(n-1)*a[i].b,ans);
			else if (num<=n-2)
				ans=max(res+a[i].a+(n-num-1)*a[i].b,ans);
			else ans=max(query2(n-2)+a[i].a+a[i].b,ans);
			insert(a[i].a);
		}
		printf("%I64d\n",ans);
	}
	/*
	insert(-INF); insert(INF);
	insert(5); insert(2);
	pair<int, long long> que=query(4);
	printf("%d %lld\n",que.first,que.second);
	//printf("%lld\n",query2(8));*/
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

const int maxM=100000+5;
const int INF=0x3f3f3f3f;

int ch[maxM][2],fa[maxM],sz[maxM],cnt[maxM];
long long w2[maxM],W[maxM],w[maxM];
struct Node
{
	long long a,b;
	bool operator < (const Node &p) const
	{
		return b==p.b ? a<p.a : b<p.b;
	}
}a[maxM];
int n,m,rt,tot;

void pushup(int x)
{
	sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+cnt[x];
	W[x]=W[ch[x][0]]+W[ch[x][1]]+cnt[x]*w2[x];
}

int chk(int x) { return ch[fa[x]][1]==x; }

void rotate(int x)
{
	int y=fa[x],z=fa[y],k=chk(x);
	ch[y][k]=ch[x][k^1]; fa[ch[x][k^1]]=y;
	ch[z][chk(y)]=x; fa[x]=z;
	ch[x][k^1]=y; fa[y]=x;
	pushup(y); pushup(x);
}

void splay(int x,int goal=0)
{
	while (fa[x]!=goal)
	{
		int y=fa[x],z=fa[y];
		if (z!=goal)
		{
			if (chk(x)==chk(y)) rotate(y);
			else rotate(x);
		}
		rotate(x);
	}
	if (!goal) rt=x;
}

void insert(int val)
{
	int u=rt,p=0;
	while (u && w[u]!=val)
	{
		p=u; u=ch[u][val>w[u]];
	}
	if (w[u]==val)
	{
		++cnt[u]; splay(u);
	}
	else
	{
		u=++tot;
		if (p) { ch[p][val>w[p]]=u; fa[u]=p; }
		w[u]=val; cnt[u]=1; sz[u]=1;
		if (val!=INF && val!=-INF)
		{
			 w2[u]=val;
		}
		splay(u);
	}
}

pair<int, long long> query(int val)
{
	int u=rt;
	while (ch[u][val>w[u]] && w[u]!=val) u=ch[u][val>w[u]];
	splay(u); //printf("%lld\n",w2[u]);
	if (w[u]>val) return make_pair(cnt[u]+sz[ch[u][1]]-1,cnt[u]*w2[u]+W[ch[u][1]]);
	u=ch[u][1];
	while (ch[u][0]) u=ch[u][0];
	splay(u);
	return make_pair(cnt[u]+sz[ch[u][1]]-1,cnt[u]*w2[u]+W[ch[u][1]]);
}

long long query2(int k)
{
	int u=rt; long long res=0;
	while (k)
	{
		if (k<=sz[ch[u][1]]) u=ch[u][1];
		else if (k>cnt[u]+sz[ch[u][1]])
		{
			k-=cnt[u]+sz[ch[u][1]];
			res+=cnt[u]*w2[u]+W[ch[u][1]];
			u=ch[u][0];
		}
		else
		{
			res+=(k-sz[ch[u][1]]-1)*w2[u]+W[ch[u][1]];
			splay(u); return res;
		}
	}
}

int main()
{
	int T; scanf("%d",&T);
	while (T--)
	{
		scanf("%d%d",&n,&m);
		for (int i=1;i<=m;++i) scanf("%I64d%I64d",&a[i].a,&a[i].b);
		sort(a+1,a+m+1);
		tot=0; rt=0;
		for (int i=1;i<=m+2;++i)
			ch[i][0]=ch[i][1]=fa[i]=sz[i]=cnt[i]=w[i]=w2[i]=W[i]=0;
		insert(-INF); insert(INF);
		long long ans=0;
		for (int i=1;i<=m;++i)
		{
			pair<int, long long> que=query(a[i].b);
			int num=que.first; long long res=que.second;
			//printf("%d %d %lld\n",i,num,res);
			if (!num)
			{
				ans=max(a[i].a+a[i].b*(n-1),ans);
			}
			else if (num<=n-2)
				ans=max(res+a[i].a+(n-num-1)*a[i].b,ans);
			else ans=max(query2(n-2+1)+a[i].a+a[i].b,ans);
			insert(a[i].a);
			sz[0]=0;
		}
		printf("%I64d\n",ans);
	}/*

	insert(-INF); insert(INF);
	for (int i=1;i<=10;++i) insert(i);
	insert(5); insert(6);
	printf("%lld\n",query2(9));*/
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

const int maxM=100000+5;
const int INF=0x3f3f3f3f;

int ch[maxM][2],fa[maxM],sz[maxM],cnt[maxM];
long long w2[maxM],W[maxM],w[maxM];
struct Node
{
	long long a,b;
	bool operator < (const Node &p) const
	{
		return b==p.b ? a<p.a : b<p.b;
	}
}a[maxM];
int n,m,rt,tot;

void pushup(int x)
{
	sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+cnt[x];
	W[x]=W[ch[x][0]]+W[ch[x][1]]+cnt[x]*w2[x];
}

int chk(int x) { return ch[fa[x]][1]==x; }

void rotate(int x)
{
	int y=fa[x],z=fa[y],k=chk(x);
	ch[y][k]=ch[x][k^1]; fa[ch[x][k^1]]=y;
	ch[z][chk(y)]=x; fa[x]=z;
	ch[x][k^1]=y; fa[y]=x;
	pushup(y); pushup(x);
}

void splay(int x,int goal=0)
{
	while (fa[x]!=goal)
	{
		int y=fa[x],z=fa[y];
		if (z!=goal)
		{
			if (chk(x)==chk(y)) rotate(y);
			else rotate(x);
		}
		rotate(x);
	}
	if (!goal) rt=x;
}

void insert(int val)
{
	int u=rt,p=0;
	while (u && w[u]!=val)
	{
		p=u; u=ch[u][val>w[u]];
	}
	if (w[u]==val)
	{
		++cnt[u]; splay(u);
	}
	else
	{
		u=++tot;
		if (p) { ch[p][val>w[p]]=u; fa[u]=p; }
		w[u]=val;
		if (val!=INF && val!=-INF)
		{
			sz[u]=cnt[u]=1; w2[u]=val;
		}
		splay(u);
	}
}

pair<int, long long> query(int val)
{
	int u=rt;
	while (ch[u][val>w[u]] && w[u]!=val) u=ch[u][val>w[u]];
	splay(u);
	if (w2[u]>val) return make_pair(cnt[u]+sz[ch[u][1]],cnt[u]*w2[u]+W[ch[u][1]]);
	while (ch[u][0]) u=ch[u][0];
	splay(u);
	return make_pair(cnt[u]+sz[ch[u][1]],cnt[u]*w2[u]+W[u]);
}

long long query2(int k)
{
	int u=rt; long long res=0;
	while (k)
	{
		if (k<=sz[ch[u][1]]) u=ch[u][1];
		else if (k>cnt[u]+sz[ch[u][1]])
		{
			k-=cnt[u]+sz[ch[u][1]];
			res+=cnt[u]*w2[u]+W[ch[u][1]];
			u=ch[u][0];
		}
		else
		{
			res+=(k-sz[ch[u][1]])*w2[u]+W[ch[u][1]];
			splay(u); return res;
		}
	}
}

int main()
{
	int T; scanf("%d",&T);
	while (T--)
	{
		scanf("%d%d",&n,&m);
		for (int i=1;i<=m;++i) scanf("%I64d%I64d",&a[i].a,&a[i].b);
		sort(a+1,a+m+1);
		tot=0;
		for (int i=1;i<=n+2;++i)
			ch[i][0]=ch[i][1]=fa[i]=sz[i]=cnt[i]=w[i]=w2[i]=W[i]=0;
		insert(-INF); insert(INF);
		long long ans=0;
		for (int i=1;i<=m;++i)
		{
			pair<int, long long> que=query(a[i].b);
			int num=que.first; long long res=que.second;
			if (!res) ans=max(a[i].a+(n-1)*a[i].b,ans);
			else if (num<=n-2)
				ans=max(res+a[i].a+(long long)(n-num-1)*a[i].b,ans);
			else ans=max(query2(n-2)+a[i].a+a[i].b,ans);
			insert(a[i].a);
		}
		printf("%I64d\n",ans);
	}
	/*
	insert(-INF); insert(INF);
	insert(5); insert(2);
	pair<int, long long> que=query(4);
	printf("%d %lld\n",que.first,que.second);
	//printf("%lld\n",query2(8));*/
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

const int maxM=100000+5;
const int INF=0x3f3f3f3f;

int ch[maxM][2],fa[maxM],sz[maxM],cnt[maxM];
long long w2[maxM],W[maxM],w[maxM];
struct Node
{
	long long a,b;
	bool operator < (const Node &p) const
	{
		return b==p.b ? a<p.a : b<p.b;
	}
}a[maxM];
int n,m,rt,tot;

void pushup(int x)
{
	sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+cnt[x];
	W[x]=W[ch[x][0]]+W[ch[x][1]]+cnt[x]*w2[x];
}

int chk(int x) { return ch[fa[x]][1]==x; }

void rotate(int x)
{
	int y=fa[x],z=fa[y],k=chk(x);
	ch[y][k]=ch[x][k^1]; fa[ch[x][k^1]]=y;
	ch[z][chk(y)]=x; fa[x]=z;
	ch[x][k^1]=y; fa[y]=x;
	pushup(y); pushup(x);
}

void splay(int x,int goal=0)
{
	while (fa[x]!=goal)
	{
		int y=fa[x],z=fa[y];
		if (z!=goal)
		{
			if (chk(x)==chk(y)) rotate(y);
			else rotate(x);
		}
		rotate(x);
	}
	if (!goal) rt=x;
}

void insert(int val)
{
	int u=rt,p=0;
	while (u && w[u]!=val)
	{
		p=u; u=ch[u][val>w[u]];
	}
	if (w[u]==val)
	{
		++cnt[u]; splay(u);
	}
	else
	{
		u=++tot;
		if (p) { ch[p][val>w[p]]=u; fa[u]=p; }
		w[u]=val;
		if (val!=INF && val!=-INF)
		{
			sz[u]=cnt[u]=1; w2[u]=val;
		}
		splay(u);
	}
}

pair<int, long long> query(int val)
{
	int u=rt;
	while (ch[u][val>w[u]] && w[u]!=val) u=ch[u][val>w[u]];
	splay(u); //printf("%lld\n",w2[u]);
	if (w[u]>val) return make_pair(cnt[u]+sz[ch[u][1]],cnt[u]*w2[u]+W[ch[u][1]]);
	u=ch[u][1];
	while (ch[u][0]) u=ch[u][0];
	splay(u);
	return make_pair(cnt[u]+sz[ch[u][1]],cnt[u]*w2[u]+W[ch[u][1]]);
}

long long query2(int k)
{
	int u=rt; long long res=0;
	while (k)
	{
		if (k<=sz[ch[u][1]]) u=ch[u][1];
		else if (cnt[u]!=0 && k>cnt[u]+sz[ch[u][1]])
		{
			k-=cnt[u]+sz[ch[u][1]];
			res+=cnt[u]*w2[u]+W[ch[u][1]];
			u=ch[u][0];
		}
		else
		{
			res+=(k-sz[ch[u][1]])*w2[u]+W[ch[u][1]];
			splay(u); return res;
		}
	}
}

int main()
{
	int T; scanf("%d",&T);
	while (T--)
	{
		scanf("%d%d",&n,&m);
		for (int i=1;i<=m;++i) scanf("%I64d%I64d",&a[i].a,&a[i].b);
		sort(a+1,a+m+1);
		tot=0; rt=0;
		for (int i=1;i<=m+2;++i)
			ch[i][0]=ch[i][1]=fa[i]=sz[i]=cnt[i]=w[i]=w2[i]=W[i]=0;
		insert(-INF); insert(INF);
		long long ans=0;
		for (int i=1;i<=m;++i)
		{
			pair<int, long long> que=query(a[i].b);
			int num=que.first; long long res=que.second;
			//printf("%d %d %lld\n",i,num,res);
			if (!num)
			{
				ans=max(a[i].a+a[i].b*(n-1),ans);
			}
			else if (num<=n-2)
				ans=max(res+a[i].a+(n-num-1)*a[i].b,ans);
			else ans=max(query2(n-2)+a[i].a+a[i].b,ans);
			insert(a[i].a);
		}
		printf("%I64d\n",ans);
	}/*

	insert(-INF); insert(INF);
	for (int i=1;i<=10;++i) insert(i);
	insert(5); insert(6);
	printf("%lld\n",query2(9));*/
	return 0;
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
int t,n,m;
struct node{
	int v;
	int f;
	int mt;
}a[200005];
bool cmp(node x,node y)
{
	if(x.v>y.v)
		return true;
	else if(x.v==y.v&&x.f==0)
		return true;
	return false;
}
bool check(int x)
{
	int sum=0,cnt=0;
	for(int i=1;i<=2*m;i++)
	{

		if(a[i].f==0)
		{
			sum+=a[i].v;
			cnt++;
		}
		else
			if(a[i].mt==-1&&(sum+a[i].v*(n-cnt))>=x)
				sum=x;
			else if(a[i].mt!=-1&&(sum+a[i].v*(n-cnt-1)+a[i].mt)>=x)
				sum=x;
		if(sum>=x)
			return true;
		if(cnt>n)
			return false;
	}
	return false;
}
signed main()
{
	cin>>t;
	while(t--)
	{
		cin>>n>>m;
		for(int i=1;i<=m;i++)
		{
			scanf("%d%d",&a[i*2-1].v,&a[i*2].v);
			a[i*2].f=1;
			a[i*2-1].f=0;
			if(a[i*2-1].v>=a[i*2].v)
				a[i*2].mt=-1;
			else
				a[i*2].mt=a[i*2-1].v;
		}
		sort(a+1,a+2*m+1,cmp);
		int l=1,r=1e13,ans=0;
		while(l<=r)
		{
//			cout<<((l+r)>>1)<<endl;
			int mid=(l+r)>>1;
			if(check(mid))
			{
				ans=mid;
				l=mid+1;
			}
			else
				r=mid-1;
		}
		cout<<ans<<endl;
	}
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
int t,n,m;
struct node{
	int v;
	int f;
	int mt;
}a[200005];
bool cmp(node x,node y)
{
	if(x.v>y.v)
		return true;
	else if(x.v==y.v&&x.f==0)
		return true;
	return false;
}
bool check(int x)
{
	int sum=0,cnt=0;
	for(int i=1;i<=2*m;i++)
	{

		if(a[i].f==0)
		{
			sum+=a[i].v;
			cnt++;
		}
		else
			if(a[i].mt==-1&&(sum+a[i].v*(n-cnt))>=x)
				sum=x;
			else if(a[i].mt!=-1&&(sum+a[i].v*(n-cnt-1)+a[i].mt)>=x)
				sum=x;
		if(sum>=x)
			return true;
		if(cnt>n)
			return false;
	}
	return false;
}
signed main()
{
	cin>>t;
	while(t--)
	{
		cin>>n>>m;
		for(int i=1;i<=m;i++)
		{
			cin>>a[i*2-1].v>>a[i*2].v;
			a[i*2].f=1;
			a[i*2-1].f=0;
			if(a[i*2-1].v>=a[i*2].v)
				a[i*2].mt=-1;
			else
				a[i*2].mt=a[i*2-1].v;
		}
		sort(a+1,a+2*m+1,cmp);
		int l=1,r=1e18,ans=0;
		while(l<=r)
		{
//			cout<<((l+r)>>1)<<endl;
			int mid=(l+r)>>1;
			if(check(mid))
			{
				ans=mid;
				l=mid+1;
			}
			else
				r=mid-1;
		}
		cout<<ans<<endl;
	}
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
int t,n,m;
struct node{
	int v;
	int f;
	int mt;
}a[200005];
bool cmp(node x,node y)
{
	if(x.v>y.v)
		return true;
	else if(x.v==y.v&&x.f==0)
		return true;
	return false;
}
bool check(int x)
{
	int sum=0,cnt=0;
	for(int i=1;i<=2*m;i++)
	{

		if(a[i].f==0)
		{
			sum+=a[i].v;
			cnt++;
		}
		else
			if(a[i].mt==-1&&(sum+a[i].v*(n-cnt))>=x)
				sum=x;
			else if(a[i].mt!=-1&&(sum+a[i].v*(n-cnt-1)+a[i].mt)>=x)
				sum=x;
		if(sum>=x)
			return true;
		if(cnt>n)
			return false;
	}
	return false;
}
signed main()
{
	cin>>t;
	while(t--)
	{
		cin>>n>>m;
		for(int i=1;i<=m;i++)
		{
			scanf("%d%d",&a[i*2-1].v,&a[i*2].v);
			a[i*2].f=1;
			a[i*2-1].f=0;
			if(a[i*2-1].v>=a[i*2].v)
				a[i*2].mt=-1;
			else
				a[i*2].mt=a[i*2-1].v;
		}
		sort(a+1,a+2*m+1,cmp);
		int l=1,r=1e18,ans=0;
		while(l<=r)
		{
//			cout<<((l+r)>>1)<<endl;
			int mid=(l+r)>>1;
			if(check(mid))
			{
				ans=mid;
				l=mid+1;
			}
			else
				r=mid-1;
		}
		cout<<ans<<endl;
	}
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
int t,n,m;
struct node{
	int v;
	int f;
	int mt;
}a[200005];
bool cmp(node x,node y)
{
	if(x.v>y.v)
		return true;
	else if(x.v==y.v&&x.f==0)
		return true;
	return false;
}
signed main()
{
	cin>>t;
	while(t--)
	{
		cin>>n>>m;
		for(int i=1;i<=m;i++)
		{
			scanf("%d%d",&a[i*2-1].v,&a[i*2].v);
			a[i*2].f=1;
			a[i*2-1].f=0;
			if(a[i*2-1].v>=a[i*2].v)
				a[i*2].mt=-1;
			else
				a[i*2].mt=a[i*2-1].v;
		}
		sort(a+1,a+2*m+1,cmp);
		int ans=0;
		int sum=0,cnt=0;
		for(int i=1;i<=2*m;i++)
		{

			if(a[i].f==0)
			{
				sum+=a[i].v;
				cnt++;
			}
			else
				if(a[i].mt==-1)
					ans=max(ans,sum+a[i].v*(n-cnt));
				else if(a[i].mt!=-1)
					ans=max(ans,sum+a[i].v*(n-cnt-1)+a[i].mt);
		}
		cout<<ans<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
int t,n,m;
struct node{
	int v;
	int f;
	int mt;
}a[200005];
bool cmp(node x,node y)
{
	if(x.v>y.v)
		return true;
	else if(x.v==y.v&&x.f==0)
		return true;
	return false;
}
bool check(int x)
{
	int sum=0,cnt=0;
	for(int i=1;i<=2*m;i++)
	{

		if(a[i].f==0)
		{
			sum+=a[i].v;
			cnt++;
		}
		else
			if(a[i].mt==-1&&(sum+a[i].v*(n-cnt))>=x)
				sum=x;
			else if(a[i].mt!=-1&&(sum+a[i].v*(n-cnt-1)+a[i].mt)>=x)
				sum=x;
		if(sum>=x)
			return true;
		if(cnt>n)
			return false;
	}
	return false;
}
int main()
{
	cin>>t;
	while(t--)
	{
		cin>>n>>m;
		for(int i=1;i<=m;i++)
		{
			cin>>a[i*2-1].v>>a[i*2].v;
			a[i*2].f=1;
			a[i*2-1].f=0;
			if(a[i*2-1].v>=a[i*2].v)
				a[i*2].mt=-1;
			else
				a[i*2].mt=a[i*2-1].v;
		}
		sort(a+1,a+2*m+1,cmp);
		int l=1,r=1e9,ans=0;
		while(l<=r)
		{
//			cout<<((l+r)>>1)<<endl;
			int mid=(l+r)>>1;
			if(check(mid))
			{
				ans=mid;
				l=mid+1;
			}
			else
				r=mid-1;
		}
		cout<<ans<<endl;
	}
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
int t,n,m;
struct node{
	int v;
	int f;
	int mt;
}a[200005];
bool cmp(node x,node y)
{
	if(x.v>y.v)
		return true;
	else if(x.v==y.v&&x.f==0)
		return true;
	return false;
}
bool check(int x)
{
	int sum=0,cnt=0;
	for(int i=1;i<=2*m;i++)
	{

		if(a[i].f==0)
		{
			sum+=a[i].v;
			cnt++;
		}
		else
			if(a[i].mt==-1&&(sum+a[i].v*(n-cnt))>=x)
				sum=x;
			else if(a[i].mt!=-1&&(sum+a[i].v*(n-cnt-1)+a[i].mt)>=x)
				sum=x;
		if(sum>=x)
			return true;
		if(cnt>n)
			return false;
	}
	return false;
}
signed main()
{
	cin>>t;
	while(t--)
	{
		cin>>n>>m;
		for(int i=1;i<=m;i++)
		{
			scanf("%d %d",&a[i+2-1].v,&a[i+2].v);
			a[i*2].f=1;
			a[i*2-1].f=0;
			if(a[i*2-1].v>=a[i*2].v)
				a[i*2].mt=-1;
			else
				a[i*2].mt=a[i*2-1].v;
		}
		sort(a+1,a+2*m+1,cmp);
		int l=1,r=1e18,ans=0;
		while(l<=r)
		{
//			cout<<((l+r)>>1)<<endl;
			int mid=(l+r)>>1;
			if(check(mid))
			{
				ans=mid;
				l=mid+1;
			}
			else
				r=mid-1;
		}
		cout<<ans<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;cin>>t;
    int64_t ar[(int)1e5+10];
    while(t--)
    {
        int64_t n,m;cin>>n>>m;
        int64_t highhest_b=-1;
        int64_t val=-1;
        int64_t ans=0;
        for(int i=0;i<m;i++)
        {
            int64_t x;
            cin>>ar[i]>>x;
            if(x>highhest_b)
            {
                highhest_b=x;
                val=ar[i];
            }
            else if(highhest_b==x)
            {
                if(ar[i]>val)
                {
                    val=ar[i];
                }
            }
        }
        sort(ar,ar+m,greater<int64_t>() );
        int64_t alt=0;
        int64_t pre=0;
		int64_t okok=0;
        bool flag=false;
        for(int i=0;i<m;i++)
        {
            if(ar[i]>=highhest_b)
            {
                ans+=ar[i];
            }
			else if(val>=highhest_b)
			{
				ans+=(highhest_b*n);
                n=0;
				break;
			}
            else
            {
				flag=true;
				if(n>1)
				{
					pre = alt + val + (n-1)*highhest_b;
				}
				alt+=ar[i];
				okok=max(okok,max(pre,alt));
				if(n>2)
				{
					okok=max(okok,alt+val+(n-2)*highhest_b);
				}
            }
            n--;
            if(n==0)
            {
                break;
            }
        }
        if(n>0 and !flag)
        {
            ans+=highhest_b*n;
        }
		if(flag)
		{
			ans+=okok;
		}
        cout<<ans<<"\n";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int int64_t
int32_t main()
{
    int t;cin>>t;
    while(t--)
    {
        int n,m;cin>>n>>m;
        vector<pair<int,int> > vp(m);
        for(int i=0;i<m;i++)
        {
            cin>>vp[i].second>>vp[i].first;
        }
        sort(vp.begin(),vp.end());
        // cout<<"----\n";
        // for(auto i:vp)
        // {
        //     cout<<i.first<<" "<<i.second<<"\n";
        // }
        // cout<<"----?\n";
        multiset<int> s;
        int sum=0;
        int cur=0;
        int ans=0;
        for(int i = 0; i < m; i++)
		{
			while(s.size() && (*s.begin() < vp[i].first || s.size() >= n))
			{
				sum -= *s.begin();
				s.erase(s.begin());
			}
			cur = sum + vp[i].second;
			int rem = n - s.size() - 1;
			//cur += rem * vp[i].first;
			ans = max(ans, cur + rem * vp[i].first);
			s.insert(vp[i].second);
			sum += vp[i].second;
		}
        cout<<ans<<"\n";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;cin>>t;
    int64_t ar[(int)1e5+10];
    while(t--)
    {
        int64_t n,m;cin>>n>>m;
        int64_t highhest_b=-1;
        int64_t val=-1;
        int64_t ans=0;
        for(int i=0;i<m;i++)
        {
            int64_t x;
            cin>>ar[i]>>x;
            if(x>highhest_b)
            {
                highhest_b=x;
                val=ar[i];
            }
            else if(highhest_b==x)
            {
                if(ar[i]>val)
                {
                    val=ar[i];
                }
            }
        }
        sort(ar,ar+m,greater<int64_t>() );
        int64_t alt=0;
        int64_t pre=0;
        bool flag=true;
        for(int i=0;i<m;i++)
        {
            if(ar[i]>=highhest_b)
            {
                ans+=ar[i];
            }
            else
            {
                if(val>=highhest_b)
                {
                    ans+=n*highhest_b;
                    n=1;
                }
                else if(ar[i]>val)
                {
                    flag=false;
                    alt=max(alt,max(pre+val+(n-1)*highhest_b,pre+ar[i]+(n-2)*highhest_b));
                    pre+=ar[i];
                }
            }
            n--;
            if(n==0)
            {
                break;
            }
        }
        if(flag)
        {
            ans+=n*highhest_b;
        }
        else
        {
            ans+=alt;
        }
        cout<<ans<<"\n";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int int64_t
int32_t main()
{
    int t;cin>>t;
    while(t--)
    {
        int n,m;cin>>n>>m;
        vector<pair<int,int> > vp(m);
        for(int i=0;i<m;i++)
        {
            cin>>vp[i].second>>vp[i].first;
        }
        sort(vp.begin(),vp.end());
        // cout<<"----\n";
        // for(auto i:vp)
        // {
        //     cout<<i.first<<" "<<i.second<<"\n";
        // }
        // cout<<"----?\n";
        multiset<int> s;
        int sum=0;
        int ans=0;
        int cur;
        for(int i = 0; i < m; i++)
		{
			while(s.size() && (*s.begin() < vp[i].first || s.size() >= n))
			{
				sum -= *s.begin();
				s.erase(s.begin());
			}
			cur = sum + vp[i].second;
			int rem = n - s.size() - 1;
			//cur += rem * vp[i].first;
			ans = max(ans, cur + rem * vp[i].first);
			s.insert(vp[i].second);
			sum += vp[i].second;
		}
        cout<<ans<<"\n";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;cin>>t;
    while(t--)
    {
        int64_t n,m;cin>>n>>m;
        vector<pair<int64_t,int64_t> > ar(m);
        int64_t highhest_b=-1;
        int64_t val=-1;
        int64_t ans=0;
        for(int i=0;i<m;i++)
        {
            int64_t x;
            cin>>ar[i].first>>x;
            ar[i].second=x;
            if(x>highhest_b)
            {
                highhest_b=x;
                val=ar[i].first;
            }
            else if(highhest_b==x)
            {
                if(ar[i].first>val)
                {
                    val=ar[i].first;
                }
            }
        }
        sort(ar.begin(),ar.end(),greater<pair<int64_t,int64_t> >() );
        int64_t alt=0;
        int64_t pre=0;
		int64_t okok=0;
        int64_t b_max=0;
        bool flag=false;
        for(int i=0;i<m;i++)
        {
            if(ar[i].first>=highhest_b)
            {
                ans+=ar[i].first;
                b_max=max(b_max,ar[i].second);
            }
			else if(val>=highhest_b)
			{
				ans+=(highhest_b*n);
                n=0;
				break;
			}
            else
            {
				flag=true;
				if(n>1)
				{
					pre = alt + val + (n-1)*highhest_b;
				}
				alt+=max(ar[i].first,b_max);
                b_max=max(b_max,ar[i].second);
				okok=max(okok,max(pre,alt));
				if(n>2)
				{
					okok=max(okok,alt+val+(n-2)*highhest_b);
				}
            }
            n--;
            if(n==0)
            {
                break;
            }
        }
        if(n>0 and !flag)
        {
            ans+=highhest_b*n;
        }
		if(flag)
		{
			ans+=okok;
		}
        cout<<ans<<"\n";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;cin>>t;
    int64_t ar[(int)1e5+10];
    while(t--)
    {
        int64_t n,m;cin>>n>>m;
        int64_t highhest_b=-1;
        int64_t val=-1;
        int64_t ans=0;
        for(int i=0;i<m;i++)
        {
            int64_t x;
            cin>>ar[i]>>x;
            if(x>highhest_b)
            {
                highhest_b=x;
                val=ar[i];
            }
            else if(highhest_b==x)
            {
                if(ar[i]>val)
                {
                    val=ar[i];
                }
            }
        }
        sort(ar,ar+m,greater<int64_t>() );
        int64_t alt=0;
        int64_t pre=0;
        bool flag=true;
        for(int i=0;i<m;i++)
        {
            if(ar[i]>=highhest_b)
            {
                ans+=ar[i];
            }
            else
            {
                if(val>=highhest_b)
                {
                    ans+=n*highhest_b;
                    n=1;
                }
                else
                {
                    flag=false;
                    alt=max(pre+val+(n-1)*highhest_b,pre+ar[i]+(n-2)*highhest_b);
                    pre+=ar[i];
                }
            }
            n--;
            if(n==0)
            {
                break;
            }
        }
        if(flag)
        {
            ans+=n*highhest_b;
        }
        else
        {
            ans+=alt;
        }
        cout<<ans<<"\n";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int int64_t
int32_t main()
{
    int t;cin>>t;
    while(t--)
    {
        int n,m;cin>>n>>m;
        vector<pair<int,int> > vp(m);
        for(int i=0;i<m;i++)
        {
            cin>>vp[i].second>>vp[i].first;
        }
        sort(vp.begin(),vp.end());
        multiset<int> s;
        int sum=0;
        int cur=0;
        int ans=0;
        for(int i = 0; i < m; i++)
		{
			while(s.size() && (*s.begin() < vp[i].first || s.size() >= n))
			{
				sum -= *s.begin();
				s.erase(s.begin());
			}
			cur = sum + vp[i].second;
			int rem = n - s.size() - 1;
			ans = max(ans, cur + rem * vp[i].first);
			s.insert(vp[i].second);
			sum += vp[i].second;
		}
        cout<<ans<<"\n";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;cin>>t;
    int64_t ar[(int)1e5+10];
    while(t--)
    {
        int64_t n,m;cin>>n>>m;
        int64_t highhest_b=-1;
        int64_t val=-1;
        int64_t ans=0;
        for(int i=0;i<m;i++)
        {
            int64_t x;
            cin>>ar[i]>>x;
            if(x>highhest_b)
            {
                highhest_b=x;
                val=ar[i];
            }
            else if(highhest_b==x)
            {
                if(ar[i]>val)
                {
                    val=ar[i];
                }
            }
        }
        sort(ar,ar+m,greater<int64_t>() );
        int64_t alt=0;
        int64_t pre=0;
        bool flag=true;
        for(int i=0;i<m;i++)
        {
            if(ar[i]>=highhest_b)
            {
                ans+=ar[i];
            }
            else
            {
                if(val>=highhest_b)
                {
                    ans+=n*highhest_b;
                    n=1;
                }
                else
                {
                    flag=false;
                    alt=max(alt,max(pre+val+(n-1)*highhest_b,pre+ar[i]+(n-2)*highhest_b));
                    //cout<<alt<<" "<<highhest_b<<" "<<pre<<"\n";
                    pre+=ar[i];
                }
            }
            n--;
            if(n==0)
            {
                break;
            }
        }
        if(flag)
        {
            ans+=n*highhest_b;
        }
        else
        {
            ans+=alt;
        }
        cout<<ans<<"\n";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int int64_t
int32_t main()
{
    int t;cin>>t;
    while(t--)
    {
        int n,m;cin>>n>>m;
        vector<pair<int,int> > vp(m);
        for(int i=0;i<m;i++)
        {
            cin>>vp[i].second>>vp[i].first;
        }
        sort(vp.begin(),vp.end());
        // cout<<"----\n";
        // for(auto i:vp)
        // {
        //     cout<<i.first<<" "<<i.second<<"\n";
        // }
        // cout<<"----?\n";
        multiset<int> s;
        int sum=0;
        int ans=0;
        for(int i = 0; i < m; i++)
		{
			while(s.size() && (*s.begin() < vp[i].first || s.size() >= n))
			{
				sum -= *s.begin();
				s.erase(s.begin());
			}
			int cur = sum + vp[i].second;
			int rem = n - s.size() - 1;
			cur += rem * vp[i].first;
			ans = max(ans, cur);
			s.insert(vp[i].second);
			sum += vp[i].second;
		}
        cout<<ans<<"\n";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int int64_t
int32_t main()
{
    int t;cin>>t;
    while(t--)
    {
        int n,m;cin>>n>>m;
        vector<pair<int,int> > vp(m);
        for(int i=0;i<m;i++)
        {
            cin>>vp[i].second>>vp[i].first;
        }
        sort(vp.begin(),vp.end());
        multiset<int> s;
        int sum=0;
        int cur=0;
        int ans=0;
        for(int i=0;i<m;i++)
        {
            if(s.size() && (*s.begin()<vp[i].first || s.size()>=n))
            {
                sum-=*s.begin();
                s.erase(s.begin());
            }
            cur = sum + vp[i].second;
            int rem = n - s.size() - 1;
            ans=max(ans,cur + rem*vp[i].first);
            sum+=vp[i].second;
            s.insert(vp[i].second);
        }
        cout<<ans<<"\n";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;cin>>t;
    while(t--)
    {
        int64_t n,m;cin>>n>>m;
        vector<pair<int64_t,int64_t> > ar(m);
        int64_t highhest_b=-1;
        int64_t val=-1;
        int64_t ans=0;
        for(int i=0;i<m;i++)
        {
            int64_t x;
            cin>>ar[i].first>>x;
            ar[i].second=x;
            if(x>highhest_b)
            {
                highhest_b=x;
                val=ar[i].first;
            }
            else if(highhest_b==x)
            {
                if(ar[i].first>val)
                {
                    val=ar[i].first;
                }
            }
        }
        sort(ar.begin(),ar.end(),greater<pair<int64_t,int64_t> >() );
        int64_t alt=0;
        int64_t pre=0;
		int64_t okok=0;
        int64_t b_max=0;
        bool flag=false;
        for(int i=0;i<m;i++)
        {
            if(ar[i].first>=highhest_b)
            {
                ans+=ar[i].first;
                b_max=max(b_max,ar[i].second);
            }
			else if(val>=highhest_b)
			{
				ans+=(highhest_b*n);
                n=0;
				break;
			}
            else
            {
				flag=true;
				if(n>1)
				{
					pre = alt + val + (n-1)*highhest_b;
				}
				alt+=max(ar[i].first,b_max);
                if(ar[i].first>b_max)
                {
                    b_max=max(b_max,ar[i].second);
                }
				okok=max(okok,max(pre,alt));
				if(n>2)
				{
					okok=max(okok,alt+val+(n-2)*highhest_b);
				}
            }
            n--;
            if(n==0)
            {
                break;
            }
        }
        if(n>0 and !flag)
        {
            ans+=highhest_b*n;
        }
		if(flag)
		{
			ans+=okok;
		}
        cout<<ans<<"\n";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;cin>>t;
    int64_t ar[(int)1e5+10];
    while(t--)
    {
        int64_t n,m;cin>>n>>m;
        int64_t highhest_b=-1;
        int64_t val=-1;
        int64_t ans=0;
        for(int i=0;i<m;i++)
        {
            int64_t x;
            cin>>ar[i]>>x;
            if(x>highhest_b)
            {
                highhest_b=x;
                val=ar[i];
            }
            else if(highhest_b==x)
            {
                if(ar[i]>val)
                {
                    val=ar[i];
                }
            }
        }
        sort(ar,ar+m,greater<int64_t>() );
        int64_t alt=0;
        int64_t pre=0;
		int64_t okok=0;
        bool flag=false;
        for(int i=0;i<m;i++)
        {
            if(ar[i]>=highhest_b)
            {
                ans+=ar[i];
            }
			else if(val>=highhest_b)
			{
				ans+=(highhest_b*n);
				break;
			}
            else
            {
				flag=true;
				if(n>1)
				{
					pre = alt + val + (n-1)*highhest_b;
				}
				alt+=ar[i];
				okok=max(okok,max(pre,alt));
				if(n>2)
				{
					okok=max(okok,alt+val+(n-2)*highhest_b);
				}
            }
            n--;
            if(n==0)
            {
                break;
            }
        }
        if(n>0 and !flag)
        {
            ans+=highhest_b*n;
        }
		if(flag)
		{
			ans+=okok;
		}
        cout<<ans<<"\n";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int int64_t
int32_t main()
{
    int t;cin>>t;
    while(t--)
    {
        int n,m;cin>>n>>m;
        vector<pair<int,int> > vp(m);
        for(int i=0;i<m;i++)
        {
            cin>>vp[i].second>>vp[i].first;
        }
        sort(vp.begin(),vp.end());
        // cout<<"----\n";
        // for(auto i:vp)
        // {
        //     cout<<i.first<<" "<<i.second<<"\n";
        // }
        // cout<<"----?\n";
        multiset<int> s;
        int sum=0;
        int ans=0;
        for(int i = 0; i < m; i++)
		{
			while(s.size() && (*s.begin() < vp[i].first || s.size() >= n))
			{
				sum -= *s.begin();
				s.erase(s.begin());
			}
			int cur = sum + vp[i].second;
			int rem = n - s.size() - 1;
			//cur += rem * vp[i].first;
			ans = max(ans, cur + rem * vp[i].first);
			s.insert(vp[i].second);
			sum += vp[i].second;
		}
        cout<<ans<<"\n";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;cin>>t;
    int64_t ar[(int)1e5+10];
    while(t--)
    {
        int64_t n,m;cin>>n>>m;
        int64_t highhest_b=-1;
        int64_t val=-1;
        int64_t ans=0;
        for(int i=0;i<m;i++)
        {
            int64_t x;
            cin>>ar[i]>>x;
            if(x>highhest_b)
            {
                highhest_b=x;
                val=ar[i];
            }
            else if(highhest_b==x)
            {
                if(ar[i]>val)
                {
                    val=ar[i];
                }
            }
        }
        sort(ar,ar+m,greater<int64_t>() );
        int64_t alt=0;
        int64_t pre=0;
        bool flag=true;
        for(int i=0;i<m;i++)
        {
            if(ar[i]>=highhest_b)
            {
                ans+=ar[i];
            }
            else
            {
                if(val>=highhest_b)
                {
                    ans+=n*highhest_b;
                    n=1;
                }
                else if(ar[i]>val)
                {
                    flag=false;
                    alt=max(pre+val+(n-1)*highhest_b,pre+ar[i]+(n-2)*highhest_b);
                    pre+=ar[i];
                }
            }
            n--;
            if(n==0)
            {
                break;
            }
        }
        if(flag)
        {
            ans+=n*highhest_b;
        }
        else
        {
            ans+=alt;
        }
        cout<<ans<<"\n";
    }
    return 0;
}


/*
	  Author : @evenbao
	  Created : 2020 / 07 / 19
*/

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;

#define pii pair<int , int>
#define mp make_pair
#define fi first
#define se second

const int N = 2e5 + 10;

struct node {
		int a , b;
} a[N];

int n , m , sz , len;
int cnt[N * 30] , lc[N * 30] , rc[N * 30] , c[N] , rt[N];
LL sum[N * 30];

template <typename T> inline void chkmax(T &x , T y) { x = max(x , y); }
template <typename T> inline void chkmin(T &x , T y) { x = min(x , y); }
template <typename T> inline void read(T &x) {
    T f = 1; x = 0;
    char c = getchar();
    for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
    for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
    x *= f;
}

inline bool cmp(node a , node b) {
		if (a.b != b.b) return a.b > b.b;
		else return a.a > b.a;
}

inline void modify(int &now , int old , int l , int r , int x , LL val) {
		now = ++sz;
		lc[now] = lc[old] , rc[now] = rc[old];
		sum[now] = sum[old] + val; cnt[now] = cnt[old] + 1;
		if (l == r) return;
		int mid = (l + r) >> 1;
		if (mid >= x) modify(lc[now] , lc[old] , l , mid , x , val);
		else modify(rc[now] , rc[old] , mid + 1 , r , x , val);
}
inline int query_rank(int now , int l , int r , int x) {
		if (l == r)
				return l;
		int mid = (l + r) >> 1;
		if (cnt[rc[now]] >= x) return query_rank(rc[now] , mid + 1 , r , x);
		else return query_rank(lc[now] , l , mid , x - cnt[rc[now]]);
}
inline int query_cnt(int now , int l , int r , int ql , int qr) {
		if (l == ql && r == qr)
				return cnt[now];
		int mid = (l + r) >> 1;
		if (mid >= qr) return query_cnt(lc[now] , l , mid , ql , qr);
		else if (mid + 1 <= ql) return query_cnt(rc[now] , mid + 1 , r , ql , qr);
		else return query_cnt(lc[now] , l , mid , ql , mid) + query_cnt(rc[now] , mid + 1 , r , mid + 1 , qr);
}
inline int query_sum(int now , int l , int r , int cur) {
		if (l == r)
				return (LL) cur * c[l];
		int mid = (l + r) >> 1;
		if (cnt[rc[now]] >= cur) return query_sum(rc[now] , mid + 1 , r , cur);
		else return query_sum(lc[now] , l , mid , cur - cnt[rc[now]]);
}

int main() {

		int T; read(T);
		while (T--) {
				read(n); read(m); len = 0;
				for (int i = 1; i <= m; ++i) {
						read(a[i].a);
						read(a[i].b);
						c[++len] = a[i].a;
				}
				sort(a + 1 , a + 1 + m , cmp);
				sort(c + 1 , c + 1 + len);
				len = unique(c + 1 , c + 1 + len) - c - 1;
				sz = 0; rt[n + 1] = 0;
				for (int i = n; i >= 1; --i) {
						int x = lower_bound(c + 1 , c + 1 + len , a[i].a) - c;
						modify(rt[i] , rt[i + 1] , 1 , len , x , a[i].a);
				}
				LL answer = 0;
				for (int i = 1; i <= m; ++i) {
						LL cur = a[i].a; int need = n - 1;
						int x = query_rank(rt[i + 1] , 1 , len , need);
						if (c[x] < a[i].b) {
								int up = lower_bound(c + 1 , c + 1 + len , a[i].b) - c;
								int rk = query_cnt(rt[i + 1] , 1 , len , up , len);
								cur += query_sum(rt[i + 1] , 1 , len , rk);
								need -= rk;
						} else {
								cur += query_sum(rt[i + 1] , 1 , len , need);
								need = 0;
						}
						if (need > 0) cur += (LL) need * a[i].b;
						chkmax(answer , cur);
				}
				printf("%lld\n" , answer);
		}
	  return 0;
}


/*
	  Author : @evenbao
	  Created : 2020 / 07 / 19
*/

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;

#define pii pair<int , int>
#define mp make_pair
#define fi first
#define se second

const int N = 2e5 + 10;

struct node {
		int a , b;
} a[N];

int n , m , sz , len;
int cnt[N * 30] , lc[N * 30] , rc[N * 30] , c[N] , rt[N];
LL sum[N * 30];

template <typename T> inline void chkmax(T &x , T y) { x = max(x , y); }
template <typename T> inline void chkmin(T &x , T y) { x = min(x , y); }
template <typename T> inline void read(T &x) {
    T f = 1; x = 0;
    char c = getchar();
    for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
    for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
    x *= f;
}

inline bool cmp(node a , node b) {
		if (a.b != b.b) return a.b > b.b;
		else return a.a > b.a;
}

inline void modify(int &now , int old , int l , int r , int x , LL val) {
		now = ++sz;
		lc[now] = lc[old] , rc[now] = rc[old];
		sum[now] = sum[old] + val; cnt[now] = cnt[old] + 1;
		if (l == r) return;
		int mid = (l + r) >> 1;
		if (mid >= x) modify(lc[now] , lc[old] , l , mid , x , val);
		else modify(rc[now] , rc[old] , mid + 1 , r , x , val);
}
inline int query_rank(int now , int l , int r , int x) {
		if (l == r)
				return l;
		int mid = (l + r) >> 1;
		if (cnt[rc[now]] >= x) return query_rank(rc[now] , mid + 1 , r , x);
		else return query_rank(lc[now] , l , mid , x - cnt[rc[now]]);
}
inline int query_cnt(int now , int l , int r , int ql , int qr) {
		if (l == ql && r == qr)
				return cnt[now];
		int mid = (l + r) >> 1;
		if (mid >= qr) return query_cnt(lc[now] , l , mid , ql , qr);
		else if (mid + 1 <= ql) return query_cnt(rc[now] , mid + 1 , r , ql , qr);
		else return query_cnt(lc[now] , l , mid , ql , mid) + query_cnt(rc[now] , mid + 1 , r , mid + 1 , qr);
}
inline LL query_sum(int now , int l , int r , int cur) {
		if (l == r)
				return (LL) cur * c[l];
		int mid = (l + r) >> 1;
		if (cnt[rc[now]] >= cur) return query_sum(rc[now] , mid + 1 , r , cur);
		else return sum[rc[now]] + query_sum(lc[now] , l , mid , cur - cnt[rc[now]]);
}

int main() {

		int T; read(T);
		while (T--) {
				read(n); read(m); len = 0;
				for (int i = 1; i <= m; ++i) {
						read(a[i].a);
						read(a[i].b);
						c[++len] = a[i].a;
				}
				sort(a + 1 , a + 1 + m , cmp);
				sort(c + 1 , c + 1 + len);
				len = unique(c + 1 , c + 1 + len) - c - 1;
				sz = 0; rt[m + 1] = 0;
				for (int i = m; i >= 1; --i) {
						int x = lower_bound(c + 1 , c + 1 + len , a[i].a) - c;
						modify(rt[i] , rt[i + 1] , 1 , len , x , a[i].a);
				}
				LL answer = a[m].a + (LL) (n - 1) * a[m].b;
				for (int i = 1; i < m; ++i) {
						LL cur = a[i].a; int need = n - 1;
						int x = query_rank(rt[i + 1] , 1 , len , need);
						if (c[x] < a[i].b) {
								int up = lower_bound(c + 1 , c + 1 + len , a[i].b) - c;
								int rk = query_cnt(rt[i + 1] , 1 , len , up , len);
								cur += (LL) query_sum(rt[i + 1] , 1 , len , rk);
								need -= rk;
						} else {
								cur += (LL) query_sum(rt[i + 1] , 1 , len , need);
								need = 0;
						}
						if (need >= 0) cur += (LL) need * a[i].b;
						chkmax(answer , cur);
				}
				printf("%lld\n" , answer);
		}
	  return 0;
}


/*
	  Author : @evenbao
	  Created : 2020 / 07 / 19
*/

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;

#define pii pair<int , int>
#define mp make_pair
#define fi first
#define se second

const int N = 1e5 + 10;

struct node {
		int a , b;
} a[N];

int n , m , sz , len , rt;
int cnt[N * 4] , bb[N] , ps[N] , lc[N * 4] , rc[N * 4] , c[N];
LL sum[N * 4] , aa[N];

template <typename T> inline void chkmax(T &x , T y) { x = max(x , y); }
template <typename T> inline void chkmin(T &x , T y) { x = min(x , y); }
template <typename T> inline void read(T &x) {
    T f = 1; x = 0;
    char c = getchar();
    for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
    for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
    x *= f;
}

inline bool cmp(node a , node b) {
		if (a.b != b.b) return a.b > b.b;
		else return a.a > b.a;
}

inline void build(int &now , int l , int r) {
		now = ++sz;
		if (l == r) {
				sum[now] = aa[l]; cnt[now] = bb[l];
				return;
		}
		int mid = l + r >> 1;
		build(lc[now] , l , mid); build(rc[now] , mid + 1 , r);
		sum[now] = sum[lc[now]] + sum[rc[now]];
		cnt[now] = cnt[lc[now]] + cnt[rc[now]];
}
inline void modify(int now , int l , int r , int x , LL val , int ad) {
 		sum[now] += val; cnt[now] += ad;
		if (l == r) return;
		int mid = (l + r) >> 1;
		if (mid >= x) modify(lc[now] , l , mid , x , val , ad);
		else modify(rc[now] , mid + 1 , r , x , val , ad);
}
inline int query_cnt(int now , int l , int r , int ql , int qr) {
		if (ql > qr) return 0;
		if (l == ql && r == qr) return cnt[now];
		int mid = (l + r) >> 1;
		if (mid >= qr) return query_cnt(lc[now] , l , mid , ql , qr);
		else if (mid + 1 <= ql) return query_cnt(rc[now] , mid + 1 , r , ql , qr);
		else return query_cnt(lc[now] , l , mid , ql , mid) + query_cnt(rc[now] , mid + 1 , r , mid + 1 , qr);
}
inline LL query_sum(int now , int l , int r , int cur) {
		if (l == r) return (LL) cur * c[l];
		int mid = (l + r) >> 1;
		if (cnt[rc[now]] >= cur) return query_sum(rc[now] , mid + 1 , r , cur);
		else return sum[rc[now]] + query_sum(lc[now] , l , mid , cur - cnt[rc[now]]);
}

int main() {

		int T; read(T);
		while (T--) {
				read(n); read(m); len = rt = sz = 0;
				for (int i = 1; i <= m; ++i) {
						read(a[i].a);
						read(a[i].b);
						c[++len] = a[i].a;
				}
				sort(a + 1 , a + 1 + m , cmp);
				sort(c + 1 , c + 1 + len);
				len = unique(c + 1 , c + 1 + len) - c - 1;
				for (int i = 2; i <= m; ++i) {
						ps[i] = lower_bound(c + 1 , c + 1 + len , a[i].a) - c;
						aa[ps[i]] += a[i].a; bb[ps[i]] += 1;
				}
				build(rt , 1 , len);
				for (int i = 1; i <= len; ++i)
						aa[i] = bb[i] = 0;
				LL answer = a[m].a + (LL) (n - 1) * a[m].b;
				int up = len + 1;
				for (int i = 1; i < m; ++i) {
						LL cur = a[i].a; int need = n - 1;
						while (up > 1 && c[up - 1] >= a[i].b) --up;
				//		int up = lower_bound(c + 1 , c + 1 + len , a[i].b) - c;
						int rk = query_cnt(rt , 1 , len , up , len);
						if (rk < need) {
								cur += (LL) query_sum(rt , 1 , len , rk);
								need -= rk;
						} else {
								cur += (LL) query_sum(rt , 1 , len , need);
								need = 0;
						}
						if (need >= 0) cur += (LL) need * a[i].b;
						chkmax(answer , cur);
						modify(rt , 1 , len , ps[i + 1] , -a[i + 1].a , -1);
				}
				printf("%lld\n" , answer);
		}
	  return 0;
}


/*
	  Author : @evenbao
	  Created : 2020 / 07 / 19
*/

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;

#define pii pair<int , int>
#define mp make_pair
#define fi first
#define se second

const int N = 2e5 + 10;

struct node {
		int a , b;
} a[N];

int n , m , sz , len;
int cnt[N * 30] , lc[N * 30] , rc[N * 30] , c[N] , rt[N];
LL sum[N * 30];

template <typename T> inline void chkmax(T &x , T y) { x = max(x , y); }
template <typename T> inline void chkmin(T &x , T y) { x = min(x , y); }
template <typename T> inline void read(T &x) {
    T f = 1; x = 0;
    char c = getchar();
    for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
    for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
    x *= f;
}

inline bool cmp(node a , node b) {
		if (a.b != b.b) return a.b > b.b;
		else return a.a > b.a;
}

inline void modify(int &now , int old , int l , int r , int x , LL val) {
		now = ++sz;
		lc[now] = lc[old] , rc[now] = rc[old];
		sum[now] = sum[old] + val; cnt[now] = cnt[old] + 1;
		if (l == r) return;
		int mid = (l + r) >> 1;
		if (mid >= x) modify(lc[now] , lc[old] , l , mid , x , val);
		else modify(rc[now] , rc[old] , mid + 1 , r , x , val);
}
inline int query_rank(int now , int l , int r , int x) {
		if (l == r)
				return l;
		int mid = (l + r) >> 1;
		if (cnt[rc[now]] >= x) return query_rank(rc[now] , mid + 1 , r , x);
		else return query_rank(lc[now] , l , mid , x - cnt[rc[now]]);
}
inline int query_cnt(int now , int l , int r , int ql , int qr) {
		if (l == ql && r == qr)
				return cnt[now];
		int mid = (l + r) >> 1;
		if (mid >= qr) return query_cnt(lc[now] , l , mid , ql , qr);
		else if (mid + 1 <= ql) return query_cnt(rc[now] , mid + 1 , r , ql , qr);
		else return query_cnt(lc[now] , l , mid , ql , mid) + query_cnt(rc[now] , mid + 1 , r , mid + 1 , qr);
}
inline LL query_sum(int now , int l , int r , int cur) {
		if (l == r)
				return (LL) cur * c[l];
		int mid = (l + r) >> 1;
		if (cnt[rc[now]] >= cur) return query_sum(rc[now] , mid + 1 , r , cur);
		else return query_sum(lc[now] , l , mid , cur - cnt[rc[now]]);
}

int main() {

		int T; read(T);
		while (T--) {
				read(n); read(m); len = 0;
				for (int i = 1; i <= m; ++i) {
						read(a[i].a);
						read(a[i].b);
						c[++len] = a[i].a;
				}
				sort(a + 1 , a + 1 + m , cmp);
				sort(c + 1 , c + 1 + len);
				len = unique(c + 1 , c + 1 + len) - c - 1;
				sz = 0; rt[m + 1] = 0;
				for (int i = m; i >= 1; --i) {
						int x = lower_bound(c + 1 , c + 1 + len , a[i].a) - c;
						modify(rt[i] , rt[i + 1] , 1 , len , x , a[i].a);
				}
				LL answer = a[m].a + (LL) (n - 1) * a[m].b;
				for (int i = 1; i < m; ++i) {
						LL cur = a[i].a; int need = n - 1;
						int x = query_rank(rt[i + 1] , 1 , len , need);
						if (c[x] < a[i].b) {
								int up = lower_bound(c + 1 , c + 1 + len , a[i].b) - c;
								int rk = query_cnt(rt[i + 1] , 1 , len , up , len);
								cur += (LL) query_sum(rt[i + 1] , 1 , len , rk);
								need -= rk;
						} else {
								cur += (LL) query_sum(rt[i + 1] , 1 , len , need);
								need = 0;
						}
						if (need > 0) cur += (LL) need * a[i].b;
						chkmax(answer , cur);
				}
				printf("%lld\n" , answer);
		}
	  return 0;
}


/*
	  Author : @evenbao
	  Created : 2020 / 07 / 19
*/

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;

#define pii pair<int , int>
#define mp make_pair
#define fi first
#define se second

const int N = 2e5 + 10;

struct node {
		int a , b;
} a[N];

int n , m , sz , len;
int cnt[N * 30] , lc[N * 30] , rc[N * 30] , c[N] , rt[N];
LL sum[N * 30];

template <typename T> inline void chkmax(T &x , T y) { x = max(x , y); }
template <typename T> inline void chkmin(T &x , T y) { x = min(x , y); }
template <typename T> inline void read(T &x) {
    T f = 1; x = 0;
    char c = getchar();
    for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
    for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
    x *= f;
}

inline bool cmp(node a , node b) {
		if (a.b != b.b) return a.b > b.b;
		else return a.a > b.a;
}

inline void modify(int &now , int old , int l , int r , int x , LL val) {
		now = ++sz;
		lc[now] = lc[old] , rc[now] = rc[old];
		sum[now] = sum[old] + val; cnt[now] = cnt[old] + 1;
		if (l == r) return;
		int mid = (l + r) >> 1;
		if (mid >= x) modify(lc[now] , lc[old] , l , mid , x , val);
		else modify(rc[now] , rc[old] , mid + 1 , r , x , val);
}
inline int query_rank(int now , int l , int r , int x) {
		if (l == r)
				return l;
		int mid = (l + r) >> 1;
		if (cnt[rc[now]] >= x) return query_rank(rc[now] , mid + 1 , r , x);
		else return query_rank(lc[now] , l , mid , x - cnt[rc[now]]);
}
inline int query_cnt(int now , int l , int r , int ql , int qr) {
		if (l == ql && r == qr)
				return cnt[now];
		int mid = (l + r) >> 1;
		if (mid >= qr) return query_cnt(lc[now] , l , mid , ql , qr);
		else if (mid + 1 <= ql) return query_cnt(rc[now] , mid + 1 , r , ql , qr);
		else return query_cnt(lc[now] , l , mid , ql , mid) + query_cnt(rc[now] , mid + 1 , r , mid + 1 , qr);
}
inline int query_sum(int now , int l , int r , int cur) {
		if (l == r)
				return (LL) cur * c[l];
		int mid = (l + r) >> 1;
		if (cnt[rc[now]] >= cur) return query_sum(rc[now] , mid + 1 , r , cur);
		else return query_sum(lc[now] , l , mid , cur - cnt[rc[now]]);
}

int main() {

		int T; read(T);
		while (T--) {
				read(n); read(m); len = 0;
				for (int i = 1; i <= m; ++i) {
						read(a[i].a);
						read(a[i].b);
						c[++len] = a[i].a;
				}
				sort(a + 1 , a + 1 + m , cmp);
				sort(c + 1 , c + 1 + len);
				len = unique(c + 1 , c + 1 + len) - c - 1;
				sz = 0; rt[m + 1] = 0;
				for (int i = m; i >= 1; --i) {
						int x = lower_bound(c + 1 , c + 1 + len , a[i].a) - c;
						modify(rt[i] , rt[i + 1] , 1 , len , x , a[i].a);
				}
				LL answer = a[m].a + (LL) (n - 1) * a[m].b;
				for (int i = 1; i < m; ++i) {
						LL cur = a[i].a; int need = n - 1;
						int x = query_rank(rt[i + 1] , 1 , len , need);
						if (c[x] < a[i].b) {
								int up = lower_bound(c + 1 , c + 1 + len , a[i].b) - c;
								int rk = query_cnt(rt[i + 1] , 1 , len , up , len);
								cur += query_sum(rt[i + 1] , 1 , len , rk);
								need -= rk;
						} else {
								cur += query_sum(rt[i + 1] , 1 , len , need);
								need = 0;
						}
						if (need > 0) cur += (LL) need * a[i].b;
						chkmax(answer , cur);
				}
				printf("%lld\n" , answer);
		}
	  return 0;
}


/*
	  Author : @evenbao
	  Created : 2020 / 07 / 19
*/

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;

#define pii pair<int , int>
#define mp make_pair
#define fi first
#define se second

const int N = 1e5 + 10;

struct node {
		int a , b;
} a[N];

int n , m , sz , len , rt;
int cnt[N * 4] , ps[N] , lc[N * 4] , rc[N * 4] , c[N];
LL sum[N * 4];

template <typename T> inline void chkmax(T &x , T y) { x = max(x , y); }
template <typename T> inline void chkmin(T &x , T y) { x = min(x , y); }
template <typename T> inline void read(T &x) {
    T f = 1; x = 0;
    char c = getchar();
    for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
    for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
    x *= f;
}

inline bool cmp(node a , node b) {
		if (a.b != b.b) return a.b > b.b;
		else return a.a > b.a;
}

inline void modify(int &now , int l , int r , int x , LL val , int ad) {
		if (!now) now = ++sz;
 		sum[now] += val; cnt[now] += ad;
		if (l == r) return;
		int mid = (l + r) >> 1;
		if (mid >= x) modify(lc[now] , l , mid , x , val , ad);
		else modify(rc[now] , mid + 1 , r , x , val , ad);
}
inline int query_cnt(int now , int l , int r , int ql , int qr) {
		if (l == ql && r == qr) return cnt[now];
		int mid = (l + r) >> 1;
		if (mid >= qr) return query_cnt(lc[now] , l , mid , ql , qr);
		else if (mid + 1 <= ql) return query_cnt(rc[now] , mid + 1 , r , ql , qr);
		else return query_cnt(lc[now] , l , mid , ql , mid) + query_cnt(rc[now] , mid + 1 , r , mid + 1 , qr);
}
inline LL query_sum(int now , int l , int r , int cur) {
		if (l == r) return (LL) cur * c[l];
		int mid = (l + r) >> 1;
		if (cnt[rc[now]] >= cur) return query_sum(rc[now] , mid + 1 , r , cur);
		else return sum[rc[now]] + query_sum(lc[now] , l , mid , cur - cnt[rc[now]]);
}

int main() {

		int T; read(T);
		while (T--) {
				read(n); read(m); len = rt = sz = 0;
				for (int i = 1; i <= m; ++i) {
						read(a[i].a);
						read(a[i].b);
						c[++len] = a[i].a;
				}
				sort(a + 1 , a + 1 + m , cmp);
				sort(c + 1 , c + 1 + len);
				len = unique(c + 1 , c + 1 + len) - c - 1;
				for (int i = 2; i <= m; ++i) {
						ps[i] = lower_bound(c + 1 , c + 1 + len , a[i].a) - c;
						modify(rt , 1 , len , ps[i] , a[i].a , 1);
				}
				LL answer = a[m].a + (LL) (n - 1) * a[m].b;
				for (int i = 1; i < m; ++i) {
						LL cur = a[i].a; int need = n - 1;
						int up = lower_bound(c + 1 , c + 1 + len , a[i].b) - c;
						int rk = query_cnt(rt , 1 , len , up , len);
						if (rk < need) {
								cur += (LL) query_sum(rt , 1 , len , rk);
								need -= rk;
						} else {
								cur += (LL) query_sum(rt , 1 , len , need);
								need = 0;
						}
						if (need >= 0) cur += (LL) need * a[i].b;
						chkmax(answer , cur);
						modify(rt , 1 , len , ps[i + 1] , -a[i + 1].a , -1);
				}
				printf("%lld\n" , answer);
		}
	  return 0;
}


/*
	  Author : @evenbao
	  Created : 2020 / 07 / 19
*/

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;

#define pii pair<int , int>
#define mp make_pair
#define fi first
#define se second

const int N = 1e5 + 10;

struct node {
		int a , b;
} a[N];

int n , m , sz , len , rt;
int cnt[N * 4] , bb[N] , ps[N] , lc[N * 4] , rc[N * 4] , c[N];
LL sum[N * 4] , aa[N];

template <typename T> inline void chkmax(T &x , T y) { x = max(x , y); }
template <typename T> inline void chkmin(T &x , T y) { x = min(x , y); }
template <typename T> inline void read(T &x) {
    T f = 1; x = 0;
    char c = getchar();
    for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
    for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
    x *= f;
}

inline bool cmp(node a , node b) {
		if (a.b != b.b) return a.b > b.b;
		else return a.a > b.a;
}

inline void build(int &now , int l , int r) {
		now = ++sz;
		if (l == r) {
				sum[now] = aa[l]; cnt[now] = bb[l];
				return;
		}
		int mid = l + r >> 1;
		build(lc[now] , l , mid); build(rc[now] , mid + 1 , r);
		sum[now] = sum[lc[now]] + sum[rc[now]];
		cnt[now] = cnt[lc[now]] + cnt[rc[now]];
}
inline void modify(int now , int l , int r , int x , LL val , int ad) {
 		sum[now] += val; cnt[now] += ad;
		if (l == r) return;
		int mid = (l + r) >> 1;
		if (mid >= x) modify(lc[now] , l , mid , x , val , ad);
		else modify(rc[now] , mid + 1 , r , x , val , ad);
}
inline int query_cnt(int now , int l , int r , int ql , int qr) {
		if (l == ql && r == qr) return cnt[now];
		int mid = (l + r) >> 1;
		if (mid >= qr) return query_cnt(lc[now] , l , mid , ql , qr);
		else if (mid + 1 <= ql) return query_cnt(rc[now] , mid + 1 , r , ql , qr);
		else return query_cnt(lc[now] , l , mid , ql , mid) + query_cnt(rc[now] , mid + 1 , r , mid + 1 , qr);
}
inline LL query_sum(int now , int l , int r , int cur) {
		if (l == r) return (LL) cur * c[l];
		int mid = (l + r) >> 1;
		if (cnt[rc[now]] >= cur) return query_sum(rc[now] , mid + 1 , r , cur);
		else return sum[rc[now]] + query_sum(lc[now] , l , mid , cur - cnt[rc[now]]);
}

int main() {

		int T; read(T);
		while (T--) {
				read(n); read(m); len = rt = sz = 0;
				for (int i = 1; i <= m; ++i) {
						read(a[i].a);
						read(a[i].b);
						c[++len] = a[i].a;
				}
				sort(a + 1 , a + 1 + m , cmp);
				sort(c + 1 , c + 1 + len);
				len = unique(c + 1 , c + 1 + len) - c - 1;
				for (int i = 2; i <= m; ++i) {
						ps[i] = lower_bound(c + 1 , c + 1 + len , a[i].a) - c;
						aa[ps[i]] += a[i].a; bb[ps[i]] += 1;
				}
				build(rt , 1 , len);
				for (int i = 1; i <= len; ++i)
						aa[i] = bb[i] = 0;
				LL answer = a[m].a + (LL) (n - 1) * a[m].b;
				for (int i = 1; i < m; ++i) {
						LL cur = a[i].a; int need = n - 1;
						int up = lower_bound(c + 1 , c + 1 + len , a[i].b) - c;
						int rk = query_cnt(rt , 1 , len , up , len);
						if (rk < need) {
								cur += (LL) query_sum(rt , 1 , len , rk);
								need -= rk;
						} else {
								cur += (LL) query_sum(rt , 1 , len , need);
								need = 0;
						}
						if (need >= 0) cur += (LL) need * a[i].b;
						chkmax(answer , cur);
						modify(rt , 1 , len , ps[i + 1] , -a[i + 1].a , -1);
				}
				printf("%lld\n" , answer);
		}
	  return 0;
}


/*
	  Author : @evenbao
	  Created : 2020 / 07 / 19
*/

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;

#define pii pair<int , int>
#define mp make_pair
#define fi first
#define se second

const int N = 2e5 + 10;

struct node {
		int a , b;
} a[N];

int n , m , sz , len;
int cnt[N * 30] , lc[N * 30] , rc[N * 30] , c[N] , rt[N];
LL sum[N * 30];

template <typename T> inline void chkmax(T &x , T y) { x = max(x , y); }
template <typename T> inline void chkmin(T &x , T y) { x = min(x , y); }
template <typename T> inline void read(T &x) {
    T f = 1; x = 0;
    char c = getchar();
    for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
    for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
    x *= f;
}

inline bool cmp(node a , node b) {
		if (a.b != b.b) return a.b > b.b;
		else return a.a > b.a;
}

inline void modify(int &now , int old , int l , int r , int x , LL val) {
		now = ++sz;
		lc[now] = lc[old] , rc[now] = rc[old];
		sum[now] = sum[old] + val; cnt[now] = cnt[old] + 1;
		if (l == r) return;
		int mid = (l + r) >> 1;
		if (mid >= x) modify(lc[now] , lc[old] , l , mid , x , val);
		else modify(rc[now] , rc[old] , mid + 1 , r , x , val);
}
inline int query_rank(int now , int l , int r , int x) {
		if (l == r)	return cnt[now] >= x ? l : l - 1;
		int mid = (l + r) >> 1;
		if (cnt[rc[now]] >= x) return query_rank(rc[now] , mid + 1 , r , x);
		else return query_rank(lc[now] , l , mid , x - cnt[rc[now]]);
}
inline int query_cnt(int now , int l , int r , int ql , int qr) {
		if (l == ql && r == qr) return cnt[now];
		int mid = (l + r) >> 1;
		if (mid >= qr) return query_cnt(lc[now] , l , mid , ql , qr);
		else if (mid + 1 <= ql) return query_cnt(rc[now] , mid + 1 , r , ql , qr);
		else return query_cnt(lc[now] , l , mid , ql , mid) + query_cnt(rc[now] , mid + 1 , r , mid + 1 , qr);
}
inline LL query_sum(int now , int l , int r , int cur) {
		if (l == r) return (LL) cur * c[l];
		int mid = (l + r) >> 1;
		if (cnt[rc[now]] >= cur) return query_sum(rc[now] , mid + 1 , r , cur);
		else return sum[rc[now]] + query_sum(lc[now] , l , mid , cur - cnt[rc[now]]);
}

int main() {

		int T; read(T);
		while (T--) {
				read(n); read(m); len = 0;
				for (int i = 1; i <= m; ++i) {
						read(a[i].a);
						read(a[i].b);
						c[++len] = a[i].a;
				}
				sort(a + 1 , a + 1 + m , cmp);
				sort(c + 1 , c + 1 + len);
				len = unique(c + 1 , c + 1 + len) - c - 1;
				sz = 0; rt[m + 1] = 0;
				for (int i = m; i >= 1; --i) {
						int x = lower_bound(c + 1 , c + 1 + len , a[i].a) - c;
						modify(rt[i] , rt[i + 1] , 1 , len , x , a[i].a);
				}
				LL answer = a[m].a + (LL) (n - 1) * a[m].b;
				for (int i = 1; i < m; ++i) {
						LL cur = a[i].a; int need = n - 1;
						int x = query_rank(rt[i + 1] , 1 , len , need);
						if (c[x] < a[i].b) {
								int up = lower_bound(c + 1 , c + 1 + len , a[i].b) - c;
								int rk = query_cnt(rt[i + 1] , 1 , len , up , len);
								cur += (LL) query_sum(rt[i + 1] , 1 , len , rk);
								need -= rk;
						} else {
								cur += (LL) query_sum(rt[i + 1] , 1 , len , need);
								need = 0;
						}
						if (need >= 0) cur += (LL) need * a[i].b;
						chkmax(answer , cur);
				}
				printf("%lld\n" , answer);
		}
	  return 0;
}


/*
	  Author : @evenbao
	  Created : 2020 / 07 / 19
*/

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;

#define pii pair<int , int>
#define mp make_pair
#define fi first
#define se second

const int N = 1e5 + 10;

struct node {
		int a , b;
} a[N];

int n , m , sz , len;
int cnt[N * 30] , lc[N * 30] , rc[N * 30] , c[N] , rt[N];
LL sum[N * 30];

template <typename T> inline void chkmax(T &x , T y) { x = max(x , y); }
template <typename T> inline void chkmin(T &x , T y) { x = min(x , y); }
template <typename T> inline void read(T &x) {
    T f = 1; x = 0;
    char c = getchar();
    for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
    for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
    x *= f;
}

inline bool cmp(node a , node b) {
		if (a.b != b.b) return a.b > b.b;
		else return a.a > b.a;
}

inline void modify(int &now , int old , int l , int r , int x , LL val) {
		now = ++sz;
		lc[now] = lc[old] , rc[now] = rc[old];
		sum[now] = sum[old] + val; cnt[now] = cnt[old] + 1;
		if (l == r) return;
		int mid = (l + r) >> 1;
		if (mid >= x) modify(lc[now] , lc[old] , l , mid , x , val);
		else modify(rc[now] , rc[old] , mid + 1 , r , x , val);
}
inline int query_rank(int now , int l , int r , int x) {
		if (l == r)	return cnt[now] >= x ? l : l - 1;
		int mid = (l + r) >> 1;
		if (cnt[rc[now]] >= x) return query_rank(rc[now] , mid + 1 , r , x);
		else return query_rank(lc[now] , l , mid , x - cnt[rc[now]]);
}
inline int query_cnt(int now , int l , int r , int ql , int qr) {
		if (l == ql && r == qr) return cnt[now];
		int mid = (l + r) >> 1;
		if (mid >= qr) return query_cnt(lc[now] , l , mid , ql , qr);
		else if (mid + 1 <= ql) return query_cnt(rc[now] , mid + 1 , r , ql , qr);
		else return query_cnt(lc[now] , l , mid , ql , mid) + query_cnt(rc[now] , mid + 1 , r , mid + 1 , qr);
}
inline LL query_sum(int now , int l , int r , int cur) {
		if (l == r) return (LL) cur * c[l];
		int mid = (l + r) >> 1;
		if (cnt[rc[now]] >= cur) return query_sum(rc[now] , mid + 1 , r , cur);
		else return sum[rc[now]] + query_sum(lc[now] , l , mid , cur - cnt[rc[now]]);
}

int main() {

		int T; read(T);
		while (T--) {
				read(n); read(m); len = 0;
				for (int i = 1; i <= m; ++i) {
						read(a[i].a);
						read(a[i].b);
						c[++len] = a[i].a;
				}
				sort(a + 1 , a + 1 + m , cmp);
				sort(c + 1 , c + 1 + len);
				len = unique(c + 1 , c + 1 + len) - c - 1;
				sz = 0; rt[m + 1] = 0;
				for (int i = m; i >= 1; --i) {
						int x = lower_bound(c + 1 , c + 1 + len , a[i].a) - c;
						modify(rt[i] , rt[i + 1] , 1 , len , x , a[i].a);
				}
				LL answer = a[m].a + (LL) (n - 1) * a[m].b;
				for (int i = 1; i < m; ++i) {
						LL cur = a[i].a; int need = n - 1;
						int up = lower_bound(c + 1 , c + 1 + len , a[i].b) - c;
						int rk = query_cnt(rt[i + 1] , 1 , len , up , len);
						if (rk < need) {
								cur += (LL) query_sum(rt[i + 1] , 1 , len , rk);
								need -= rk;
						} else {
								cur += (LL) query_sum(rt[i + 1] , 1 , len , need);
								need = 0;
						}
						if (need >= 0) cur += (LL) need * a[i].b;
						chkmax(answer , cur);
				}
				printf("%lld\n" , answer);
		}
	  return 0;
}