//region trash
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>

using namespace std;

#define DB(x){if(cond)cerr<<__LINE__<<" "<<#x<<" " <<x<<endl;}
#define REP(i,b)for(auto i=0;i<(b);++i)
#define FOR(i,a,b)for(auto i=(a);i<=(b);++i)
#define FORD(i,a,b)for(auto i=(a);i>=(b);--i)
#define FORDE(rit,x)for(auto rit=(x).rbegin();rit!=(x).rend();rit++)
#define VV vector
#define PB push_back
#define ALL(x) (x).begin(),(x).end()
template<typename X, typename Y>void MA(X&a,Y b){a=max(a,b);}
template<typename X, typename Y>void MI(X&a,Y b){a=min(a,b);}
typedef long long LL,ll;typedef long double LD,ld;
typedef array<int,2>I2,i2;typedef VV<int>VI,vi;
int cond=0,multi=0,gcj=0;
using namespace std;
//endregion

struct edg_t {
  int other, e;
};

struct fus_t {
  VV<int>PARENT;
  fus_t(){}
  fus_t(int N){
    PARENT.resize(N);
    REP(n,N)PARENT[n]=n;
  }

  int parent(int u) {
    if (PARENT[u] == u) return u;
    return PARENT[u] = parent(PARENT[u]);
  }

  bool join(int u, int v) {
    int pu=parent(u);
    int pv=parent(v);
    if(pu==pv)return false;

    PARENT[pu] = pv;
    return true;
  }
};

struct Solver {
  int N,M,D;

  VV<VV<edg_t>>EDG;

  VV<int>KOL;
  VV<int>VIS;
  fus_t fus;

  int go(int u, int p, int parent_e) {
    if (VIS[u] == -2) {
      DB(u+1<<" "<<p+1);
      return INT_MAX;
    }
    if (VIS[u] == -1) {
      DB(u+1<<" "<<p+1);
      VIS[u]=KOL.size();
    }
    else {
      DB(u+1<<" "<<p+1);
      assert(parent_e >= 0);

      return VIS[u];
    }
    int merge = INT_MAX;
    int cur_id = KOL.size();
    for(const auto &edg:EDG[u]){
      if (edg.other == p) continue;
      KOL.push_back(edg.e);

      DB(edg.other<<" "<<u);
      int res = go(edg.other, u, edg.e);
      DB(res);
      if (res < cur_id) {
        assert(parent_e >= 0);
        DB(edg.e<<" "<<parent_e);
        fus.join(edg.e, parent_e);
      }
      MI(merge, res);

      KOL.pop_back();
    }
    VIS[u] = -2;
    return merge;
  }

  VV<array<int,2>>RES;

  void go(int v) {
    if (VIS[v]) return;
    VIS[v] = 1;
    for(auto edg:EDG[v]){
      int u = edg.other;
      if(VIS[u])continue;
      RES.push_back({v,u});
      go(u);
    }
  }

  void solve() {
    cin>>N>>M>>D;
    EDG.resize(N);
    VIS.resize(N,-1);
    fus = fus_t(M);
    REP(m,M){
      int a,b;cin>>a>>b;
      a--,b--;
      EDG[a].push_back({b,m});
      EDG[b].push_back({a,m});
    }
    go(0,-1, -1);
    REP(m,M)if(fus.parent(m) == m) DB(m);
    REP(m,M)DB(m<<" "<<fus.parent(m));

    VIS.clear();VIS.resize(N);
    REP(n,N) {
      int edges = EDG[n].size();
      map<int,int>groups;
      for(auto edg:EDG[n]) groups[fus.parent(edg.e)] += 1;

      if (groups.size() <= D && D <= EDG[n].size()){
        cout<<"YES\n";
        VIS[n] = 1;
        int cur = EDG[n].size();
        auto tmp = EDG[n]; tmp.clear();

        for(auto edg:EDG[n]){
          if (groups[fus.parent(edg.e)] > 1 && D != cur) {
            cur--;
            continue;
            groups[fus.parent(edg.e)] -= 1;
          }
          int v = edg.other;
          VIS[v] = 1;
          tmp.push_back(edg);
        }

        for(auto edg:tmp){
          int v = edg.other;
          VIS[v] = 0;
          RES.push_back({n, v});
          go(v);
        }

        for(auto kr:RES)cout<<kr[0]+1<<" "<<kr[1]+1<<endl;

        return;
      }
    }
    cout<<"NO"<<endl;
  }
};

struct Solver2 {
  void gen() {}
  void brute() {}
};

int main(int argc,char** argv){
  random_device rd{}; srand(rd());

  ios::sync_with_stdio(false),cin.tie(nullptr);
  FOR(a,1,argc-1){if(argv[a][0]=='g') { Solver2 solver; solver.gen(); return 0; }}
  FOR(a,1,argc-1){if(argv[a][0]=='q') cond=1U<<30U;}
  cout.setf(ios::fixed),cout.precision(10);
  int t{};if(multi||gcj)cin>>t;else t=1;
  bool isBrute=false;FOR(a,1,argc-1)if(argv[a][0]=='b')isBrute=true;
  FOR(i,1,t){if(cond)cerr<<__LINE__<<" "<<i<<endl;if(gcj)cout<<"Case #"<<i<<": ";
    auto solver = make_unique<Solver>();
    auto solver2 = make_unique<Solver2>();
    if (isBrute)
      solver2->brute();
    else
      solver->solve();
  }
  return 0;
}


#include<bits/stdc++.h>
#define db(x) cerr<<#x<<" = "<<x<<endl;
using namespace std;
struct _edge{
	int u,v,nxt;
}edge[400005];
int inn[200005];
int fa[200005];
bool cho[200005];
int fl[200005];
int ecnt;
int hed[200005];
bool met[200005];
int f(int x){
	if(fa[x]==x)return x;
	else return fa[x]=f(fa[x]);
}
void uni(int x,int y){
	if(rand()&1){
		fa[f(x)]=y;
	}else{
		fa[f(y)]=x;
	}
	return;
}
void add(int u,int v){
	edge[++ecnt]=(_edge){u,v,hed[u]};
	hed[u]=ecnt;
	return;
}
void dfs(int rt,int col){
	//cerr<<"DFS "<<rt<<' '<<col<<endl;
	fl[rt]=col;
	for(int i=hed[rt];~i;i=edge[i].nxt){
		int v=edge[i].v;
		if(fl[v]||v==1)continue;
		dfs(v,col);
	}
	return;
}
int main(){
	memset(hed,-1,sizeof(hed));
	int n,m,d;
	scanf("%d%d%d",&n,&m,&d);
	for(int i=1;i<=m;++i){
		int uu,vv;
		scanf("%d%d",&uu,&vv);
		++inn[uu];
		++inn[vv];
		add(uu,vv);
		add(vv,uu);
	}
	if(inn[1]<d){
		puts("NO");
		return 0;
	}
	puts("YES");
	for(int i=1;i<=n;++i)fa[i]=i;
	int cnt=0;
	for(int i=2;i<=n;++i){
		if(!fl[i]){
			dfs(i,++cnt);
		}
	}
	int fr=d-cnt;

	for(int i=hed[1];~i;i=edge[i].nxt){
		int v=edge[i].v;
		if(!met[fl[v]]){
			met[fl[v]]=1;
			uni(edge[i].u,edge[i].v);
			cho[i]=1;
		}else if(fr){
			--fr;
			cho[i]=1;
			uni(edge[i].u,edge[i].v);
		}
	}

	for(int i=1;i<=ecnt;++i){
		if(f(edge[i].u)!=f(edge[i].v)&&cho[i]==0&&edge[i].u!=1&&edge[i].v!=1){
			uni(edge[i].u,edge[i].v);
			cho[i]=1;
		}
	}
	for(int i=1;i<=ecnt;++i){
		if(cho[i]){
			printf("%d %d\n",edge[i].u,edge[i].v);
		}
	}
	return 0;
}


#include<bits/stdc++.h>
#define db(x) cerr<<#x<<" = "<<x<<endl;
using namespace std;
struct _edge{
	int u,v,nxt;
}edge[400205];
int inn[200205];
int fa[200205];
bool cho[400205];
int fl[200205];
int ecnt;
int hed[200205];
bool met[200205];
int f(int x){
	if(fa[x]==x)return x;
	else return fa[x]=f(fa[x]);
}
void uni(int x,int y){
	if(rand()&1){
		fa[f(x)]=y;
	}else{
		fa[f(y)]=x;
	}
	return;
}
void add(int u,int v){
	edge[++ecnt]=(_edge){u,v,hed[u]};
	hed[u]=ecnt;
	return;
}
void dfs(int rt,int col){
	//cerr<<"DFS "<<rt<<' '<<col<<endl;
	fl[rt]=col;
	for(int i=hed[rt];~i;i=edge[i].nxt){
		int v=edge[i].v;
		if(fl[v]||v==1)continue;
		dfs(v,col);
	}
	return;
}
int main(){
	memset(hed,-1,sizeof(hed));
	int n,m,d;
	scanf("%d%d%d",&n,&m,&d);
	for(int i=1;i<=m;++i){
		int uu,vv;
		scanf("%d%d",&uu,&vv);
		++inn[uu];
		++inn[vv];
		add(uu,vv);
		add(vv,uu);
	}
	if(inn[1]<d){
		puts("NO");
		return 0;
	}
	puts("YES");
	for(int i=1;i<=n;++i)fa[i]=i;
	int cnt=0;
	for(int i=2;i<=n;++i){
		if(!fl[i]){
			dfs(i,++cnt);
		}
	}
	int fr=d-cnt;

	for(int i=hed[1];~i;i=edge[i].nxt){
		int v=edge[i].v;
		if(!met[fl[v]]){
			met[fl[v]]=1;
			uni(edge[i].u,edge[i].v);
			cho[i]=1;
		}else if(fr){
			--fr;
			cho[i]=1;
			uni(edge[i].u,edge[i].v);
		}
	}

	for(int i=1;i<=ecnt;++i){
		if(f(edge[i].u)!=f(edge[i].v)&&cho[i]==0&&edge[i].u!=1&&edge[i].v!=1){
			uni(edge[i].u,edge[i].v);
			cho[i]=1;
		}
	}
	for(int i=1;i<=ecnt;++i){
		if(cho[i]){
			printf("%d %d\n",edge[i].u,edge[i].v);
		}
	}
	return 0;
}


#include<bits/stdc++.h>
#define db(x) cerr<<#x<<" = "<<x<<endl;
using namespace std;
struct _edge{
	int u,v,nxt;
}edge[400005];
int inn[200005];
int fa[200005];
bool cho[200005];
int fl[200005];
int ecnt;
int hed[200005];
bool met[200005];
int f(int x){
	if(fa[x]==x)return x;
	else return fa[x]=f(fa[x]);
}
void uni(int x,int y){
	if(rand()&1){
		fa[f(x)]=y;
	}else{
		fa[f(y)]=x;
	}
	return;
}
void add(int u,int v){
	edge[++ecnt]=(_edge){u,v,hed[u]};
	hed[u]=ecnt;
	return;
}
void dfs(int rt,int col){
	//cerr<<"DFS "<<rt<<' '<<col<<endl;
	fl[rt]=col;
	for(int i=hed[rt];~i;i=edge[i].nxt){
		int v=edge[i].v;
		if(fl[v]||v==1)continue;
		dfs(v,col);
	}
	return;
}
int main(){
	memset(hed,-1,sizeof(hed));
	int n,m,d;
	scanf("%d%d%d",&n,&m,&d);
	for(int i=1;i<=m;++i){
		int uu,vv;
		scanf("%d%d",&uu,&vv);
		++inn[uu];
		++inn[vv];
		add(uu,vv);
	}
	if(inn[1]<d){
		puts("NO");
		return 0;
	}
	puts("YES");
	for(int i=1;i<=n;++i)fa[i]=i;
	int cnt=0;
	for(int i=2;i<=n;++i){
		if(!fl[i]){
			dfs(i,++cnt);
		}
	}
	int ma=-1,mp;
	for(int i=1;i<=n;++i){
		fa[i]=i;
		if(inn[i]>ma){
			ma=inn[i];
			mp=i;
		}
	}
	int fr=d-cnt;

	for(int i=hed[1];~i;i=edge[i].nxt){
		int v=edge[i].v;
		if(!met[fl[v]]){
			met[fl[v]]=1;
			uni(edge[i].u,edge[i].v);
			cho[i]=1;
		}else if(fr){
			--fr;
			cho[i]=1;
			uni(edge[i].u,edge[i].v);
		}
	}

	for(int i=1;i<=ecnt;++i){
		if(f(edge[i].u)!=f(edge[i].v)&&cho[i]==0&&edge[i].u!=1&&edge[i].v!=1){
			uni(edge[i].u,edge[i].v);
			cho[i]=1;
		}
	}
	for(int i=1;i<=ecnt;++i){
		if(cho[i]){
			printf("%d %d\n",edge[i].u,edge[i].v);
		}
	}
	return 0;
}


#include<bits/stdc++.h>
#define db(x) cerr<<#x<<" = "<<x<<endl;
using namespace std;
struct _edge{
	int u,v,nxt;
}edge[400205];
int inn[200205];
int fa[200205];
bool cho[400205];
int fl[200205];
int ecnt;
int hed[200205];
bool met[200205];
int f(int x){
	if(fa[x]==x)return x;
	else return fa[x]=f(fa[x]);
}
void uni(int x,int y){
	if(f(x)==f(y))return;
	fa[f(y)]=x;
	return;
}
void add(int u,int v){
	edge[++ecnt]=(_edge){u,v,hed[u]};
	hed[u]=ecnt;
	return;
}
void dfs(int rt,int col){
	//cerr<<"DFS "<<rt<<' '<<col<<endl;
	fl[rt]=col;
	for(int i=hed[rt];~i;i=edge[i].nxt){
		int v=edge[i].v;
		if(fl[v]||v==1)continue;
		dfs(v,col);
	}
	return;
}
int main(){
	memset(hed,-1,sizeof(hed));
	int n,m,d;
	scanf("%d%d%d",&n,&m,&d);
	for(int i=1;i<=m;++i){
		int uu,vv;
		scanf("%d%d",&uu,&vv);
		++inn[uu];
		++inn[vv];
		add(uu,vv);
		add(vv,uu);
	}
	if(inn[1]<d){
		puts("NO");
		return 0;
	}

	for(int i=1;i<=n;++i)fa[i]=i;
	int cnt=0;
	for(int i=2;i<=n;++i){
		if(!fl[i]){
			dfs(i,++cnt);
		}
	}
	int fr=d-cnt;
	if(cnt>d){
		puts("NO");
		return 0;
	}
	puts("YES");
	for(int i=hed[1];~i;i=edge[i].nxt){
		int v=edge[i].v;
		if(!met[fl[v]]){
			met[fl[v]]=1;
			uni(edge[i].u,edge[i].v);
			cho[i]=1;
		}else if(fr){
			--fr;
			cho[i]=1;
			uni(edge[i].u,edge[i].v);
		}
	}

	for(int i=1;i<=ecnt;++i){
		if(f(edge[i].u)!=f(edge[i].v)&&cho[i]==0&&edge[i].u!=1&&edge[i].v!=1){
			uni(edge[i].u,edge[i].v);
			cho[i]=1;
		}
	}
	for(int i=1;i<=ecnt;++i){
		if(cho[i]){
			printf("%d %d\n",edge[i].u,edge[i].v);
		}
	}
	return 0;
}


#include<bits/stdc++.h>
#define db(x) cerr<<#x<<" = "<<x<<endl;
using namespace std;
struct _edge{
	int u,v,nxt;
}edge[400205];
int inn[200205];
int fa[200205];
bool cho[400205];
int fl[200205];
int ecnt;
int hed[200205];
bool met[200205];
int f(int x){
	if(fa[x]==x)return x;
	else return fa[x]=f(fa[x]);
}
void uni(int x,int y){
	if(f(x)==f(y))return;
	fa[f(x)]=f(y);
	return;
}
void add(int u,int v){
	edge[++ecnt]=(_edge){u,v,hed[u]};
	hed[u]=ecnt;
	return;
}
void dfs(int rt,int col){
	//cerr<<"DFS "<<rt<<' '<<col<<endl;
	fl[rt]=col;
	for(int i=hed[rt];~i;i=edge[i].nxt){
		int v=edge[i].v;
		if(fl[v]||v==1)continue;
		dfs(v,col);
	}
	return;
}
int main(){
	memset(hed,-1,sizeof(hed));
	int n,m,d;
	scanf("%d%d%d",&n,&m,&d);
	for(int i=1;i<=m;++i){
		int uu,vv;
		scanf("%d%d",&uu,&vv);
		++inn[uu];
		++inn[vv];
		add(uu,vv);
		add(vv,uu);
	}
	if(inn[1]<d){
		puts("NO");
		return 0;
	}
	puts("YES");
	for(int i=1;i<=n;++i)fa[i]=i;
	int cnt=0;
	for(int i=2;i<=n;++i){
		if(!fl[i]){
			dfs(i,++cnt);
		}
	}
	int fr=d-cnt;

	for(int i=hed[1];~i;i=edge[i].nxt){
		int v=edge[i].v;
		if(!met[fl[v]]){
			met[fl[v]]=1;
			uni(edge[i].u,edge[i].v);
			cho[i]=1;
		}else if(fr){
			--fr;
			cho[i]=1;
			uni(edge[i].u,edge[i].v);
		}
	}

	for(int i=1;i<=ecnt;++i){
		if(cho[i]==0&&edge[i].u!=1&&edge[i].v!=1){
			uni(edge[i].u,edge[i].v);
			cho[i]=1;
		}
	}
	for(int i=1;i<=ecnt;++i){
		if(cho[i]){
			printf("%d %d\n",edge[i].u,edge[i].v);
		}
	}
	return 0;
}


#include<bits/stdc++.h>
#define db(x) cerr<<#x<<" = "<<x<<endl;
using namespace std;
struct _edge{
	int u,v,nxt;
}edge[400205];
int inn[200205];
int fa[200205];
bool cho[400205];
int fl[200205];
int ecnt;
int hed[200205];
bool met[200205];
int f(int x){
	if(fa[x]==x)return x;
	else return fa[x]=f(fa[x]);
}
void uni(int x,int y){
	fa[f(x)]=f(y);
	return;
}
void add(int u,int v){
	edge[++ecnt]=(_edge){u,v,hed[u]};
	hed[u]=ecnt;
	return;
}
void dfs(int rt,int col){
	//cerr<<"DFS "<<rt<<' '<<col<<endl;
	fl[rt]=col;
	for(int i=hed[rt];~i;i=edge[i].nxt){
		int v=edge[i].v;
		if(fl[v]||v==1)continue;
		dfs(v,col);
	}
	return;
}
int main(){
	memset(hed,-1,sizeof(hed));
	int n,m,d;
	scanf("%d%d%d",&n,&m,&d);
	for(int i=1;i<=m;++i){
		int uu,vv;
		scanf("%d%d",&uu,&vv);
		++inn[uu];
		++inn[vv];
		add(uu,vv);
		add(vv,uu);
	}
	if(inn[1]<d){
		puts("NO");
		return 0;
	}
	puts("YES");
	for(int i=1;i<=n;++i)fa[i]=i;
	int cnt=0;
	for(int i=2;i<=n;++i){
		if(!fl[i]){
			dfs(i,++cnt);
		}
	}
	int fr=d-cnt;

	for(int i=hed[1];~i;i=edge[i].nxt){
		int v=edge[i].v;
		if(!met[fl[v]]){
			if(f(edge[i].u)==f(edge[i].v))continue;
			met[fl[v]]=1;
			uni(edge[i].u,edge[i].v);
			cho[i]=1;
		}else if(fr){
			if(f(edge[i].u)==f(edge[i].v))continue;
			--fr;
			cho[i]=1;
			uni(edge[i].u,edge[i].v);
		}
	}

	for(int i=1;i<=ecnt;++i){
		if(f(edge[i].u)!=f(edge[i].v)&&cho[i]==0&&edge[i].u!=1&&edge[i].v!=1){
			uni(edge[i].u,edge[i].v);
			cho[i]=1;
		}
	}
	for(int i=1;i<=ecnt;++i){
		if(cho[i]){
			printf("%d %d\n",edge[i].u,edge[i].v);
		}
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
vector < int > g[200001];
int vis[200001];
int vis2[200001];
int low[200001];
int dfn[200001];
int cnt[200001];
int t=1;

map< pair<int,int> ,int> h;
vector <pair<int,int> > ans;

void dfs(int v,int prev){
    low[v]=t;
    dfn[v]=t;
    t++;
    for(auto i:g[v]){
        if(i!=prev){
            if(vis[i]==0){
                vis[i]=1;
                dfs(i,v);
                low[v]=min(low[v],low[i]);
                if(low[i]>dfn[v]){
                    h[{min(i,v),max(i,v)}]=1;
                    cnt[i]++;
                    cnt[v]++;
                }
            }
            else{
                low[v]=min(low[v],low[i]);
            }
        }
    }
}

int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n,m,d;
    cin >> n >> m >> d;
    for(int i=0;i<m;i++){
        int x,y;
        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }

    dfs(1,-1);

    int fff=0;
    for(int i=1;i<=1;i++){
        if(g[i].size()>=d && cnt[i]<=d){
            int zz=0;
            fff=1;
            vis2[i]=1;
            queue <int> q;
            for(int j=0;j<g[i].size();j++){
                if(h[{min(i,g[i][j]),max(i,g[i][j])}]==1){
                    vis2[g[i][j]]=1;
                    q.push(g[i][j]);
                    ans.push_back({i,g[i][j]});
                    zz++;
                }
            }
            for(int j=0;j<g[i].size() && zz<d;j++){
                if(h[{min(i,g[i][j]),max(i,g[i][j])}]==0){
                    q.push(g[i][j]);
                    zz++;
                    vis2[g[i][j]]=1;
                    ans.push_back({i,g[i][j]});
                }
            }
            while(!q.empty()){
                int x = q.front();
                q.pop();
                for(auto w:g[x]){
                    if(vis2[w]==0){
                        vis2[w]=1;
                        q.push(w);
                        ans.push_back({x,w});
                    }
                }
            }
            break;
        }
    }

    if(fff==0){
        cout << "NO";
        return 0;
    }
    cout << "YES" << endl;
    for(auto i:ans){
        cout << i.first << " " << i.second << endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
vector < int > g[200001];
int vis[200001];
int vis2[200001];
int low[200001];
int dfn[200001];
int cnt[200001];
int t=1;

map< pair<int,int> ,int> h;
vector <pair<int,int> > ans;

void dfs(int v,int prev){
    low[v]=t;
    dfn[v]=t;
    t++;
    for(auto i:g[v]){
        if(i!=prev){
            if(vis[i]==0){
                vis[i]=1;
                dfs(i,v);
                low[v]=min(low[v],low[i]);
                if(low[i]>dfn[v]){
                    h[{min(i,v),max(i,v)}]=1;
                    cnt[i]++;
                    cnt[v]++;
                }
            }
            else{
                low[v]=min(low[v],low[i]);
            }
        }
    }
}

int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n,m,d;
    cin >> n >> m >> d;
    for(int i=0;i<m;i++){
        int x,y;
        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }

    dfs(2,-1);

    int fff=0;
    for(int i=1;i<=1;i++){
        if(g[i].size()>=d && cnt[i]<=d){
            int zz=0;
            fff=1;
            vis2[i]=1;
            queue <int> q;
            for(int j=0;j<g[i].size();j++){
                if(h[{min(i,g[i][j]),max(i,g[i][j])}]==1){
                    vis2[g[i][j]]=1;
                    q.push(g[i][j]);
                    ans.push_back({i,g[i][j]});
                    zz++;
                }
            }
            for(int j=0;j<g[i].size() && zz<d;j++){
                if(h[{min(i,g[i][j]),max(i,g[i][j])}]!=1){
                    q.push(g[i][j]);
                    zz++;
                    vis2[g[i][j]]=1;
                    ans.push_back({i,g[i][j]});
                }
            }
            while(!q.empty()){
                int x = q.front();
                q.pop();
                for(auto w:g[x]){
                    if(vis2[w]==0){
                        vis2[w]=1;
                        q.push(w);
                        ans.push_back({x,w});
                    }
                }
            }
            break;
        }
    }

    if(fff==0){
        cout << "NO";
        return 0;
    }
    cout << "YES" << endl;
    for(auto i:ans){
        cout << i.first << " " << i.second << endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
vector < int > g[200001];
int vis[200001];
int vis2[200001];
int low[200001];
int dfn[200001];
int cnt[200001];
int t=1;

map< pair<int,int> ,int> h;
vector <pair<int,int> > ans;

void dfs(int v,int prev){
    low[v]=t;
    dfn[v]=t;
    t++;
    for(auto i:g[v]){
        if(i!=prev){
            if(vis[i]==0){
                vis[i]=1;
                dfs(i,v);
                low[v]=min(low[v],low[i]);
                if(low[i]>dfn[v]){
                    h[{min(i,v),max(i,v)}]=1;
                    cnt[i]++;
                    cnt[v]++;
                }
            }
            else{
                low[v]=min(low[v],low[i]);
            }
        }
    }
}

int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n,m,d;
    cin >> n >> m >> d;
    for(int i=0;i<m;i++){
        int x,y;
        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }

    dfs(2,-1);

    int fff=0;
    for(int i=1;i<=1;i++){
        if(g[i].size()>=d && cnt[i]<=d){
            int zz=0;
            fff=1;
            vis2[i]=1;
            queue <int> q;
            for(int j=0;j<g[i].size();j++){
                if(h[{min(i,g[i][j]),max(i,g[i][j])}]==1){
                    vis2[g[i][j]]=1;
                    q.push(g[i][j]);
                    ans.push_back({i,g[i][j]});
                    zz++;
                }
            }
            for(int j=0;j<g[i].size() && zz<d;j++){
                if(h[{min(i,g[i][j]),max(i,g[i][j])}]==0){
                    q.push(g[i][j]);
                    zz++;
                    vis2[g[i][j]]=1;
                    ans.push_back({i,g[i][j]});
                }
            }
            while(!q.empty()){
                int x = q.front();
                q.pop();
                for(auto w:g[x]){
                    if(vis2[w]==0){
                        vis2[w]=1;
                        q.push(w);
                        ans.push_back({x,w});
                    }
                }
            }
            break;
        }
    }

    if(fff==0){
        cout << "NO";
        return 0;
    }
    cout << "YES" << endl;
    for(auto i:ans){
        cout << i.first << " " << i.second << endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
vector < int > g[200001];
int vis[200001];
int vis2[200001];
int low[200001];
int dfn[200001];
int cnt[200001];
int t=1;
map< pair<int,int> ,int> h;
vector <pair<int,int> > ans;
void dfs(int v,int prev){
    low[v]=t;
    dfn[v]=t;
    t++;
    for(auto i:g[v]){
        if(i!=prev){
            if(vis[i]==0){
                vis[i]=1;
                dfs(i,v);
                low[v]=min(low[v],low[i]);
                if(low[i]>dfn[v]){
                    h[{min(i,v),max(i,v)}]=1;
                    cnt[i]++;
                    cnt[v]++;
                }
            }
            else{
                low[v]=min(low[v],low[i]);
            }
        }
    }
}

int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n,m,d;
    cin >> n >> m >> d;
    for(int i=0;i<m;i++){
        int x,y;
        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }

    dfs(1,-1);

    queue <int> q;
    int fff=0;
    for(int i=1;i<=n;i++){
        if(g[i].size()>=d && cnt[i]<=d){
            int zz=0;
            fff=1;
            vis2[i]=1;
            for(int j=0;j<g[i].size();j++){
                if(h[{min(i,g[i][j]),max(i,g[i][j])}]==1){
                    vis2[g[i][j]]=1;
                    q.push(g[i][j]);
                    ans.push_back({i,g[i][j]});
                    zz++;
                }
            }
            for(int j=0;j<g[i].size() && zz<d;j++){
                if(h[{min(i,g[i][j]),max(i,g[i][j])}]==0){
                    q.push(g[i][j]);
                    zz++;
                    vis2[g[i][j]]=1;
                    ans.push_back({i,g[i][j]});
                }
            }
            while(!q.empty()){
                int x = q.front();
                q.pop();
                for(auto w:g[x]){
                    if(vis2[w]==0){
                        vis2[w]=1;
                        q.push(w);
                        ans.push_back({x,w});
                    }
                }
            }
            break;
        }
    }

    // while(!q.empty()){
    //     int y = q.front();
    //     q.pop();
    //     for(auto i:g[y]){
    //         if(vis[i]==0){
    //             vis[i]=1;
    //             q.push(i);
    //             ans.push_back({y,i});
    //         }
    //     }
    // }
    if(fff==0){
        cout << "NO";
        return 0;
    }
    cout << "YES" << endl;
    for(auto i:ans){
        cout << i.first << " " << i.second << endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
vector <int> g[200001];
vector <pair<int,int> > edges;
int vis[200001];
int n,m,d;
vector <int> conn;
int t=0;

void dfs(int u){
    vis[u]=t;
    for(auto i:g[u]){
        if(i!=1 && vis[i]==0){
            dfs(i);
        }
    }
}

void dfs2(int u){
    for(auto i:g[u]){
        if(i!=1 && !vis[i]){
            edges.push_back({u,i});
            vis[i]=1;
            dfs2(i);
        }
    }
}

int main(){
    cin >> n >> m >> d;
    for(int i=0;i<m;i++){
        int x,y;
        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    for(auto i:g[1]){
        if(!vis[i]){
            t++;
            conn.push_back(i);
            dfs(i);
        }
    }
    if(t>d || d>g[1].size()){
        cout << "NO";
        return 0;
    }
    for(int i=1;i<=n;i++){
        vis[i]=0;
    }
    queue <int> q;
    map <int,int> h;
    vis[1]=1;
    for(int i=0;i<t;i++){
        q.push(conn[i]);
        h[conn[i]]=1;
        vis[conn[i]]=1;
        edges.push_back({1,conn[i]});
    }
    d = d-t;
    for(auto i:g[1]){
        if(d==0){
            break;
        }
        if(h[i]){
            continue;
        }
        q.push(i);
        vis[i]=1;
        edges.push_back({1,i});
        d--;
    }
    while(!q.empty()){
        int x = q.front();
        dfs2(x);
        q.pop();
    }
    cout << "YES" << endl;
    for(auto i:edges){
        cout << i.first << " " << i.second << endl;
    }

}


#include<bits/stdc++.h>
using namespace std;
int in[234567]={},p[234567];
int find(int x){
	return x==p[x]?x:p[x]=find(p[x]);
}
void unite(int u,int v){
	p[find(u)]=find(v);
}
#define pii pair<int,int>
pii edge[234567];
int main(){
	ios::sync_with_stdio();
	cin.tie(0);
	for(int i=0;i<234567;i++) p[i]=i;
	int cnt=0,n,m,D;cin>>n>>m>>D;
	vector<pii> ans;
	for(int i=0;i<m;i++){
		int u,v;cin>>u>>v;
		edge[i]=make_pair(u,v);
		in[u]++;
		in[v]++;
		if((u==1 || v==1) && cnt<D) ans.push_back(edge[i]),cnt++;
	}
	if(in[1]<D) cout<<"NO\n";
	else{
		cout<<"YES\n";
		for(int i=0;i<m && cnt<n-1;i++){
			int u=edge[i].first,v=edge[i].second;
			if((u!=1 && v!=1) && find(u)!=find(v)){
				unite(u,v);
				ans.push_back(edge[i]);
				cnt++;
			}
		}
		for(int i=0;i<cnt;i++) cout<<ans[i].first<<' '<<ans[i].second<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
int p[234567];
vector<int> G[234567],G1;
int find(int x){
	return x==p[x]?x:p[x]=find(p[x]);
}
void unite(int u,int v){
	p[find(u)]=find(v);
}
#define pii pair<int,int>
bool visit[234567]={};
vector<pii> ans,edge;
int main(){
	ios::sync_with_stdio();
	cin.tie(0);
	for(int i=0;i<234567;i++) p[i]=i;
	int n,m,D;cin>>n>>m>>D;
	int in=0;
	for(int i=0;i<m;i++){
		int u,v;cin>>u>>v;
		if(u!=1 && v!=1){
			G[u].push_back(v);
			G[v].push_back(u);
			edge.push_back(make_pair(u,v));
			if(find(u)!=find(v)) unite(u,v);
		}else G1.push_back(u==1?v:u),in++;
	}
	int cnt1=0;map<int,bool> mp;
	for(int i=2;i<=n;i++){
		int u=find(i);
		if(!mp[u]) cnt1++;
		mp[u]=true;
	}
	mp.clear();
	int cnt2=0;
	for(int i=0;i<G1.size();i++){
		int u=find(G1[i]);
		if(!mp[u]) cnt2++;
		mp[u]=true;
	}
	if(D>in || D<cnt1 || cnt1>cnt2) cout<<"NO\n";
	else{
		int cnt=0;
		bool chosen[234567]={};
		for(int i=0;i<G1.size();i++){
			int u=find(G1[i]);
			if(!mp[u]){
				visit[G1[i]]=true;
				++cnt,chosen[i]=true,ans.push_back(make_pair(1,G1[i]));
			}
		}
		for(int i=0;i<G1.size() && cnt<D;i++){
			if(!chosen[i]){
				visit[G1[i]]=true;
				++cnt,ans.push_back(make_pair(1,G1[i]));
			}
		}
		for(int i=0;i<edge.size();i++){
			int u=edge[i].first,v=edge[i].second;
			if(!visit[u] || !visit[v]){
				visit[u]=true;visit[v]=true;
				ans.push_back(edge[i]);
			}
		}
//		assert(ans.size()==n-1);
		cout<<"YES\n";
		for(int i=0;i<ans.size();i++) cout<<ans[i].first<<' '<<ans[i].second<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
int p[234567];
vector<int> G[234567],G1;
int find(int x){
	return x==p[x]?x:p[x]=find(p[x]);
}
void unite(int u,int v){
	p[find(u)]=find(v);
}
#define pii pair<int,int>
bool visit[234567]={};
vector<pii> ans;
void dfs(int cur){
	visit[cur]=true;
	for(int nxt:G[cur]){
		if(visit[nxt]) continue;
		ans.push_back(make_pair(cur,nxt));
		dfs(nxt);
	}
}
int main(){
	ios::sync_with_stdio();
	cin.tie(0);
	for(int i=0;i<234567;i++) p[i]=i;
	int n,m,D;cin>>n>>m>>D;
	int in=0;
	for(int i=0;i<m;i++){
		int u,v;cin>>u>>v;
		if(u!=1 && v!=1){
			G[u].push_back(v);
			G[v].push_back(u);
			if(find(u)!=find(v)) unite(u,v);
		}else G1.push_back(u==1?v:u),in++;
	}
	int cnt1=0;map<int,bool> mp;
	for(int i=2;i<=n;i++){
		int u=find(i);
		if(!mp[u]) cnt1++;
		mp[u]=true;
	}
	mp.clear();
	int cnt2=0;
	for(int i=0;i<G1.size();i++){
		int u=find(G1[i]);
		if(!mp[u]) cnt2++;
		mp[u]=true;
	}
	if(D>in || D<cnt1 || cnt1>cnt2) cout<<"NO\n";
	else{
		int cnt=0;
		bool chosen[234567]={};
		for(int i=0;i<G1.size();i++){
			int u=find(G1[i]);
			if(!mp[u]){
				visit[G1[i]]=true;
				++cnt,chosen[i]=true,ans.push_back(make_pair(1,G1[i]));
			}
		}
		for(int i=0;i<G1.size() && cnt<D;i++){
			if(!chosen[i]){
				visit[G1[i]]=true;
				++cnt,ans.push_back(make_pair(1,G1[i]));
			}
		}
		for(int i=2;i<=n;i++) dfs(i);
		assert(ans.size()==n-1);
		cout<<"YES\n";
		for(int i=0;i<n-1;i++) cout<<ans[i].first<<' '<<ans[i].second<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
int p[234567];
vector<int> G[234567],G1;
int find(int x){
	return x==p[x]?x:p[x]=find(p[x]);
}
void unite(int u,int v){
	p[find(u)]=find(v);
}
#define pii pair<int,int>
bool visit[234567]={};
vector<pii> ans,edge;
int main(){
	ios::sync_with_stdio();
	cin.tie(0);
	for(int i=0;i<234567;i++) p[i]=i;
	int n,m,D;cin>>n>>m>>D;
	int in=0;
	for(int i=0;i<m;i++){
		int u,v;cin>>u>>v;
		if(u!=1 && v!=1){
			G[u].push_back(v);
			G[v].push_back(u);
			edge.push_back(make_pair(u,v));
			if(find(u)!=find(v)) unite(u,v);
		}else G1.push_back(u==1?v:u),in++;
	}
	int cnt1=0;map<int,bool> mp;
	for(int i=2;i<=n;i++){
		int u=find(i);
		if(!mp[u]) cnt1++;
		mp[u]=true;
	}
	mp.clear();
	int cnt2=0;
	for(int i=0;i<G1.size();i++){
		int u=find(G1[i]);
		if(!mp[u]) cnt2++;
		mp[u]=true;
	}
	if(D>in || D<cnt1 || cnt1>cnt2) cout<<"NO\n";
	else{
		int cnt=0;
		bool chosen[234567]={};
		for(int i=0;i<G1.size();i++){
			int u=find(G1[i]);
			if(!mp[u]){
				visit[G1[i]]=true;
				++cnt,chosen[i]=true,ans.push_back(make_pair(1,G1[i]));
			}
		}
		for(int i=0;i<G1.size() && cnt<D;i++){
			if(!chosen[i]){
				visit[G1[i]]=true;
				++cnt,ans.push_back(make_pair(1,G1[i]));
			}
		}
		for(int i=0;i<edge.size();i++){
			int u=edge[i].first,v=edge[i].second;
			if(!visit[u] || !visit[v])
				ans.push_back(edge[i]);
		}
//		assert(ans.size()==n-1);
		cout<<"YES\n";
		for(int i=0;i<ans.size();i++) cout<<ans[i].first<<' '<<ans[i].second<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
int p[234567];
vector<int> G[234567],G1;
int find(int x){
	return x==p[x]?x:p[x]=find(p[x]);
}
void unite(int u,int v){
	p[find(u)]=find(v);
}
#define pii pair<int,int>
vector<pii> ans,edge;
int main(){
	ios::sync_with_stdio();
	cin.tie(0);
	for(int i=0;i<234567;i++) p[i]=i;
	int n,m,D;cin>>n>>m>>D;
	int in=0;
	for(int i=0;i<m;i++){
		int u,v;cin>>u>>v;
		if(u!=1 && v!=1){
			G[u].push_back(v);
			G[v].push_back(u);
			edge.push_back(make_pair(u,v));
			if(find(u)!=find(v)) unite(u,v);
		}else G1.push_back(u==1?v:u),in++;
	}
	int cnt1=0;map<int,bool> mp;
	for(int i=2;i<=n;i++){
		int u=find(i);
		if(!mp[u]) cnt1++;
		mp[u]=true;
	}
	mp.clear();
	int cnt2=0;
	for(int i=0;i<G1.size();i++){
		int u=find(G1[i]);
		if(!mp[u]) cnt2++;
		mp[u]=true;
	}
	if(D>in || D<cnt1 || cnt1>cnt2) cout<<"NO\n";
	else{
		int cnt=0;
		bool chosen[234567]={};
		for(int i=0;i<G1.size();i++){
			int u=find(G1[i]);
			if(!mp[u]){
				chosen[i]=true;
				++cnt,ans.push_back(make_pair(1,G1[i]));
			}
		}
		for(int i=0;i<G1.size() && cnt<D;i++){
			if(!chosen[i])
				++cnt,ans.push_back(make_pair(1,G1[i]));
		}
		for(int i=0;i<=n;i++) p[i]=i;
		for(int i=0;i<ans.size();i++) unite(1,ans[i].second);
		for(int i=0;i<edge.size();i++){
			int u=edge[i].first,v=edge[i].second;
			if(find(u)!=find(v)){
				unite(u,v);
				ans.push_back(edge[i]);
			}
		}
//		assert(ans.size()==n-1);
		cout<<"YES\n";
		for(int i=0;i<ans.size();i++) cout<<ans[i].first<<' '<<ans[i].second<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
int in[234567]={},p[234567];
int find(int x){
	return x==p[x]?x:p[x]=find(p[x]);
}
void unite(int u,int v){
	p[find(u)]=find(v);
}
#define pii pair<int,int>
pii edge[234567];
int main(){
	ios::sync_with_stdio();
	cin.tie(0);
	for(int i=0;i<234567;i++) p[i]=i;
	int cnt=0,n,m,D;cin>>n>>m>>D;
	vector<pii> ans;
	for(int i=0;i<m;i++){
		int u,v;cin>>u>>v;
		edge[i]=make_pair(u,v);
		in[u]++;
		in[v]++;
		if((u==1 || v==1) && cnt<D) ans.push_back(edge[i]),unite(u,v),cnt++;
	}
	if(in[1]<D || m<n-1) cout<<"NO\n";
	else{
		for(int i=0;i<m && cnt<n-1;i++){
			int u=edge[i].first,v=edge[i].second;
			if((u!=1 && v!=1) && find(u)!=find(v)){
				unite(u,v);
				ans.push_back(edge[i]);
				cnt++;
			}
		}
		if(cnt==n-1){
			cout<<"YES\n";
			for(int i=0;i<cnt;i++) cout<<ans[i].first<<' '<<ans[i].second<<endl;
		}else cout<<"NO\n";
	}
}


#include<bits/stdc++.h>
using namespace std;
int p[234567];
vector<int> G[234567],G1;
int find(int x){
	return x==p[x]?x:p[x]=find(p[x]);
}
void unite(int u,int v){
	p[find(u)]=find(v);
}
#define pii pair<int,int>
vector<pii> ans,edge;
int main(){
	ios::sync_with_stdio();
	cin.tie(0);
	for(int i=0;i<234567;i++) p[i]=i;
	int n,m,D;cin>>n>>m>>D;
	int in=0;
	for(int i=0;i<m;i++){
		int u,v;cin>>u>>v;
		if(u!=1 && v!=1){
			G[u].push_back(v);
			G[v].push_back(u);
			edge.push_back(make_pair(u,v));
			if(find(u)!=find(v)) unite(u,v);
		}else G1.push_back(u==1?v:u),in++;
	}
	int cnt1=0;map<int,bool> mp;
	for(int i=2;i<=n;i++){
		int u=find(i);
		if(!mp[u]) cnt1++;
		mp[u]=true;
	}
	mp.clear();
	int cnt2=0;
	for(int i=0;i<G1.size();i++){
		int u=find(G1[i]);
		if(!mp[u]) cnt2++;
		mp[u]=true;
	}
	if(D>in || D<cnt1 || cnt1>cnt2) cout<<"NO\n";
	else{
		int cnt=0;
		bool chosen[234567]={};
		mp.clear();
		for(int i=0;i<G1.size();i++){
			int u=find(G1[i]);
			if(!mp[u]){
				mp[u]=true;
				chosen[i]=true;
				++cnt,ans.push_back(make_pair(1,G1[i]));
			}
		}
		for(int i=0;i<G1.size() && cnt<D;i++){
			if(!chosen[i])
				++cnt,ans.push_back(make_pair(1,G1[i]));
		}
		for(int i=0;i<=n;i++) p[i]=i;
		for(int i=0;i<ans.size();i++) unite(1,ans[i].second);
		for(int i=0;i<edge.size();i++){
			int u=edge[i].first,v=edge[i].second;
			if(find(u)!=find(v)){
				unite(u,v);
				ans.push_back(edge[i]);
			}
		}
//		assert(ans.size()==n-1);
		cout<<"YES\n";
		for(int i=0;i<ans.size();i++) cout<<ans[i].first<<' '<<ans[i].second<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
int p[234567];
vector<int> G[234567],G1;
int find(int x){
	return x==p[x]?x:p[x]=find(p[x]);
}
void unite(int u,int v){
	p[find(u)]=find(v);
}
#define pii pair<int,int>
vector<pii> ans,edge;
int main(){
	ios::sync_with_stdio();
	cin.tie(0);
	for(int i=0;i<234567;i++) p[i]=i;
	int n,m,D;cin>>n>>m>>D;
	int in=0;
	for(int i=0;i<m;i++){
		int u,v;cin>>u>>v;
		if(u!=1 && v!=1){
			G[u].push_back(v);
			G[v].push_back(u);
			edge.push_back(make_pair(u,v));
			if(find(u)!=find(v)) unite(u,v);
		}else G1.push_back(u==1?v:u),in++;
	}
	int cnt1=0;map<int,bool> mp;
	for(int i=2;i<=n;i++){
		int u=find(i);
		if(!mp[u]) cnt1++;
		mp[u]=true;
	}
	mp.clear();
	int cnt2=0;
	for(int i=0;i<G1.size();i++){
		int u=find(G1[i]);
		if(!mp[u]) cnt2++;
		mp[u]=true;
	}
	if(D>in || D<cnt1 || cnt1>cnt2) cout<<"NO\n";
	else{
		int cnt=0;
		bool chosen[234567]={};
		for(int i=0;i<=n;i++) p[i]=i;
		for(int i=0;i<G1.size();i++){
			int u=find(G1[i]);
			if(!mp[u]){
				chosen[i]=true;
				++cnt,ans.push_back(make_pair(1,G1[i]));
				unite(1,G1[i]);
			}
		}
		for(int i=0;i<G1.size() && cnt<D;i++){
			if(!chosen[i]){
				++cnt,ans.push_back(make_pair(1,G1[i]));
				unite(1,G1[i]);
			}
		}
		for(int i=0;i<edge.size();i++){
			int u=edge[i].first,v=edge[i].second;
			if(find(u)!=find(v)){
				unite(u,v);
				ans.push_back(edge[i]);
			}
		}
//		assert(ans.size()==n-1);
		cout<<"YES\n";
		for(int i=0;i<ans.size();i++) cout<<ans[i].first<<' '<<ans[i].second<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
int in[234567]={},p[234567];
int find(int x){
	return x==p[x]?x:p[x]=find(p[x]);
}
void unite(int u,int v){
	p[find(u)]=find(v);
}
#define pii pair<int,int>
pii edge[234567];
int main(){
	ios::sync_with_stdio();
	cin.tie(0);
	for(int i=0;i<234567;i++) p[i]=i;
	int cnt=0,n,m,D;cin>>n>>m>>D;
	vector<pii> ans;
	for(int i=0;i<m;i++){
		int u,v;cin>>u>>v;
		edge[i]=make_pair(u,v);
		in[u]++;
		in[v]++;
		if((u==1 || v==1) && cnt<D) ans.push_back(edge[i]),cnt++;
	}
	if(in[1]<D || m<n-1) cout<<"NO\n";
	else{
		for(int i=0;i<m && cnt<n-1;i++){
			int u=edge[i].first,v=edge[i].second;
			if((u!=1 && v!=1) && find(u)!=find(v)){
				unite(u,v);
				ans.push_back(edge[i]);
				cnt++;
			}
		}
		if(cnt==n-1){
			cout<<"YES\n";
			for(int i=0;i<cnt;i++) cout<<ans[i].first<<' '<<ans[i].second<<endl;
		}else cout<<"NO\n";
	}
}


#include <bits/stdc++.h>

using namespace std;

#define fo(i,j,n) for(i=j;i<n;++i)
#define Fo(i,j,n) for(i=n-1;i>=j;--i)
#define foo(i,j,v) fo(i,j,sz(v))
#define Foo(i,j,v) Fo(i,j,sz(v))
#define li(v) v.begin(),v.end()
#define sz(v) ((int)v.size())
#define CLR(a,v) memset((a),(v),sizeof(a))

#define pi (2*acos(0.0))
#define eps (1e-6)

typedef long long Long;

const int imax = 1e9+7;
const Long lmax = 1e18;

const int maxn = 2e5 + 10;
vector<int> v;
string s;
vector<int> adj[maxn],deg,v1,v2;
vector<pair<int,int> > ans;

struct UnionFind{
    vector<int> parent,size;
    UnionFind(int n){
        int i;
        parent.resize(n);
        size.resize(n);
        for(i = 0; i < n; i++) { parent[i] = i; size[i] = 1; }
    }

    int root(int x){
        if(parent[x] != x) parent[x] = root(parent[x]);
        return parent[x];
    }

    void connect(int x, int y){
        int rx = root(x), ry = root(y);
        if(rx == ry) return;
        if(size[rx] > size[ry])  { parent[ry] = rx; size[rx] += size[ry]; }
        if(size[rx] <= size[ry]) { parent[rx] = ry; size[ry] += size[rx]; }
    }

    int getsize(int x){
        int rx = root(x);
        return size[rx];
    }
};

int calc()
{
    int i,j,a,b,n,m,k = 0,d; cin >> n >> m >> d; deg.clear(); deg.resize(n); v1.resize(m); v2.resize(m);
    fo(i,0,maxn)adj[i].clear();
    fo(i,0,m)
    {
        cin >> a >> b; a--; b--;
        adj[a].push_back(b);
        adj[b].push_back(a);
        deg[a]++; deg[b]++;
        v1[i] = a; v2[i] = b;
    }
    ans.clear();
    UnionFind uf(n);
    if(adj[k].size() < d)return 0;
    foo(i,0,adj[k])
    {
        j = adj[k][i];
        ans.push_back({k,j});
        uf.connect(k,j);
        d--; if(d == 0)break;
    }
    fo(i,0,m)
    {
        a = v1[i]; b = v2[i];
        if(a == 0 || b == 0)continue;
        if(uf.root(a) == uf.root(b))continue;
        ans.push_back({a,b});
        uf.connect(a,b);
    }
    cout << "YES" << endl;
    foo(i,0,ans)
    {
        cout << ans[i].first + 1 << " " << ans[i].second + 1 << endl;
    }
	return 1;
}

int main()
{
	ios::sync_with_stdio(false);
    cin.tie(0);
	#ifdef localhost
	freopen("E://input.txt","r",stdin);
	//freopen("E://output.txt","w",stdout);
	#endif

	if(calc() == 0)cout << "NO" << endl;

	//cout << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define fo(i,j,n) for(i=j;i<n;++i)
#define Fo(i,j,n) for(i=n-1;i>=j;--i)
#define foo(i,j,v) fo(i,j,sz(v))
#define Foo(i,j,v) Fo(i,j,sz(v))
#define li(v) v.begin(),v.end()
#define sz(v) ((int)v.size())
#define CLR(a,v) memset((a),(v),sizeof(a))

#define pi (2*acos(0.0))
#define eps (1e-6)

typedef long long Long;

const int imax = 1e9+7;
const Long lmax = 1e18;

vector<int> v1,v2;
vector<pair<int,int> > ans;

struct UnionFind{
    vector<int> parent,size;
    UnionFind(int n){
        int i;
        parent.resize(n);
        size.resize(n);
        for(i = 0; i < n; i++) { parent[i] = i; size[i] = 1; }
    }

    int root(int x){
        if(parent[x] != x) parent[x] = root(parent[x]);
        return parent[x];
    }

    void connect(int x, int y){
        int rx = root(x), ry = root(y);
        if(rx == ry) return;
        if(size[rx] > size[ry])  { parent[ry] = rx; size[rx] += size[ry]; }
        if(size[rx] <= size[ry]) { parent[rx] = ry; size[ry] += size[rx]; }
    }

    int getsize(int x){
        int rx = root(x);
        return size[rx];
    }
};

int calc()
{
    int i,a,b,n,m,k = 0,d; cin >> n >> m >> d;
    v1.resize(m); v2.resize(m); ans.clear();
    fo(i,0,m){ cin >> v1[i] >> v2[i]; v1[i]--; v2[i]--; }

    UnionFind uf(n);
    fo(i,0,m)
    {
        a = v1[i]; b = v2[i];
        if(a == 0 || b == 0)continue;
        if(uf.root(a) == uf.root(b))continue;
        ans.push_back({a,b});
        uf.connect(a,b);
    }
    fo(i,1,n)if(uf.root(i) == i)k++;
    if(k > d)return 0;
    fo(i,0,m)
    {
        a = v1[i]; b = v2[i];
        if(a != 0 && b != 0)continue;
        if(uf.root(a) == uf.root(b))continue;
        ans.push_back({a,b});
        uf.connect(a,b);
        v1[i] = v2[i] = 1;
    }
    k = d - k;
    fo(i,0,m)
    {
        if(k == 0)break;
        a = v1[i]; b = v2[i];
        if(a != 0 && b != 0)continue;
        k--;
        //if(uf.root(a) == uf.root(b))continue;
        ans.push_back({a,b});
        uf.connect(a,b);
        //v1[i] = v2[i] = 1;
    }
    m = ans.size(); v1.resize(m); v2.resize(m);
    foo(i,0,ans)v1[i] = ans[i].first, v2[i] = ans[i].second;
    uf = UnionFind(n); ans.clear();
    fo(i,0,m)
    {
        a = v1[i]; b = v2[i];
        if(a != 0 && b != 0)continue;
        if(uf.root(a) == uf.root(b))continue;
        ans.push_back({a,b});
        uf.connect(a,b);
        d--;
    }
    if(d != 0)return 0;
    fo(i,0,m)
    {
        a = v1[i]; b = v2[i];
        if(uf.root(a) == uf.root(b))continue;
        ans.push_back({a,b});
        uf.connect(a,b);
    }
    if(uf.getsize(0) != n)return 0;
    cout << "YES" << endl;
    foo(i,0,ans)
    {
        cout << ans[i].first + 1 << " " << ans[i].second + 1 << endl;
    }
	return 1;
}

int main()
{
	//ios::sync_with_stdio(false);
    //cin.tie(0);
	#ifdef localhost
	freopen("E://input.txt","r",stdin);
	//freopen("E://output.txt","w",stdout);
	#endif

	if(calc() == 0)cout << "NO" << endl;

	//cout << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define fo(i,j,n) for(i=j;i<n;++i)
#define Fo(i,j,n) for(i=n-1;i>=j;--i)
#define foo(i,j,v) fo(i,j,sz(v))
#define Foo(i,j,v) Fo(i,j,sz(v))
#define li(v) v.begin(),v.end()
#define sz(v) ((int)v.size())
#define CLR(a,v) memset((a),(v),sizeof(a))

#define pi (2*acos(0.0))
#define eps (1e-6)

typedef long long Long;

const int imax = 1e9+7;
const Long lmax = 1e18;

const int maxn = 2e5 + 10;
vector<int> v;
string s;
vector<int> adj[maxn],deg,v1,v2;
vector<pair<int,int> > ans;

struct UnionFind{
    vector<int> parent,size;
    UnionFind(int n){
        int i;
        parent.resize(n);
        size.resize(n);
        for(i = 0; i < n; i++) { parent[i] = i; size[i] = 1; }
    }

    int root(int x){
        if(parent[x] != x) parent[x] = root(parent[x]);
        return parent[x];
    }

    void connect(int x, int y){
        int rx = root(x), ry = root(y);
        if(rx == ry) return;
        if(size[rx] > size[ry])  { parent[ry] = rx; size[rx] += size[ry]; }
        if(size[rx] <= size[ry]) { parent[rx] = ry; size[ry] += size[rx]; }
    }

    int getsize(int x){
        int rx = root(x);
        return size[rx];
    }
};

int calc()
{
    int i,j,a,b,n,m,k = 0,d; cin >> n >> m >> d; deg.clear(); deg.resize(n); v1.resize(m); v2.resize(m);
    fo(i,0,maxn)adj[i].clear();
    fo(i,0,m)
    {
        cin >> a >> b; a--; b--;
        adj[a].push_back(b);
        adj[b].push_back(a);
        deg[a]++; deg[b]++;
        v1[i] = a; v2[i] = b;
    }
    ans.clear();
    UnionFind uf(n);
    if(adj[k].size() < d)return 0;
    foo(i,0,adj[k])
    {
        j = adj[k][i];
        ans.push_back({k,j});
        uf.connect(k,j);
        d--; if(d == 0)break;
    }
    fo(i,0,m)
    {
        a = v1[i]; b = v2[i];
        if(uf.root(a) == uf.root(b))continue;
        ans.push_back({a,b});
        uf.connect(a,b);
    }
    cout << "YES" << endl;
    foo(i,0,ans)
    {
        cout << ans[i].first + 1 << " " << ans[i].second + 1 << endl;
    }
	return 1;
}

int main()
{
	ios::sync_with_stdio(false);
    cin.tie(0);
	#ifdef localhost
	freopen("E://input.txt","r",stdin);
	//freopen("E://output.txt","w",stdout);
	#endif

	if(calc() == 0)cout << "NO" << endl;

	//cout << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define fo(i,j,n) for(i=j;i<n;++i)
#define Fo(i,j,n) for(i=n-1;i>=j;--i)
#define foo(i,j,v) fo(i,j,sz(v))
#define Foo(i,j,v) Fo(i,j,sz(v))
#define li(v) v.begin(),v.end()
#define sz(v) ((int)v.size())
#define CLR(a,v) memset((a),(v),sizeof(a))

#define pi (2*acos(0.0))
#define eps (1e-6)

typedef long long Long;

const int imax = 1e9+7;
const Long lmax = 1e18;

vector<int> v1,v2;
vector<pair<int,int> > ans;

struct UnionFind{
    vector<int> parent,size;
    UnionFind(int n){
        int i;
        parent.resize(n);
        size.resize(n);
        for(i = 0; i < n; i++) { parent[i] = i; size[i] = 1; }
    }

    int root(int x){
        if(parent[x] != x) parent[x] = root(parent[x]);
        return parent[x];
    }

    void connect(int x, int y){
        int rx = root(x), ry = root(y);
        if(rx == ry) return;
        if(size[rx] > size[ry])  { parent[ry] = rx; size[rx] += size[ry]; }
        if(size[rx] <= size[ry]) { parent[rx] = ry; size[ry] += size[rx]; }
    }

    int getsize(int x){
        int rx = root(x);
        return size[rx];
    }
};

int calc()
{
    int i,a,b,n,m,k = 0,d; cin >> n >> m >> d;
    v1.resize(m); v2.resize(m); ans.clear();
    fo(i,0,m){ cin >> v1[i] >> v2[i]; v1[i]--; v2[i]--; }

    UnionFind uf(n);
    fo(i,0,m)
    {
        a = v1[i]; b = v2[i];
        if(a == 0 || b == 0)continue;
        if(uf.root(a) == uf.root(b))continue;
        ans.push_back({a,b});
        uf.connect(a,b);
    }
    fo(i,1,n)if(uf.root(i) == i)k++;
    if(k > d)return 0;
    fo(i,0,m)
    {
        a = v1[i]; b = v2[i];
        if(a != 0 && b != 0)continue;
        if(uf.root(a) == uf.root(b))continue;
        ans.push_back({a,b});
        uf.connect(a,b);
        v1[i] = v2[i] = 1;
    }
    d -= k;
    fo(i,0,m)
    {
        if(d == 0)break; d--;
        a = v1[i]; b = v2[i];
        if(a != 0 && b != 0)continue;
        if(uf.root(a) == uf.root(b))continue;
        ans.push_back({a,b});
        uf.connect(a,b);
        v1[i] = v2[i] = 1;
    }
    m = ans.size(); v1.resize(m); v2.resize(m);
    foo(i,0,ans)v1[i] = ans[i].first, v2[i] = ans[i].second;
    uf = UnionFind(n); ans.clear();
    fo(i,0,m)
    {
        a = v1[i]; b = v2[i];
        if(a != 0 && b != 0)continue;
        if(uf.root(a) == uf.root(b))continue;
        ans.push_back({a,b});
        uf.connect(a,b);
    }
    fo(i,0,m)
    {
        a = v1[i]; b = v2[i];
        if(uf.root(a) == uf.root(b))continue;
        ans.push_back({a,b});
        uf.connect(a,b);
    }
    if(uf.getsize(0) != n)return 0;
    cout << "YES" << endl;
    foo(i,0,ans)
    {
        cout << ans[i].first + 1 << " " << ans[i].second + 1 << endl;
    }
	return 1;
}

int main()
{
	//ios::sync_with_stdio(false);
    //cin.tie(0);
	#ifdef localhost
	freopen("E://input.txt","r",stdin);
	//freopen("E://output.txt","w",stdout);
	#endif

	if(calc() == 0)cout << "NO" << endl;

	//cout << endl;
	return 0;
}


#include <iostream>
#include <string>
#include <sstream>
#include <cmath>
#include <stdio.h>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <set>
#include <iterator>
#include <map>
#include <fstream>
#include <queue>
using namespace std;
//
#define ll long long
#define rep(i,a,n) for (ll i=a;i<n;i++)
#define repm(i,a,n) for (ll i=a;i<=n;i++)
#define syp system("pause");
//

typedef struct
{
    ll m;
    ll r;
} list;

bool rule(list a, list b)
{
    if (a.m != b.m) return a.m < b.m;
    else return a.m > b.m;
}

list a[100005];
int main()
{
    ios_base::sync_with_stdio(false);

    int n, d;
    int max = -5;
    cin >> n >> d;
    rep(i, 0, n) cin >> a[i].m >> a[i].r;
    if (n == 1)
    {
        cout << a[0].r;
        return 0;
    }
    //rep(i, 0, n) cout << a[i].m <<"  "<< a[i].r<<endl;
    rep(i, 0, n)
    {
        ll s = 0;
        //s += a[i].r;
        rep(j, 0, n)
        {
            if ((abs(a[j].m - a[i].m)) < d)
            {
                s += a[j].r;
            }
        }
        if (s > max) max = s;
    }
    cout << max;
}


#include <iostream>
#include <string>
#include <sstream>
#include <cmath>
#include <stdio.h>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <set>
#include <iterator>
#include <map>
#include <fstream>
#include <queue>
using namespace std;
//
#define ll long long
#define rep(i,a,n) for (ll i=a;i<n;i++)
#define repm(i,a,n) for (ll i=a;i<=n;i++)
//

typedef struct{
    ll m;
    ll r;
} list;

bool rule(list a, list b) { return a.m < b.m; }
list a[100005];
int main()
{
    int n, d;
    ll max = 0;
    cin >> n >> d;
    for(int i=0;i<n;i++) cin >> a[i].m >> a[i].r;
    sort(a, a + n, rule);
    for (int i = 0; i<n; i++)
    {
        ll s = 0;
        s += a[i].r;
        for (int j = i+1; j<n; j++)
        {
            if (a[j].m - a[i].m < d)
                s += a[j].r;
            else
                break;
        }
        if (s > max) max = s;
    }
    cout << max;
}


#include <iostream>
#include <string>
#include <sstream>
#include <cmath>
#include <stdio.h>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <set>
#include <iterator>
#include <map>
#include <fstream>
#include <queue>
using namespace std;
//
#define ll long long
#define rep(i,a,n) for (ll i=a;i<n;i++)
#define repm(i,a,n) for (ll i=a;i<=n;i++)
//

typedef struct{
    ll m, r;
} list;

bool rule(list a, list b) { return a.m < b.m; }
list a[100005];
int main()
{
    ll i, m = 0, n, d;
    int s = 0, j = 0;
    cin >> n >> d;
    rep(i, 0, n) cin >> a[i].m >> a[i].r;
    sort(a, a + n, rule);
    rep(i,0,n)
    {
        s = s + a[i].r;
        while (a[i].m - a[j].m >= d)
        {
            s = s - a[j].r;
            j++;
        }
        if (s > m) m = s;

    }
    cout << m;
}


#include <iostream>
#include <string>
#include <sstream>
#include <cmath>
#include <stdio.h>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <set>
#include <iterator>
#include <map>
#include <fstream>
#include <queue>
using namespace std;
//
#define ll long long
#define rep(i,a,n) for (ll i=a;i<n;i++)
#define repm(i,a,n) for (ll i=a;i<=n;i++)
//

typedef struct{
    ll m;
    ll r;
} list;

bool rule(list a, list b) { return a.m < b.m; }

int main()
{
    list a[100005];
    int n, d;
    ll max = 0;
    cin >> n >> d;
    rep(i, 0, n) cin >> a[i].m >> a[i].r;
    sort(a, a + n, rule);
    rep(i, 0, n)
    {
        ll s = 0;
        s += a[i].r;
        rep(j, i + 1, n)
        {
            if (a[j].m - a[i].m < d)
                s += a[j].r;
            else
                break;
        }
        if (s > max) max = s;
    }
    cout << max;
}


#include <iostream>
#include <string>
#include <sstream>
#include <cmath>
#include <stdio.h>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <set>
#include <iterator>
#include <map>
#include <fstream>
#include <queue>
using namespace std;
//
#define ll long long
#define rep(i,a,n) for (ll i=a;i<n;i++)
#define repm(i,a,n) for (ll i=a;i<=n;i++)
//

typedef struct{
    ll m, r;
} list;

bool rule(list a, list b) { return a.m < b.m; }
list a[100005];
int main()
{
    ll i, m = 0, n, d;
    ll s = 0, j = 0;
    cin >> n >> d;
    rep(i, 0, n) cin >> a[i].m >> a[i].r;
    sort(a, a + n, rule);
    rep(i,0,n)
    {
        s = s + a[i].r;
        while (a[i].m - a[j].m >= d)
        {
            s = s - a[j].r;
            j++;
        }
        if (s > m) m = s;

    }
    cout << m;
}


#include <iostream>
#include <cmath>
#include <algorithm>
#define ll long long

const int MAXN = 100000;
using namespace std;
bool compare(pair<int, int> a, pair<int, int> b) {
    return a < b;
}
int main() {
    int n, d;
    cin >> n >> d;
    pair<int, int> f[MAXN];
    for (int i = 0; i < n; i++) {
        int a, b;
        cin >> a >> b;
        f[i] = make_pair(a,b);
    }
    ll ans[MAXN];
    for (int i = 0; i < n; i++) {
        ans[i] = 0;
    }
    sort(f, f + n, compare);
    for (int i = 0; i < n;  i++) {
        pair<int, int> q =  f[i];
        for (int j = i; j < n; j++) {
            pair<int, int> p =  f[j];
            if (q.first + d >= p.first) {
                ans[i] += p.second;
            }
            else {
                j = n;
            }
        }
    }
    ll m = -1;
    for (int i = 0; i < n; i++) {
        if (m < ans[i]) {
            m = ans[i];
        }
    }
    cout << m;
}
/*
*/


#include <iostream>
#include <cmath>
#include <algorithm>
#define ll long long

const int MAXN = 100000;
int b[MAXN];
using namespace std;
bool compare(pair<int, int> a, pair<int, int> b) {
    return a < b;
}
int get_sum(int l, int r) {
    return l != 0 ? b[r] - b[l - 1] : b[r];
}

int main() {
    int n, d;
    cin >> n >> d;
    pair<int, int> f[MAXN];
    for (int i = 0; i < n; i++) {
        int a, b;
        cin >> a >> b;
        f[i] = make_pair(a,b);
    }
    ll ans[MAXN];
    for (int i = 0; i < n; i++) {
        ans[i] = 0;
    }
    sort(f, f + n, compare);

    b[0] = f[0].second;
    for (int i = 1; i < n; i++) {
        b[i] = b[i - 1] + f[i].second;
    }
    for (int i = 0; i < n;  i++) {
        pair<int, int> q =  f[i];
        int l, r, m;
        l = i - 1;
        r = n;
        while (r - l > 1) {
            m = (r + l) / 2;
            if (f[m].first - f[i].first < d) {
                l = m;
            }
            else {
                r = m;
            }
        }
        ans[i] = get_sum(i, l);
    }
    ll m = -1;
    for (int i = 0; i < n; i++) {
        if (m < ans[i]) {
            m = ans[i];
        }
    }
    cout << m;
}
/*
*/


#include <iostream>
#include <cmath>

#define ll long long

const int MAXN = 100000;
using namespace std;
int main() {
    int n, d;
    cin >> n >> d;
    pair<int, int> f[MAXN];
    for (int i = 0; i < n; i++) {
        int a, b;
        cin >> a >> b;
        f[i] = make_pair(a,b);
    }
    ll ans[MAXN];
    for (int i = 0; i < n; i++) {
        ans[i] = 0;
    }
    for (int i = 0; i < n; i++) {
        pair<int, int> q =  f[i];
        ans[i] = q.second;
        for (int j = 0; j < n; j++) {
            pair<int, int> p =  f[j];
            if ((i != j) && (abs(q.first - p.first) < d)) {
                ans[i] += p.second;
            }
        }
    }
    ll m = -1;
    for (int i = 0; i < n; i++) {
        if (m < ans[i]) {
            m = ans[i];
        }
    }
    cout << m;
}


#include <iostream>
#include <cmath>
#include <algorithm>
#define ll long long

const int MAXN = 100000;
ll b[MAXN];
using namespace std;
bool compare(pair<int, int> a, pair<int, int> b) {
    return a < b;
}
ll get_sum(int l, int r) {
    return l != 0 ? b[r] - b[l - 1] : b[r];
}

int main() {
    int n, d;
    cin >> n >> d;
    pair<int, int> f[MAXN];
    for (int i = 0; i < n; i++) {
        int a, b;
        cin >> a >> b;
        f[i] = make_pair(a,b);
    }
    ll ans[MAXN];
    for (int i = 0; i < n; i++) {
        ans[i] = 0;
    }
    sort(f, f + n, compare);

    b[0] = f[0].second;
    for (int i = 1; i < n; i++) {
        b[i] = b[i - 1] + f[i].second;
    }
    for (int i = 0; i < n;  i++) {
        pair<int, int> q =  f[i];
        int l, r, m;
        l = i - 1;
        r = n;
        while (r - l > 1) {
            m = (r + l) / 2;
            if (f[m].first - f[i].first < d) {
                l = m;
            }
            else {
                r = m;
            }
        }
        ans[i] = get_sum(i, l);
    }
    ll m = -1;
    for (int i = 0; i < n; i++) {
        if (m < ans[i]) {
            m = ans[i];
        }
    }
    cout << m;
}
/*
*/


#include <iostream>
#include <cmath>
#include <algorithm>
#define ll long long

const int MAXN = 100000;
using namespace std;
bool compare(pair<int, int> a, pair<int, int> b) {
    return a < b;
}
int main() {
    int n, d;
    cin >> n >> d;
    pair<int, int> f[MAXN];
    for (int i = 0; i < n; i++) {
        int a, b;
        cin >> a >> b;
        f[i] = make_pair(a,b);
    }
    ll ans[MAXN];
    for (int i = 0; i < n; i++) {
        ans[i] = 0;
    }
    sort(f, f + n, compare);
    for (int i = 0; i < n;  i++) {
        pair<int, int> q =  f[i];
        for (int j = i; j < n; j++) {
            pair<int, int> p =  f[j];
            if ((p.first - q.first) < d) {
                ans[i] += p.second;
            }
            else {
                j = n;
            }
        }
    }
    ll m = -1;
    for (int i = 0; i < n; i++) {
        if (m < ans[i]) {
            m = ans[i];
        }
    }
    cout << m;
}
/*
*/


#include <iostream>
#include <algorithm>
#define ll long long
const int MAXN = 100100;
using namespace std;
bool compare(pair<int, int> a, pair<int, int> b) {
    return a < b;
}
int main() {
    int n, d;
    cin >> n >> d;
    pair<int, int> f[MAXN];
for (int i = 0; i < n; i++) {
        int a, b;
        cin >> a >> b;
        f[i] = make_pair(a,b);
    }
    ll ans[MAXN];
    for (int i = 0; i < n; i++) {
        ans[i] = 0;
    }
    sort(f, f + n, compare);

    int left, right = 0;
    ans[0] += f[0].second;
    for (int i = 0; i < n; i++) {
        left = i;
        if (i != 0) {
            ans[left] = ans[left - 1] - f[left - 1].second;
        }
        for (int j = right + 1; j < n; j++){
            if (f[j].first - f[left].first < d) {
                right = j;
                ans[left] += f[right].second;
            }
            else {
                j = n;
            }
        }
    }
    ll m = -1;
    for (int i = 0; i < n; i++) {
        if (m < ans[i]) {
            m = ans[i];
        }
    }
    cout << m;
}


#include <iostream>
#include <cmath>
const int MAXN = 100000;
using namespace std;
int main() {
    int n, d;
    cin >> n >> d;
    pair<int, int> f[MAXN];
    for (int i = 0; i < n; i++) {
        int a, b;
        cin >> a >> b;
        f[i] = make_pair(a,b);
    }
    int ans[MAXN];
    for (int i = 0; i < n; i++) {
        ans[i] = 0;
    }
    for (int i = 0; i < n; i++) {
        pair<int, int> q =  f[i];
        ans[i] = q.second;
        for (int j = 0; j < n; j++) {
            pair<int, int> p =  f[j];
            if ((i != j) && (abs(q.first - p.first) < d)) {
                ans[i] += p.second;
            }
        }
    }
    int m = -1;
    for (int i = 0; i < n; i++) {
        if (m < ans[i]) {
            m = ans[i];
        }
    }
    cout << m;
}


#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
long long cum[100002];
void main()
{
    long long n, d;
    cin >> n >> d;
    vector<pair<long long, long long> > v(n);
    for (int i = 0; i < n; i++)
    {
        cin >> v[i].first >> v[i].second;
    }
    sort(v.begin(), v.end());
    vector<long long> tem(n);
    for (int i = 0; i < n; i++)
    {
        cum[i] = v[i].second;
        if (i)
            cum[i] += cum[i - 1];
        tem[i] = v[i].first;
    }
    long long res = 0;
    for (int i = 0; i < n; i++)
    {
        int ind = upper_bound(tem.begin(), tem.end(), tem[i] + (d - 1)) - tem.begin();
        //cout << ind << "d " << endl;
        if (ind < tem.size() && tem[ind] > tem[i] + (d - 1))
        {
            if (ind - 1 >= 0)
                ind--;
        }
        if (ind == tem.size())
            ind --;
        res = max(res, cum[ind] - (i?cum[i-1]:0));
    }
    cout << res << endl;
    //system("pause");
}
/*
5 106
0 7
11 32
99 10
46 8
87 54

*/


#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
long long cum[100002];
void main()
{
    long long n, d;
    cin >> n >> d;
    vector<pair<int, int> > v(n);
    for (int i = 0; i < n; i++)
    {
        cin >> v[i].first >> v[i].second;
    }
    sort(v.begin(), v.end());
    vector<int> tem(n);
    for (int i = 0; i < n; i++)
    {
        cum[i] = v[i].second;
        if (i)
            cum[i] += cum[i - 1];
        tem[i] = v[i].first;
    }
    long long res = 0;
    for (int i = 0; i < n; i++)
    {
        int ind = lower_bound(tem.begin(), tem.end(), tem[i] + (d - 1)) - tem.begin();
        if (ind < tem.size() && tem[ind] > tem[i] + (d - 1))
        {
            if (ind - 1 >= 0)
                ind--;
            else
                continue;
        }
        if (ind == tem.size())
            ind --;
        res = max(res, cum[ind] - (i?cum[i-1]:0));
    }
    cout << res << endl;
  //  system("pause");
}
/*
5 106
0 7
11 32
99 10
46 8
87 54

*/


#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
long long cum[100002];
void main()
{
    long long n, d;
    cin >> n >> d;
    vector<pair<long long, long long> > v(n);
    for (int i = 0; i < n; i++)
    {
        cin >> v[i].first >> v[i].second;
    }
    sort(v.begin(), v.end());
    vector<long long> tem(n);
    for (int i = 0; i < n; i++)
    {
        cum[i] = v[i].second;
        if (i)
            cum[i] += cum[i - 1];
        tem[i] = v[i].first;
    }
    long long res = 0;
    for (int i = 0; i < n; i++)
    {
        int ind = lower_bound(tem.begin(), tem.end(), tem[i] + (d - 1)) - tem.begin();
        if (ind < tem.size() && tem[ind] > tem[i] + (d - 1))
        {
            if (ind - 1 >= 0)
                ind--;
        }
        if (ind == tem.size())
            ind --;
        res = max(res, cum[ind] - (i?cum[i-1]:0));
    }
    cout << res << endl;
  //  system("pause");
}
/*
5 106
0 7
11 32
99 10
46 8
87 54

*/


#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
long long cum[100002];
void main()
{
    long long n, d;
    cin >> n >> d;
    vector<pair<long long, long long> > v(n);
    for (int i = 0; i < n; i++)
    {
        cin >> v[i].first >> v[i].second;
    }
    sort(v.begin(), v.end());
    vector<long long> tem(n);
    for (int i = 0; i < n; i++)
    {
        cum[i] = v[i].second;
        if (i)
            cum[i] += cum[i - 1];
        tem[i] = v[i].first;
    }
    long long res = 0;
    for (int i = 0; i < n; i++)
    {
        int ind = lower_bound(tem.begin(), tem.end(), tem[i] + (d)) - tem.begin();
        if (ind < tem.size() && tem[ind] > tem[i] + (d))
        {
            if (ind - 1 >= 0)
                ind--;
            else
                continue;
        }
        if (ind == tem.size())
            ind --;
        res = max(res, cum[ind] - (i?cum[i-1]:0));
    }
    cout << res << endl;
  //  system("pause");
}
/*
5 106
0 7
11 32
99 10
46 8
87 54

*/


#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
int cum[100002];
void main()
{
    int n, d;
    cin >> n >> d;
    vector<pair<int, int> > v(n);
    for (int i = 0; i < n; i++)
    {
        cin >> v[i].first >> v[i].second;
    }
    sort(v.begin(), v.end());
    vector<int> tem(n);
    for (int i = 0; i < n; i++)
    {
        cum[i] = v[i].second;
        if (i)
            cum[i] += cum[i - 1];
        tem[i] = v[i].first;
    }
    int res = 0;
    for (int i = 0; i < n; i++)
    {
        int ind = lower_bound(tem.begin(), tem.end(), tem[i] + (d - 1)) - tem.begin();
        if (ind < tem.size() && tem[ind] > tem[i] + (d - 1))
        {
            if (ind - 1 >= 0)
                ind--;
            else
                continue;
        }
        if (ind == tem.size())
            ind --;
        res = max(res, cum[ind] - (i?cum[i-1]:0));
    }
    cout << res << endl;
   // system("pause");
}


#include <string>
#include <algorithm>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <cstdlib>
#include <iostream>
#include <set>
#include <cstring>
#include <map>
#include <iomanip>
#include <sstream>
#include <utility>
#include <iterator>
#include <ctype.h>
#include <cmath>

//freopen("knight.in", "r", stdin);
//freopen("knight.out", "w", stdout);

using namespace std;
#define pi 3.1415926535
#define ll long long

int main()
{
    vector<pair<ll, ll>> v,venv;
    ll n, d,ans=0;
    cin >> n >> d;
    for (int i = 0; i < n; i++)
    {
        ll mi, si;
        cin >> mi >> si;
        v.push_back(make_pair(mi, si));
        venv.push_back(make_pair(si, mi));
        //ans += v[i].second;
    }
    sort(v.begin(), v.end());
    sort(venv.begin(), venv.end());
    reverse(venv.begin(), venv.end());

    ll anss = -1;
    int ref = 0;
    ans += v[ref].second;
    for (int i = 0; i < n ; i++)
    {
        if (abs(v[ref].first - v[i].first) < d && ref != i)
        {
            ans += v[i].second;
            anss = max(ans, anss);
        }
        if (abs(v[ref].first - v[i].first) >= d && ref != i)
        {
            ans -= v[ref].second;
            ref++;
            --i;
            if (ans == 0)
                ans += v[ref].second;
        }
    }
    anss = max(anss, venv[0].first);
    cout << anss << endl;
    return 0;
}


#include <string>
#include <algorithm>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <cstdlib>
#include <iostream>
#include <set>
#include <cstring>
#include <map>
#include <iomanip>
#include <sstream>
#include <utility>
#include <iterator>
#include <ctype.h>
#include <cmath>

//freopen("knight.in", "r", stdin);
//freopen("knight.out", "w", stdout);

using namespace std;
#define pi 3.1415926535
#define ll long long

int main()
{
    vector<pair<ll, ll>> v,venv,vans;
    ll m, s,ans=0;
    cin >> m >> s;
    for (int i = 0; i < m; i++)
    {
        int mi, si;
        cin >> mi >> si;
        v.push_back(make_pair(mi, si));
        venv.push_back(make_pair(si, mi));
        ans += v[i].second;
    }
    sort(v.begin(), v.end());
    sort(venv.begin(), venv.end());
    reverse(venv.begin(), venv.end());

    for (int j = 0; j < m; j++)
    {
        int flag = 0;
        for (int i = 0; i < m ; i++)
        {
            if ( i != j && abs(v[i].first - v[j].first) < s)
            {

            }
            else if (i != j && abs(v[i].first - v[j].first) >= s)
            {
                flag = 1;
                break;
            }
        }
        if (flag == 1)
            ans -= v[j].second;
    }

    ans = max(ans, venv[0].first);
    cout << ans << endl;
    return 0;
}


#include <string>
#include <algorithm>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <cstdlib>
#include <iostream>
#include <set>
#include <cstring>
#include <map>
#include <iomanip>
#include <sstream>
#include <utility>
#include <iterator>
#include <ctype.h>
#include <cmath>

//freopen("knight.in", "r", stdin);
//freopen("knight.out", "w", stdout);

using namespace std;
#define pi 3.1415926535
#define ll long long

int main()
{
    vector<pair<ll, ll>> v,venv,vans;
    ll m, s;
    cin >> m >> s;
    for (int i = 0; i < m; i++)
    {
        int mi, si;
        cin >> mi >> si;
        v.push_back(make_pair(mi, si));
        venv.push_back(make_pair(si, mi));
    }
    sort(v.begin(), v.end());
    sort(venv.begin(), venv.end());
    reverse(venv.begin(), venv.end());

    //vans.push_back(make_pair(v[0].first, v[0].second));
    ll ans = 0;
    ans += v[0].second;
    for (int i = 0; i < m-1; i++)
    {
        if (abs(v[i].first - v[i + 1].first) >= s)
        {
            continue;
        }
        else
        {
            ans += v[i + 1].second;
            //vans.push_back(make_pair(v[i+1].first,v[i+1].second));
        }
    }
    ans = max(ans, venv[0].second);
    cout << ans << endl;
    return 0;
}


#include <string>
#include <algorithm>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <cstdlib>
#include <iostream>
#include <set>
#include <cstring>
#include <map>
#include <iomanip>
#include <sstream>
#include <utility>
#include <iterator>
#include <ctype.h>
#include <cmath>

//freopen("knight.in", "r", stdin);
//freopen("knight.out", "w", stdout);

using namespace std;
#define pi 3.1415926535
#define ll long long

int main()
{
    vector<pair<ll, ll>> v,venv,vans;
    ll m, s;
    cin >> m >> s;
    for (int i = 0; i < m; i++)
    {
        int mi, si;
        cin >> mi >> si;
        v.push_back(make_pair(mi, si));
        venv.push_back(make_pair(si, mi));
    }
    sort(v.begin(), v.end());
    sort(venv.begin(), venv.end());
    reverse(venv.begin(), venv.end());

    ll ans = 0;
    ans += v[0].second;
    for (int i = 0; i < m-1; i++)
    {
        if (abs(v[i].first - v[i + 1].first) >= s)
        {
            ll minx = min(v[0].first,v[1].first);
            if (i == 0 && minx == v[0].first)
                ans -= v[0].second;
            else if (i == 0 && minx == v[1].first)
                ans -= v[1].second;
            continue;
        }
        else
        {
            ans += v[i + 1].second;
        }
    }
    ans = max(ans, venv[0].first);
    cout << ans << endl;
    return 0;
}


#include <string>
#include <algorithm>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <cstdlib>
#include <iostream>
#include <set>
#include <cstring>
#include <map>
#include <iomanip>
#include <sstream>
#include <utility>
#include <iterator>
#include <ctype.h>
#include <cmath>

//freopen("knight.in", "r", stdin);
//freopen("knight.out", "w", stdout);

using namespace std;
#define pi 3.1415926535
#define ll long long

int main()
{
    vector<pair<ll, ll>> v,venv;
    ll n, d,ans=0;
    cin >> n >> d;
    for (int i = 0; i < n; i++)
    {
        ll mi, si;
        cin >> mi >> si;
        v.push_back(make_pair(mi, si));
        venv.push_back(make_pair(si, mi));
        //ans += v[i].second;
    }
    sort(v.begin(), v.end());
    sort(venv.begin(), venv.end());
    reverse(venv.begin(), venv.end());

    ll anss = -1;
    int ref = 0;
    ans += v[ref].second;
    for (int i = 0; i < n ; i++)
    {
        if (abs(v[ref].first - v[i].first) >= d && ref != i)
        {
            ans -= v[ref].second;
            ref++;
            if (i <= ref)
                ans += v[ref].second;
        }
        if (abs(v[ref].first - v[i].first) < d && ref != i)
        {
            ans += v[i].second;
            anss = max(ans, anss);
        }

    }
    anss = max(anss, venv[0].first);
    cout << anss << endl;
    return 0;
}


#include <string>
#include <algorithm>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <cstdlib>
#include <iostream>
#include <set>
#include <cstring>
#include <map>
#include <iomanip>
#include <sstream>
#include <utility>
#include <iterator>
#include <ctype.h>
#include <cmath>

//freopen("knight.in", "r", stdin);
//freopen("knight.out", "w", stdout);

using namespace std;
#define pi 3.1415926535
#define ll long long

int main()
{
    vector<pair<ll, ll>> v,venv,vans;
    ll m, s,ans=0;
    cin >> m >> s;
    for (int i = 0; i < m; i++)
    {
        int mi, si;
        cin >> mi >> si;
        v.push_back(make_pair(mi, si));
        venv.push_back(make_pair(si, mi));
        ans += v[i].second;
    }
    sort(v.begin(), v.end());
    sort(venv.begin(), venv.end());
    reverse(venv.begin(), venv.end());
    int flag = 0;
    for (int j = 0; j < m; j++)
    {
        for (int i = 0; i < m ; i++)
        {
            if ( i != j && abs(v[i].first - v[j].first) < s)
            {

            }
            else if (i != j && abs(v[i].first - v[j].first) >= s)
            {
                flag = 1;
                break;
            }
        }
        if (flag == 1)
            ans -= v[j].second;
    }

    ans = max(ans, venv[0].first);
    cout << ans << endl;
    return 0;
}


#include <string>
#include <algorithm>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <cstdlib>
#include <iostream>
#include <set>
#include <cstring>
#include <map>
#include <iomanip>
#include <sstream>
#include <utility>
#include <iterator>
#include <ctype.h>
#include <cmath>

//freopen("knight.in", "r", stdin);
//freopen("knight.out", "w", stdout);

using namespace std;
#define pi 3.1415926535
#define ll long long

int main()
{
    vector<pair<ll, ll>> v,venv;
    ll n, d,ans=0;
    cin >> n >> d;
    for (int i = 0; i < n; i++)
    {
        ll mi, si;
        cin >> mi >> si;
        v.push_back(make_pair(mi, si));
        venv.push_back(make_pair(si, mi));
        //ans += v[i].second;
    }
    sort(v.begin(), v.end());
    sort(venv.begin(), venv.end());
    reverse(venv.begin(), venv.end());

    int ref = 0;
    ans += v[ref].second;
    for (int i = 0; i < n-1 ; i++)
    {
        if (abs(v[i].first - v[i+1].first) >= d)
        {
            ans -= v[ref].second;
            ref++;
            ans += v[ref].second;
        }
        if (abs(v[i].first - v[i+1].first) < d)
        {
            ans += v[i + 1].second;
        }
    }

    ans = max(ans, venv[0].first);
    cout << ans << endl;
    return 0;
}


#include <string>
#include <algorithm>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <cstdlib>
#include <iostream>
#include <set>
#include <cstring>
#include <map>
#include <iomanip>
#include <sstream>
#include <utility>
#include <iterator>
#include <ctype.h>
#include <cmath>

//freopen("knight.in", "r", stdin);
//freopen("knight.out", "w", stdout);

using namespace std;
#define pi 3.1415926535
#define ll long long

int main()
{
    vector<pair<ll, ll>> v,venv;
    ll n, d,ans=0;
    cin >> n >> d;
    for (int i = 0; i < n; i++)
    {
        ll mi, si;
        cin >> mi >> si;
        v.push_back(make_pair(mi, si));
        venv.push_back(make_pair(si, mi));
        ans += v[i].second;
    }
    sort(v.begin(), v.end());
    sort(venv.begin(), venv.end());
    reverse(venv.begin(), venv.end());
    int temp_i;
    for (int j = 0; j < n; j++)
    {
        int flag = 0;
        for (int i = 0; i < n ; i++)
        {
            if (i != j && abs(v[i].first - v[j].first) >= d)
            {
                flag = 1;
                temp_i = i;
                break;
            }
        }
        if (flag == 1 && v[j].first > v[temp_i].first)
            ans -= v[j].second;

    }

    ans = max(ans, venv[0].first);
    cout << ans << endl;
    return 0;
}


#include <string>
#include <algorithm>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <cstdlib>
#include <iostream>
#include <set>
#include <cstring>
#include <map>
#include <iomanip>
#include <sstream>
#include <utility>
#include <iterator>
#include <ctype.h>
#include <cmath>

//freopen("knight.in", "r", stdin);
//freopen("knight.out", "w", stdout);

using namespace std;
#define pi 3.1415926535
#define ll long long

int main()
{
    vector<pair<ll, ll>> v,venv,vans;
    ll m, s;
    cin >> m >> s;
    for (int i = 0; i < m; i++)
    {
        int mi, si;
        cin >> mi >> si;
        v.push_back(make_pair(mi, si));
        venv.push_back(make_pair(si, mi));
    }
    sort(v.begin(), v.end());
    sort(venv.begin(), venv.end());
    reverse(venv.begin(), venv.end());

    ll ans = 0;
    ans += v[0].second;
    for (int i = 0; i < m-1; i++)
    {
        if (abs(v[i].first - v[i + 1].first) >= s)
        {
            if (i == 0)
                ans -= v[0].second;
            continue;
        }
        else
        {
            ans += v[i + 1].second;
        }
    }
    ans = max(ans, venv[0].first);
    cout << ans << endl;
    return 0;
}


#include <string>
#include <algorithm>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <cstdlib>
#include <iostream>
#include <set>
#include <cstring>
#include <map>
#include <iomanip>
#include <sstream>
#include <utility>
#include <iterator>
#include <ctype.h>
#include <cmath>

//freopen("knight.in", "r", stdin);
//freopen("knight.out", "w", stdout);

using namespace std;
#define pi 3.1415926535
#define ll long long

int main()
{
    vector<pair<ll, ll>> v,venv;
    ll n, d,ans=0;
    cin >> n >> d;
    for (int i = 0; i < n; i++)
    {
        ll mi, si;
        cin >> mi >> si;
        v.push_back(make_pair(mi, si));
        venv.push_back(make_pair(si, mi));
        //ans += v[i].second;
    }
    sort(v.begin(), v.end());
    sort(venv.begin(), venv.end());
    reverse(venv.begin(), venv.end());

    int ref = 0;
    ans += v[ref].second;
    for (int i = 0; i < n ; i++)
    {
        if (abs(v[ref].first - v[i].first) >= d && ref != i)
        {
            ans -= v[ref].second;
            ref++;
            if (i <= ref)
                ans += v[ref].second;
        }
        if (abs(v[ref].first - v[i].first) < d && ref != i)
        {
            ans += v[i].second;
        }

    }

    ans = max(ans, venv[0].first);
    cout << ans << endl;
    return 0;
}


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

vector <pair <int, int> > vt;

int main() {
	//freopen ("input.txt", "r", stdin);
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	int n, d;
	long long maxy = 0;
	cin >> n >> d;
	for (int i = 0; i < n; i++) {
		int a, b;
		cin >> a >> b;
		vt.push_back (make_pair (a, b) );
	}
	sort (vt.begin(), vt.end() );
	for (int i = 0; i < n; i++) {
		if (i > 0)
			vt[i].second += vt[i-1].second;
	}
	for (int i = 0; i < n; i++) {
		long long sum = 0;
		int l = i, r = n - 1, ans;
		while (l <= r) {
			int mid = (l + r) >> 1;
			if ( (vt[mid].first - vt[i].first) >= d) {
				r = mid - 1;
			}
			else {
				ans = mid;
				l = mid + 1;
			}
		}
		if (i > 0)
			sum = vt[ans].second - vt[i-1].second;
		else
			sum = vt[ans].second;
		maxy = max (maxy, sum);
	}
	cout << maxy << endl;
}


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

vector <pair <long long, long long> > vt;

int main() {
	//freopen ("input.txt", "r", stdin);
	int n, d;
	scanf ("%d%d", &n, &d);
	for (int i = 0; i < n; i++) {
		long long a, b;
		scanf ("%I64d%I64d", &a, &b);
		vt.push_back (make_pair (a, b) );
	}
	sort (vt.begin(), vt.end() );
	for (int i = 0; i < n; i++) {
		if (i > 0)
			vt[i].second += vt[i-1].second;
	}
	int l = 0, r = 0;
	long long maxy = 0, sum = 0;
	for (int i = 0; r < n; i++) {
		if ( (vt[r].first - vt[l].first) < d) {
			r++;
		}
		else {
			if (l > 0 && (r - l) > 0)
				sum = vt[r - 1].second - vt[l-1].second;
			else
				sum = vt[r - 1].second;
			maxy = max (maxy, sum);
			l++;
			r = max (r, l);
		}
		//cout << l << ' ' << r << endl;
	}
	if (l > 0 && (r - l) > 0)
		sum = vt[r - 1].second - vt[l-1].second;
	else
		sum = vt[r - 1].second;
	maxy = max (maxy, sum);
	cout << maxy << endl;
}


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

vector <pair <int, int> > vt;

int main() {
	//freopen ("input.txt", "r", stdin);

	long long n, d, r, l, ans, maxy = 0;
	cin >> n >> d;
	for (long long i = 0; i < n; i++) {
		int a, b;
		cin >> a >> b;
		vt.push_back (make_pair (a, b) );
	}
	sort (vt.begin(), vt.end() );
	for (long long i = 0; i < n; i++) {
		long long sum = 0;
		l = 0, r = i;
		while (l <= r) {
			long long mid = (l + r) >> 1;
			//cout << vt[mid].first << ' ' << vt[i].first << endl;
			if ( (abs(vt[mid].first - vt[i].first) ) >= d) {
				r = mid - 1;
			}
			else {
				ans = mid;
				l = mid + 1;
			}
		}
		for (long long j = ans; j <= i; j++) {
			sum += vt[j].second;
		}
		l = i, r = n - 1;
		while (l <= r) {
			long long mid = (l + r) >> 1;
			//cout << vt[mid].first << ' ' << vt[i].first << endl;
			if ( (abs(vt[mid].first - vt[i].first) ) >= d) {
				r = mid - 1;
			}
			else {
				ans = mid;
				l = mid + 1;
			}
		}
		for (long long j = i + 1; j <= ans; j++) {
			sum += vt[j].second;
		}

		maxy = max (maxy, sum);
	}
	cout << maxy << endl;
}


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

vector <pair <int, int> > vt;

int main() {
	//freopen ("input.txt", "r", stdin);
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	long long n, d, r, l, ans, maxy = 0;
	cin >> n >> d;
	for (long long i = 0; i < n; i++) {
		int a, b;
		cin >> a >> b;
		vt.push_back (make_pair (a, b) );
	}
	sort (vt.begin(), vt.end() );
	for (long long i = 0; i < n; i++) {
		long long sum = 0;
		l = i, r = n - 1;
		while (l <= r) {
			long long mid = (l + r) >> 1;
			if ( (vt[mid].first - vt[i].first) >= d) {
				r = mid - 1;
			}
			else {
				ans = mid;
				l = mid + 1;
			}
		}
		for (long long j = i; j <= ans; j++) {
			sum += vt[j].second;
		}
		maxy = max (maxy, sum);
	}
	cout << maxy << endl;
}


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

vector <pair <long long, long long> > vt;

int main() {
	int n, d;
	long long maxy = 0;
	scanf ("%d%d", &n, &d);
	for (int i = 0; i < n; i++) {
		long long a, b;
		scanf ("%I64d%I64d", &a, &b);
		vt.push_back (make_pair (a, b) );
	}
	sort (vt.begin(), vt.end() );
	for (int i = 0; i < n; i++) {
		if (i > 0)
			vt[i].second += vt[i-1].second;
	}
	for (int i = 0; i < n; i++) {
		long long sum = 0;
		int l = i, r = n - 1, ans;
		while (l <= r) {
			int mid = (l + r) >> 1;
			if ( (vt[mid].first - vt[i].first) >= d) {
				r = mid - 1;
			}
			else {
				ans = mid;
				l = mid + 1;
			}
		}
		if (i > 0)
			sum = vt[ans].second - vt[i-1].second;
		else
			sum = vt[ans].second;
		maxy = max (maxy, sum);
	}
	cout << maxy << endl;
}


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

vector <pair <int, int> > vt;

int main() {
	//freopen ("input.txt", "r", stdin);
	int n, d, r, l, ans, a, b;
	long long sum, maxy = 0;
	scanf ("%d%d", &n, &d);
	for (long long i = 0; i < n; i++) {
		scanf ("%d%d", &a, &b);
		vt.push_back (make_pair (a, b) );
	}
	sort (vt.begin(), vt.end() );
	for (int i = 0; i < n; i++) {
		sum = 0;
		l = i, r = n - 1;
		while (l <= r) {
			int mid = (l + r) >> 1;
			if ( (vt[mid].first - vt[i].first) >= d) {
				r = mid - 1;
			}
			else {
				ans = mid;
				l = mid + 1;
			}
		}
		for (int j = i; j <= ans; j++) {
			sum += vt[j].second;
		}
		maxy = max (maxy, sum);
	}
	printf ("%I64d\n", maxy);
}


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

pair <long long, long long> vt[100001];

int main() {
	int n, d;
	long long maxy = 0;
	scanf ("%d%d", &n, &d);
	for (int i = 0; i < n; i++) {
		long long a, b;
		scanf ("%I64d%I64d", &a, &b);
		vt[i] = make_pair (a, b);
	}
	sort (vt, vt + n);
	for (int i = 0; i < n; i++) {
		if (i > 0)
			vt[i].second += vt[i-1].second;
	}
	for (int i = 0; i < n; i++) {
		long long sum = 0;
		int l = i, r = n - 1, ans;
		while (l <= r) {
			int mid = (l + r) >> 1;
			if ( (vt[mid].first - vt[i].first) >= d) {
				r = mid - 1;
			}
			else {
				ans = mid;
				l = mid + 1;
			}
		}
		if (i > 0)
			sum = vt[ans].second - vt[i-1].second;
		else
			sum = vt[ans].second;
		maxy = max (maxy, sum);
	}
	cout << maxy << endl;
}


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

vector <pair <long long, long long> > vt;

int main() {
	//freopen ("input.txt", "r", stdin);
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	long long n, d;
	long long maxy = 0;
	cin >> n >> d;
	for (long long i = 0; i < n; i++) {
		long long a, b;
		cin >> a >> b;
		vt.push_back (make_pair (a, b) );
	}
	sort (vt.begin(), vt.end() );
	for (long long i = 0; i < n; i++) {
		if (i > 0)
			vt[i].second += vt[i-1].second;
	}
	for (long long i = 0; i < n; i++) {
		long long sum = 0;
		long long l = i, r = n - 1, ans;
		while (l <= r) {
			long long mid = (l + r) >> 1;
			if ( (vt[mid].first - vt[i].first) >= d) {
				r = mid - 1;
			}
			else {
				ans = mid;
				l = mid + 1;
			}
		}
		if (i > 0)
			sum = vt[ans].second - vt[i-1].second;
		else
			sum = vt[ans].second;
		maxy = max (maxy, sum);
	}
	cout << maxy << endl;
}


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

vector <pair <int, int> > vt;

int main() {
	//freopen ("input.txt", "r", stdin);

	long long n, d, r, l, ans, maxy = 0;
	cin >> n >> d;
	for (long long i = 0; i < n; i++) {
		int a, b;
		cin >> a >> b;
		vt.push_back (make_pair (a, b) );
	}
	sort (vt.begin(), vt.end() );
	for (long long i = 0; i < n; i++) {
		long long sum = 0;
		l = i, r = n - 1;
		while (l <= r) {
			long long mid = (l + r) >> 1;
			//cout << vt[mid].first << ' ' << vt[i].first << endl;
			if ( (vt[mid].first - vt[i].first) > d) {
				r = mid - 1;
			}
			else {
				ans = mid;
				l = mid + 1;
			}
		}
		for (long long j = i; j <= ans; j++) {
			sum += vt[j].second;
		}
		maxy = max (maxy, sum);
	}
	cout << maxy << endl;
}


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

pair <int, int> v[100001];

int main() {
	//freopen ("input.txt", "r", stdin);
	int n, d, r, l, ans, a, b;
	long long sum, maxy = 0;
	scanf ("%d%d", &n, &d);
	for (int i = 0; i < n; i++) {
		scanf ("%d%d", &a, &b);
		v[i] = make_pair (a, b);
	}
	sort (v, v + n);
	for (int i = 0; i < n; i++) {
		sum = 0;
		l = i, r = n - 1;
		while (l <= r) {
			int mid = (l + r) >> 1;
			if ( (v[mid].first - v[i].first) >= d) {
				r = mid - 1;
			}
			else {
				ans = mid;
				l = mid + 1;
			}
		}
		for (int j = i; j <= ans; j++) {
			sum += v[j].second;
		}
		maxy = max (maxy, sum);
	}
	printf ("%I64d\n", maxy);
}


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

vector <pair <int, int> > vt;

int main() {
	//freopen ("input.txt", "r", stdin);

	long long n, d, r, l, ans, maxy = 0;
	cin >> n >> d;
	for (long long i = 0; i < n; i++) {
		int a, b;
		cin >> a >> b;
		vt.push_back (make_pair (a, b) );
	}
	sort (vt.begin(), vt.end() );
	for (long long i = 0; i < n; i++) {
		long long sum = 0;
		l = i, r = n - 1;
		while (l <= r) {
			long long mid = (l + r) >> 1;
			if ( (vt[mid].first - vt[i].first) >= d) {
				r = mid - 1;
			}
			else {
				ans = mid;
				l = mid + 1;
			}
		}
		for (long long j = i; j <= ans; j++) {
			sum += vt[j].second;
		}
		maxy = max (maxy, sum);
	}
	cout << maxy << endl;
}


#include<iostream>
#include<math.h>
#include <iomanip>
#include <algorithm>
#include <vector>
using namespace std;
int d[100005];
long long int s[100005];
int d1[100005];
long long int s1[100005];
long long int st[100005];
void sort(int l, int r){
	if (l == r) return;
	int m = (r + l) / 2;
	sort(l, m);
	sort(m + 1, r);
	int p = l;
	int p1 = l;
	int p2 = m + 1;
	while ((p1 <= m) && (p2 <= r)){
		if (d[p1] > d[p2]){
		d1[p] = d[p2];
		s1[p] = s[p2];
		p++;
		p2++;
		}
		else {
			d1[p] = d[p1];
			s1[p] = s[p1];
			p++;
			p1++;
		}
}
	if (p1 == m + 1){
		while (p2 <= r){
			d1[p] = d[p2];
			s1[p] = s[p2];
			p++; p2++;
		}
	}
	else {
		while (p1 <= m){

			d1[p] = d[p1];
			s1[p] = s[p1];
			p++;
			p1++;
		}

	}

	for (p = l; p <= r; p++){
		d[p] = d1[p];
		s[p] = s1[p];
	}
}
int main(){
#pragma warning(disable : 4996)
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	int n, de;
	cin >> n >> de;

	for (int i = 1; i <= n; i++){
		cin >> d[i] >> s[i];
	}
	sort(1, n);

	for (int i=1;i<=n;i++){
	cout<<d[i]<<" "<<s[i]<<endl;

	}

	for (int i = 1; i <= n; i++){
		st[i] = s[i];
	}

	for (int i = 1; i < n; i++){
		int j = i+1;
		while ((d[j] < d[i] + de)&&(j<=n)){
			st[i] = st[i] + s[j];
			j++;
		}
	}
	int maxi = 1;
	for (int i = 1; i <= n; i++){
		if (st[i] >st[maxi]){
			maxi = i;
		}
		cout<<st[i]<<endl;
	}
	cout << st[maxi];
	return 0;

}


#include<iostream>
#include<math.h>
#include <iomanip>
#include <algorithm>
#include <vector>
using namespace std;
int d[100005];
long long int s[100005];
int d1[100005];
long long int s1[100005];
long long int st[100005];
void sort(int l, int r){
	if (l == r) return;
	int m = (r + l) / 2;
	sort(l, m);
	sort(m + 1, r);
	int p = l;
	int p1 = l;
	int p2 = m + 1;
	while ((p1 <= m) && (p2 <= r)){
		if (d[p1] > d[p2]){
		d1[p] = d[p2];
		s1[p] = s[p2];
		p++;
		p2++;
		}
		else {
			d1[p] = d[p1];
			s1[p] = s[p1];
			p++;
			p1++;
		}
}
	if (p1 == m + 1){
		while (p2 <= r){
			d1[p] = d[p2];
			s1[p] = s[p2];
			p++; p2++;
		}
	}
	else {
		while (p1 <= m){

			d1[p] = d[p1];
			s1[p] = s[p1];
			p++;
			p1++;
		}

	}

	for (p = l; p <= r; p++){
		d[p] = d1[p];
		s[p] = s1[p];
	}
}
int main(){
#pragma warning(disable : 4996)
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	int n, de;
cin >> n >> de;

	for (int i = 1; i <= n; i++){
		cin >> d[i] >> s[i];
	}
	sort(1, n);

	int l=1;
	int r=1;
	long long int max=s[1];
	long long int st=0;
	while (r<=n){
		if (d[r]-d[l]<de){
			st=st+s[r];
			r++;
		}else {

		st=st-s[l];
		l++;
		}
		if (st>max){
		max=st;}
	}
	cout<<max;

	return 0;

}


#include<iostream>
#include<math.h>
#include <iomanip>
#include <algorithm>
#include <vector>
using namespace std;
int d[100005];
long long int s[100005];
int d1[100005];
long long int s1[100005];
long long int st[100005];
void sort(int l, int r){
	if (l == r) return;
	int m = (r + l) / 2;
	sort(l, m);
	sort(m + 1, r);
	int p = l;
	int p1 = l;
	int p2 = m + 1;
	while ((p1 <= m) && (p2 <= r)){
		if (d[p1] > d[p2]){
		d1[p] = d[p2];
		s1[p] = s[p2];
		p++;
		p2++;
		}
		else {
			d1[p] = d[p1];
			s1[p] = s[p1];
			p++;
			p1++;
		}
}
	if (p1 == m + 1){
		while (p2 <= r){
			d1[p] = d[p2];
			s1[p] = s[p2];
			p++; p2++;
		}
	}
	else {
		while (p1 <= m){

			d1[p] = d[p1];
			s1[p] = s[p1];
			p++;
			p1++;
		}

	}

	for (p = l; p <= r; p++){
		d[p] = d1[p];
		s[p] = s1[p];
	}
}
int main(){
#pragma warning(disable : 4996)
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	int n, de;
cin >> n >> de;

	for (int i = 1; i <= n; i++){
		cin >> d[i] >> s[i];
	}
	sort(1, n);

	int l=1;
	int r=1;
	int max=s[1];
	int st=0;
	while (r<=n){
		if (d[r]-d[l]<de){
			st=st+s[r];
			r++;
		}else {

		st=st-s[l];
		l++;
		}
		if (st>max){
		max=st;}
	}
	cout<<max;

	return 0;

}


#include<iostream>
#include<math.h>
#include <iomanip>
#include <algorithm>
#include <vector>
using namespace std;
int d[100005];
long long int s[100005];
int d1[100005];
long long int s1[100005];
long long int st[100005];
void sort(int l, int r){
	if (l == r) return;
	int m = (r + l) / 2;
	sort(l, m);
	sort(m + 1, r);
	int p = l;
	int p1 = l;
	int p2 = m + 1;
	while ((p1 <= m) && (p2 <= r)){
		if (d[p1] > d[p2]){
		d1[p] = d[p2];
		s1[p] = s[p2];
		p++;
		p2++;
		}
		else {
			d1[p] = d[p1];
			s1[p] = s[p1];
			p++;
			p1++;
		}
}
	if (p1 == m + 1){
		while (p2 <= r){
			d1[p] = d[p2];
			s1[p] = s[p2];
			p++; p2++;
		}
	}
	else {
		while (p1 <= m){

			d1[p] = d[p1];
			s1[p] = s[p1];
			p++;
			p1++;
		}

	}

	for (p = l; p <= r; p++){
		d[p] = d1[p];
		s[p] = s1[p];
	}
}
int main(){
#pragma warning(disable : 4996)
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	int n, de;
	cin >> n >> de;

	for (int i = 1; i <= n; i++){
		cin >> d[i] >> s[i];
	}
	sort(1, n);

	for (int i = 1; i <= n; i++){
		st[i] = s[i];
	}

	for (int i = 1; i < n; i++){
		int j = i+1;
		while ((d[j] < d[i] + de)&&(j<=n)){
			st[i] = st[i] + s[j];
			j++;
		}
	}
	int maxi = 1;
	for (int i = 1; i <= n; i++){
		if (st[i] >st[maxi]){
			maxi = i;
		}

	}
	cout << st[maxi];
	return 0;

}


#include<iostream>
#include<math.h>
#include <iomanip>
#include <algorithm>
#include <vector>
using namespace std;
int d[100005];
int s[100005];
int d1[100005];
int s1[100005];
int st[100005];
void sort(int l, int r){
	if (l == r) return;
	int m = (r + l) / 2;
	sort(l, m);
	sort(m + 1, r);
	int p = l;
	int p1 = l;
	int p2 = m + 1;
	while ((p1 <= m) && (p2 <= r)){
		if (d[p1] > d[p2]){
		d1[p] = d[p2];
		s1[p] = s[p2];
		p++;
		p2++;
		}
		else {
			d1[p] = d[p1];
			s1[p] = s[p1];
			p++;
			p1++;
		}
}
	if (p1 == m + 1){
		while (p2 <= r){
			d1[p] = d[p2];
			s1[p] = s[p2];
			p++; p2++;
		}
	}
	else {
		while (p1 <= m){

			d1[p] = d[p1];
			s1[p] = s[p1];
			p++;
			p1++;
		}

	}

	for (p = l; p <= r; p++){
		d[p] = d1[p];
		s[p] = s1[p];
	}
}
int main(){
#pragma warning(disable : 4996)
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	int n, de;
	cin >> n >> de;

	for (int i = 1; i <= n; i++){
		cin >> d[i] >> s[i];
	}
	sort(1, n);

	for (int i = 1; i <= n; i++){
		st[i] = s[i];
	}

	for (int i = 1; i < n; i++){
		int j = i+1;
		while ((d[j] < d[i] + de)&&(j<=n)){
			st[i] = st[i] + s[j];
			j++;
		}
	}
	int maxi = 1;
	for (int i = 1; i <= n; i++){
		if (st[i] >st[maxi]){
			maxi = i;
		}

	}
	cout << st[maxi];
	return 0;

}


#include<iostream>
#include<math.h>
#include <iomanip>
#include <algorithm>
#include <vector>
using namespace std;
int d[100005];
int s[100005];
int d1[100005];
int s1[100005];
int st[100005];
void sort(int l, int r){
	if (l == r) return;
	int m = (r + l) / 2;
	sort(l, m);
	sort(m + 1, r);
	int p = l;
	int p1 = l;
	int p2 = m + 1;
	while ((p1 <= m) && (p2 <= r)){
		if (d[p1] > d[p2]){
		d1[p] = d[p2];
		s1[p] = s[p2];
		p++;
		p2++;
		}
		else {
			d1[p] = d[p1];
			s1[p] = s[p1];
			p++;
			p1++;
		}
}
	if (p1 == m + 1){
		while (p2 <= r){
			d1[p] = d[p2];
			s1[p] = s[p2];
			p++; p2++;
		}
	}
	else {
		while (p1 <= m){

			d1[p] = d[p1];
			s1[p] = s[p1];
			p++;
			p1++;
		}

	}

	for (p = l; p <= r; p++){
		d[p] = d1[p];
		s[p] = s1[p];
	}
}
int main(){
#pragma warning(disable : 4996)
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	int n, de;
	cin >> n >> de;

	for (int i = 1; i <= n; i++){
		cin >> d[i] >> s[i];
	}
	sort(1, n);

	for (int i = 1; i <= n; i++){
		st[i] = s[i];
	}

	for (int i = 1; i < n; i++){
		int j = i+1;
		while (d[j] < d[i] + de){
			st[i] = st[i] + s[j];
			j++;
		}
	}
	int maxi = 1;
	for (int i = 1; i <= n; i++){
		if (st[i] > maxi){
			maxi = i;
		}
	}
	cout << st[maxi];
	return 0;

}


# define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stdio.h>
#include <vector>
#include <cstdlib>
#include <string>
#include <map>
#include <ctime>
#include <list>
#include <stack>
#include <set>
#include <memory.h>
#include <deque>
#include <queue>
#include <cctype>
#include <iomanip>
#include <bitset>
#include <sstream>
typedef long long ll;

using namespace std;

# define pi 2*acos(0.)
# define e exp(1.)
# define N 111111

ll n, l, m, i, j, ans = 0, k, d, x, y;
ll a[N],b[N];
//string s;
pair<ll, ll> v1[N];

bool cmp(pair<ll,ll> &p1, pair<ll, ll>&p2){
   if(p1.first != p2.first)  return p1.first<p2.first;
   else return p1.second < p2.second;
}

int main(){

    cin >> n >> d;
    for (i = 1; i <= n; ++i)
    cin >> v1[i].first>> v1[i].second;

    sort(v1+1, v1+n+1,cmp);

    b[1] = v1[1].second;

    for (i = 1; i <= n; ++i)
        b[i] = b[i - 1] + v1[i].second;

    ans = 0;
    int q = 1;
    for (i = 2; i <= n; ++i)
    {

        if (v1[i].first - v1[q].first >= d){
            ans = max(b[i - 1] - b[q-1], ans);
            while (v1[i].first - v1[q].first >= d)
                q++;
        }
    }
        //if (!x)sum += v1[j].second;

    ans = max(b[n - 1] - b[q-1], ans);

    cout << ans << endl;
    return 0;

}


# define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stdio.h>
#include <vector>
#include <cstdlib>
#include <string>
#include <map>
#include <ctime>
#include <list>
#include <stack>
#include <set>
#include <memory.h>
#include <deque>
#include <queue>
#include <cctype>
#include <iomanip>
#include <bitset>
#include <sstream>
typedef long long ll;

using namespace std;

# define pi 2*acos(0.)
# define e exp(1.)
# define N 100010

ll n, l, m, i, j, ans = 0, k, d, x, y;
ll a[N];
string s;
vector <pair<ll, ll>>v1, v2;

bool cmp(pair<ll,ll> &p1, pair<ll, ll>&p2){
    return p1.second < p2.second;
}

int main(){
    cin >> n >> d;
    for (i = 0; i < n; ++i)
    {
        cin >> x >> y;

        v1.push_back(make_pair(x, y));
    }

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

    for (i = 0; i < v1.size() - 1; ++i)
    {
        if (v1[i].first + d < v1[v1.size() - 1].first)
            continue;
        else {
            if (i == v1.size() - 2)
                ans += v1[i].second + v1[i + 1].second;
            else ans += v1[i].second;
        }
    }

    if (!ans){
        sort(v1.begin(), v1.end(), cmp);
        ans = v1[v1.size() - 1].second;
    }

    cout << ans << endl;

    /*int x0, y0, x1, y1, x2, y2;
    cin >> x0 >> y0;*/
    return 0;
}


# define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stdio.h>
#include <vector>
#include <cstdlib>
#include <string>
#include <map>
#include <ctime>
#include <list>
#include <stack>
#include <set>
#include <memory.h>
#include <deque>
#include <queue>
#include <cctype>
#include <iomanip>
#include <bitset>
#include <sstream>
typedef long long ll;

using namespace std;

# define pi 2*acos(0.)
# define e exp(1.)
# define N 100010

int n, l, m, i, j, ans = 0, k,d,x,y;
int a[N];
string s;
vector <pair<int,int>>v1,v2;

bool cmp(pair<int, int> &p1, pair<int, int>&p2){
    return p1.second < p2.second;
}

int main(){
    cin >> n >> d;
    for (i = 0; i < n; ++i)
    {
        cin >> x >> y;

        v1.push_back(make_pair(x, y));
    }

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

    for (i = 0; i < v1.size()-1; ++i)
    {
        if (v1[i].first+d < v1[v1.size()-1].first)
            continue;
        else {
            if (i == v1.size() - 2)
                ans += v1[i].second + v1[i + 1].second;
            else ans += v1[i].second;
        }
    }

    if (!ans){
        sort(v1.begin(), v1.end(),cmp);
        ans = v1[v1.size() - 1].second;
    }

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


# define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stdio.h>
#include <vector>
#include <cstdlib>
#include <string>
#include <map>
#include <ctime>
#include <list>
#include <stack>
#include <set>
#include <memory.h>
#include <deque>
#include <queue>
#include <cctype>
#include <iomanip>
#include <bitset>
#include <sstream>
typedef long long ll;

using namespace std;

# define pi 2*acos(0.)
# define e exp(1.)
# define N 1111

ll n, l, m, i, j, ans = 0, k, d, x, y;
ll a[N],b[N];
//string s;
vector <pair<ll, ll> >v1, v2;

bool cmp(pair<ll,ll> &p1, pair<ll, ll>&p2){
   if(p1.first != p2.first)  return p1.first<p2.first;
   else return p1.second < p2.second;
}

int main(){

	cin >> n >> d;
	for (i = 0; i < n; ++i)
	{
	cin >> x >> y;
	v1.push_back(make_pair(x, y));
	}

	sort(v1.begin(), v1.end(),cmp);

	ans = 0;
	for (i = 0; i < n-1; ++i)
	{
		ll sum = v1[i].second;
		j = i+1;
		bool x = true;
		while (j<n-1&&v1[j].first - v1[i].first < d){
			sum += v1[j].second;
			j++;
			if (j == n - 1 && v1[j].first - v1[i].first < d)x = false;
		}

		if (!x)sum += v1[j].second;

		ans = max(sum, ans);
	}

	ans = max(ans, v1[n - 1].second);

	cout << ans << endl;
	return 0;

}


# define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stdio.h>
#include <vector>
#include <cstdlib>
#include <string>
#include <map>
#include <ctime>
#include <list>
#include <stack>
#include <set>
#include <memory.h>
#include <deque>
#include <queue>
#include <cctype>
#include <iomanip>
#include <bitset>
#include <sstream>
typedef long long ll;

using namespace std;

# define pi 2*acos(0.)
# define e exp(1.)
# define N 111111

ll n, l, m, i, j, ans = 0, k, d, x, y;
ll a[N],b[N];
//string s;
pair<ll, ll> v1[N];

bool cmp(pair<ll,ll> &p1, pair<ll, ll>&p2){
   if(p1.first != p2.first)  return p1.first<p2.first;
   else return p1.second < p2.second;
}

int main(){

	cin >> n >> d;
	for (i = 1; i <= n; ++i)
	cin >> v1[i].first>> v1[i].second;

	sort(v1+1, v1+n+1,cmp);

	b[1] = v1[1].second;

	for (i = 1; i <= n; ++i)
		b[i] = b[i - 1] + v1[i].second;

	ans = 0;
	int q = 1;
	for (i = 2; i <= n; ++i)
	{

		if (v1[i].first - v1[q].first >= d){
			ans = max(b[i - 1] - b[q-1], ans);
			while (v1[i].first - v1[q].first >= d)
				q++;
		}
	}
		//if (!x)sum += v1[j].second;

	ans = max(b[n] - b[q-1], ans);

	cout << ans << endl;
	return 0;

}