Go: package main

import (
	"bytes"
	"fmt"
)

//Ktai has a keyboard and a state.
type Ktai struct {
	keyboard KeyBoard
	textBuf  *bytes.Buffer
}

// KeyBoard is type of a map.
type KeyBoard map[rune]string

// KEYBOARD that Ktai has
var KEYBOARD = KeyBoard{
	'0': "",
	'1': ".,!? ",
	'2': "abc",
	'3': "def",
	'4': "ghi",
	'5': "jkl",
	'6': "mno",
	'7': "pqrs",
	'8': "tuv",
	'9': "wxyz",
}

func main() {
	var N int
	fmt.Scanf("%d", &N)
	ktai := &Ktai{
		KEYBOARD, &bytes.Buffer{},
	}
	for i := 0; i < N; i++ {
		var numstr string
		fmt.Scanf("%s", &numstr)
		ktai.feed(numstr)
		ktai.print()
	}
}

func (ktai *Ktai) feed(str string) {
	var char *byte
	for _, v := range str {
		if v == '0' {
			if char != nil {
				ktai.textBuf.WriteByte(*char)
				char = nil
			}
			continue
		} else {
			runestring := ktai.keyboard[v]
			idx := ktai.indexOf(char, runestring)
			next := (idx + 1) % len(runestring)
			c := runestring[next]
			char = &c
		}
	}
}

func (ktai *Ktai) indexOf(ch *byte, str string) int {
	if ch == nil {
		return -1
	}
	r := rune(*ch)
	idx := -1
	for i, v := range str {
		if r == v {
			idx = i
			break
		}
	}
	return idx
}

func (ktai *Ktai) print() {
	fmt.Println(ktai.textBuf.String())
	ktai.textBuf.Reset()
}

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

int main(){

  int n, cnt = 0, num = 0;
  string str, phone;

  cin >> n;

  for(int i = 0; i < n; i++){
    cin >> phone;
    for(int j = 0; j < (int)phone.size(); j++){
      if(phone[j] == '1'){
	cnt++;
	if(phone[j+1] == '0'){
	  switch (cnt % 5){
	  case 0: 
	    cout << " ";
	    break;
	  case 4: 
	    cout << '?';
	    break;
	  case 3: 
	    cout << '!';
	    break;
	  case 2: 
	    cout << ',';
	    break;
	  case 1: 
	    cout << '.';
	    break;
	  }
	  cnt = 0;
	}
      }else if(phone[j] == '2'){
	cnt++;
	if(phone[j+1] == '0'){
	  switch (cnt % 3){
	  case 0: 
	    cout << 'c';
	    break;
	  case 2: 
	    cout << 'b';
	    break;
	  case 1: 
	    cout << 'a';
	    break;
	  }
	  cnt = 0;
	}
      }else if(phone[j] == '3'){
	cnt++;
	if(phone[j+1] == '0'){
	  switch (cnt % 3){
	  case 0: 
	    cout << 'f';
	    break;
	  case 2: 
	    cout << 'e';
	    break;
	  case 1: 
	    cout << 'd';
	    break;
	  }
	  cnt = 0;
	}
      }else if(phone[j] == '4'){
	cnt++;
	if(phone[j+1] == '0'){
	  switch (cnt % 3){
	  case 0: 
	    cout << 'i';
	    break;
	  case 2: 
	    cout << 'h';
	    break;
	  case 1: 
	    cout << 'g';
	    break;
	  }
	  cnt = 0;
	}
      }else if(phone[j] == '5'){
	cnt++;
	if(phone[j+1] == '0'){
	  switch (cnt % 3){
	  case 0: 
	    cout << 'l';
	    break;
	  case 2: 
	    cout << 'k';
	    break;
	  case 1: 
	    cout << 'j';
	    break;
	  }
	  cnt = 0;
	}
      }else if(phone[j] == '6'){
	cnt++;
	if(phone[j+1] == '0'){
	  switch (cnt % 3){
	  case 0: 
	    cout << 'o';
	    break;
	  case 2: 
	    cout << 'n';
	    break;
	  case 1: 
	    cout << 'm';
	    break;
	  }
	  cnt = 0;
	}
      }else if(phone[j] == '7'){
	cnt++;
	if(phone[j+1] == '0'){
	  switch (cnt % 4){
	  case 0: 
	    cout << 's';
	    break;
	  case 3: 
	    cout << 'r';
	    break;
	  case 2: 
	    cout << 'q';
	    break;
	  case 1: 
	    cout << 'p';
	    break;
	  }
	  cnt = 0;
	}
      }else if(phone[j] == '8'){
	cnt++;
	if(phone[j+1] == '0'){
	  switch (cnt % 3){
	  case 0: 
	    cout << 'v';
	    break;
	  case 2: 
	    cout << 'u';
	    break;
	  case 1: 
	    cout << 't';
	    break;
	  }
	  cnt = 0;
	}
      }else if(phone[j] == '9'){
	cnt++;
	if(phone[j+1] == '0'){
	  switch (cnt % 4){
	  case 0: 
	    cout << 'z';
	    break;
	  case 3: 
	    cout << 'y';
	    break;
	  case 2: 
	    cout << 'x';
	    break;
	  case 1: 
	    cout << 'w';
	    break;
	  }
	  cnt = 0;
	}
      }
    }
    cout << endl;
  }









return 0;
}

C++: #include<stdio.h>
#include<iostream>
#include<list>
#include<map>
#include<math.h>
#include<vector>
#include<algorithm>
#include<string.h>
#include<iomanip>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) range(i,0,b)
#define debug(x) cout << "debug" << x << endl;
using namespace std;

int main(){
    string inp;
    int n;
    cin >> n;
    while(n--){
        cin >> inp;
            inp.append(1, '0');
        bool head = true;
        char headNum;
        int count;
        for(int i = 0; inp[i]; i++){
            if((inp[i - 1] == '0' && inp[i] == '0') || (i == 0 && inp[i] == '0')) continue;
            if(head){
                headNum = inp[i];
                count = 0;
                head = false;
            }
            if(headNum == inp[i] && headNum != '0'){
                count++;
            }else{
                char outp;
                if(headNum == '1'){
                    count %= 5;
                    if(count == 0) count = 5;
                    if(count == 1) outp = '.';
                    else if(count == 2) outp =',';
                    else if(count == 3) outp ='!';
                    else if(count == 4) outp ='?';
                    else if(count == 5) outp =' ';
                }else if(headNum >= '2' && headNum <= '6'){
                    count %= 3;
                    if(count == 0) count = 3;
                    outp = 'a' - 1 + count + (headNum - '2') * 3;
                }else if(headNum == '7'){
                    count %= 4;
                    if(count == 0) count = 4;
                    outp = 'p' - 1 + count;
                }else if(headNum == '8'){
                    count %= 3;
                    if(count == 0) count = 3;
                    outp = 't' - 1 + count;
                }else if(headNum == '9'){
                    count %= 4;
                    if(count == 0) count = 4;
                    outp = 'w' - 1 + count;
                }
                cout.put(outp);

                if(inp[i] - '0'){
                    i--;
                }
                head = true;
                continue;
            }
        }
        cout << endl;
    }
}