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 <bits/stdc++.h>

using namespace std;
using i64 = long long int;
using ui64 = unsigned long long int;

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

int main()
{
    int n,m;
    vector<vector<int> > v;
    while(cin >> n >> m,n&&m){
        v.resize(m);
        for(int i=0;i<n;i++){
            int d,t;
            cin >> d >> t;
            v[--d].push_back(t);
        }
        for(auto &a:v)sort(a.begin(),a.end(),greater<int>());
        int res = 0;
        for(auto &a:v)res += a.front();
        cout << res << endl;
        v.clear();
    }
}

C++: #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(int)(n);i++)

int main(){
    int n,m;
    while(cin>> n >> m&&n!=0){
        vector<pair<int,int> > a;
        rep(i,n){
            int b,c;
            cin >> b >> c;
            a.push_back(make_pair(c,b));
        }
        sort(a.rbegin(),a.rend());
        vector<bool> p(m+1);
        int res = 0;
        rep(i,n){
            if(!p[a[i].second]){
                res += a[i].first;
                p[a[i].second] = 1;
            }
        }
        cout << res << "\n";
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define pp pair<int,int>
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define ll long long
#define ld long double
#define all(a) (a).begin(),(a).end()
#define mk make_pair
ll MOD=1000000007;
int inf=1000001000;
ll INF=100000000000000000;

int main() {
  while(true){
      int n,m;
      cin >> n >> m;
      if (n==0 && m==0) break;
      vector<int> a(n),b(n),c(m+1,0);
      rep(i,n) cin >> a[i] >> b[i];
      rep(i,n){
          c[a[i]]=max(c[a[i]],b[i]);
      }
      int ans=0;
      rep(i,m+1){
          ans+=c[i]; 
      }
      cout << ans << endl;
  }
}

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

int n, m, ans = 0;
int memo[105] = {0};

int main() {
  while(1) {
    cin >> n >> m;
    if(n + m == 0) break;
    ans = 0;
    for(int i = 0; i < n; ++i) {
      int d, v;
      cin >> d >> v;
      memo[d] = max(memo[d], v);
    }
    for(int i = 1; i <= m; ++i) ans += memo[i];
    cout << ans << endl;
    for(int i = 0; i <= m; ++i) memo[i] = 0;
  }
  return 0;
}

C++: #include<bits/stdc++.h>
#define N 1000
using namespace std;
using Int = long long;
Int mx[N];

signed main(){
  while(1){
    memset(mx,0,sizeof(mx));
    Int n,m;
    cin>>n>>m;
    if(!n&&!m)break;
    for(Int i=0;i<n;i++){
      Int d,v;
      cin>>d>>v;
      mx[d-1] = max(mx[d-1],v);
    }

    Int sum = 0;
    for(Int i=0;i<m;i++) sum+=mx[i];
    cout<<sum<<endl;
  }
  return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,a,b) for (int i = (a); i < (b); i++)
#define REP(i,n) rep(i,0,n)
using namespace std;

int main()
{
    int s[5];
  REP(z,5){
    
    int n,m;
    cin>>n>>m;
    vector<int>a(m);
    REP(i,n)
    {
        int d,v;
        cin>>d>>v;
        d--;
        if(v>a.at(d))
        {
            a.at(d)=v;
        }
    }
    int sum=0;
    REP(i,m)
    {
        sum+=a.at(i);
    }
    s[z]=sum;
  }
  REP(i,5){
    cout<<s[i]<<endl;
  }
}