Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	io := NewIo()
	defer io.Flush()
	for {
		n, m := io.NextInt(), io.NextInt()
		if n == 0 {
			break
		}
		p := make([][]int, m)
		for i := 0; i < n; i++ {
			d, v := io.NextInt()-1, io.NextInt()
			p[d] = append(p[d], v)
		}
		sum := 0
		for i := 0; i < m; i++ {
			max := 0
			for _, v := range p[i] {
				max = intMax(v, max)
			}
			sum += max
		}
		io.Println(sum)
	}
}

func intMax(a, b int) int {
	if a < b {
		return b
	}
	return a
}

// I/O template
type Io struct {
	reader    *bufio.Reader
	writer    *bufio.Writer
	tokens    []string
	nextToken int
}

func NewIo() *Io {
	return &Io{
		reader: bufio.NewReader(os.Stdin),
		writer: bufio.NewWriter(os.Stdout),
	}
}

func (io *Io) Flush() {
	err := io.writer.Flush()
	if err != nil {
		panic(err)
	}
}

func (io *Io) NextLine() string {
	var buff []byte
	for {
		line, isPrefix, err := io.reader.ReadLine()
		if err != nil {
			panic(err)
		}
		buff = append(buff, line...)
		if !isPrefix {
			break
		}
	}
	return string(buff)
}

func (io *Io) Next() string {
	for io.nextToken >= len(io.tokens) {
		line := io.NextLine()
		io.tokens = strings.Fields(line)
		io.nextToken = 0
	}
	s := io.tokens[io.nextToken]
	io.nextToken++
	return s
}

func (io *Io) NextInt() int {
	i, err := strconv.Atoi(io.Next())
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) NextFloat() float64 {
	i, err := strconv.ParseFloat(io.Next(), 64)
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) Println(a ...interface{}) {
	fmt.Fprintln(io.writer, a...)
}

func (io *Io) Printf(format string, a ...interface{}) {
	fmt.Fprintf(io.writer, format, a...)
}

func (io *Io) PrintIntSlice(a []int) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func (io *Io) PrintStringSlice(a []string) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func Log(name string, value interface{}) {
	fmt.Fprintf(os.Stderr, "%s=%+v\n", name, value)
}

func ExampleIo() {
	io := NewIo()
	defer io.Flush()
	a := make([]int, 0, 3)
	a[0], a[1], a[2] = io.NextInt(), io.NextInt(), io.NextInt()
	io.Println(a)
	io.PrintIntSlice(a)
	Log("io", a)
}

C++: #include<iostream>
#include<algorithm>
#include<string>
#include<cstdlib>
#include<map>
#include<iomanip>
#include<sstream>
#include<vector>
#include<stack>
#include<math.h>
#include<queue>
#include<complex>
using namespace std;

struct TWO{
    long long int a,b;
};

bool cmp(TWO x,TWO y){
    if(x.a!=y.a){
        return x.a<y.a;
    }else{
        return x.b<y.b;
    }
}

int main() {
    const long long int mod = 1000000007,INF=50000000000000;
    typedef pair<long long int,long long int>P;
	map<long long int, long long int>mp;
    priority_queue<P,vector<P>,greater<P> >mazuisushi;
    
    priority_queue<P>umaisushi;
	cout << fixed << setprecision(10);
    string str;
    long long int y,m,d;

    while(1){
      int n, m;
      cin >> n >> m;
      if (n == 0) return 0;
      vector<int> a(n), b(n);
      for (int i = 0; i < n; i++) cin >> a[i] >> b[i];
      map<int, priority_queue<int> > mp;
      for (int i = 0; i < n; i++) {
          mp[a[i]].push(b[i]);
      }
      int res = 0;
      for (int i = 1; i <= m; i++) {
          res += mp[i].top();
      }
      cout << res << endl;
    }
}

C++: #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define INF (1e9)
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define all(x) x.begin(),x.end()
#define ln '\n'
const long double PI = acos(-1.0L);
const long long MOD = 1000000007LL;
//const long long MOD = 998244353LL;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
template<class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true;} return false; }
template<class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true;} return false; }
///////////////////////////////////////////////////////////////////////////////////////////////////

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    while (1) {
        int N,M; cin >> N >> M;
        if (N+M == 0) break;
        vector<int> d(N),v(N),A(M+1);
        rep(i,N) cin >> d[i] >> v[i];

        rep(i,N) {
            chmax(A[d[i]],v[i]);
        }

        cout << accumulate(all(A),0) << ln;
    }
}

C++: #include <iostream>
#include <cstdio>
#include <string.h>
#define NMMAX 100
using namespace std;
int main(void)
{
  while (1) {
    int n, m, asum = 0, recommend[NMMAX] = {0};
    scanf("%d %d", &n, &m);
    if (n == 0 && m == 0) break;
    for (int i = 0; i < n; i++) {
      int d, v;
      scanf("%d %d", &d, &v);
      if (recommend[d-1] < v) recommend[d-1] = v;
    }
    for (int i = 0; i < m; i++) asum += recommend[i];
    printf("%d\n", asum);
  }
}

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

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for(int64 i = 0;i < (n);i++)
#define FOR(i, a, b) for(int64 i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}
template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}

template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
  return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value!=0>::type
fill_v(U &u,const V... v){u=U(v...);}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value==0>::type
fill_v(U &u,const V... v){
  for(auto &e:u) fill_v<T>(e,v...);
}

int main(void) {
	int64 N, M;
	while (cin >> N >> M && N+M) {
		auto pro = make_v<int64>(M, 0);
		REP(i, N) {
			int64 d, v;
			cin >> d >> v; d--;
			pro[d].push_back(v);
		}
		int64 res = 0;
		REP(i, M) {
			sort(all(pro[i]));
			res += pro[i].back();
		}
		cout << res << endl;
	}
}

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

//#define int long long
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)
template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; }

int main() {
	for (int N, M; cin >> N >> M&&N;) {
		vector<int> d(N), v(N);
		vector<int> a(M);
		rep(i, 0, N) {
			cin >> d[i] >> v[i];
			d[i]--;
			chmax(a[d[i]], v[i]);
		}
		int ans = 0;
		rep(i, 0, M) {
			ans += a[i];
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;

int main(){
  int N, M;
  while(cin >> N >> M,N){
    vector<int> V(M);
    for(int i = 0; i < N; ++i){
      int d, v;
      cin >> d >> v;
      --d;
      V[d] = max(V[d],v);
    }
    int ans = 0;
    for(auto v: V) ans += v;
    cout << ans << endl;
  }
}

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

#define en '\n'
using ll = long long;
template <class T>
using vec = vector<T>;
template <class T>
using vvec = vec<vec<T>>;
#define REP(i, n) for(int(i) = 0; (i) < (n); (i)++)
#define all(hoge) hoge.begin(), hoge.end()

int main(void) {
    int n, m;

    while(cin >> n >> m) {
        if(n == 0 and m == 0)
            break;

        vvec<int> v(m);
        REP(i, n) {
            int dd, vv;
            cin >> dd >> vv;
            dd--;
            v[dd].push_back(vv);
        }

        int ans = 0;
        REP(i, m) {
            sort(all(v[i]));
            ans += v[i].back();
        }
        cout << ans << en;
    }
    return 0;
}