#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>

// I/O
void put_uint(int n) {
    if(!n) {
        putchar_unlocked('0');
        return;
    }
    char buf[11];
    int i = 0;
    while(n) buf[i++] = (char)(n % 10 + '0'), n /= 10;
    while(i--)putchar_unlocked(buf[i]);
}

void put_str(char *str) { while(*str) putchar_unlocked(*str++); }

int get_uint() {
    int n = 0;
    int c = getchar_unlocked();
    if(c < 48 || 57 < c) return c;
    while(47 < c && c < 58) n = 10 * n + (c & 0xf), c = getchar_unlocked();
    return n;
}

int get_str(char *str) {
    int c;
    while((c = getchar_unlocked()) > 32) *str++ = (char)c;
    *str = 0;
    return c;
}
//


// Util
#define container_of(ptr, type, member) ({  \
    void *__mptr = (void *)(ptr);           \
    ((type *)(__mptr - offsetof(type, member))); })
//


// List
struct list_head {
    struct list_head *next, *prev;
};

static inline void INIT_LIST_HEAD(struct list_head *list) {
    list->next = list;
    list->prev = list;
}

static inline void __list_add(struct list_head *new, struct list_head *prev, struct list_head *next) {
    next->prev = new;
    new->next = next;
    new->prev = prev;
    prev->next = new;
}

static inline void list_add(struct list_head *new, struct list_head *head) {
    __list_add(new, head, head->next);
}

static inline void list_add_tail(struct list_head *new, struct list_head *head) {
    __list_add(new, head->prev, head);
}

static inline void __list_del(struct list_head *prev, struct list_head *next) {
    next->prev = prev;
    prev->next = next;
}

static inline void __list_del_entry(struct list_head *entry) {
    __list_del(entry->prev, entry->next);
}

static inline void list_del(struct list_head *entry) {
    __list_del_entry(entry);
    entry->next = NULL;
    entry->prev = NULL;
}

static inline void list_move_tail(struct list_head *list, struct list_head *head) {
    __list_del_entry(list);
    list_add_tail(list, head);
}

static inline int list_empty(const struct list_head *head) {
    return head->next == head;
}

#define list_entry(ptr, type, member) container_of(ptr, type, member)
#define list_first_entry(ptr, type, member) list_entry((ptr)->next, type, member)
//


// my node
struct my_node {
    char str[16];
    int num;
    struct list_head list;
};
//


int main(int argc, char **argv) {
    struct list_head head;
    INIT_LIST_HEAD(&head);

    int n = get_uint();
    int q = get_uint();
    for(int i = 0; i < n; ++i) {
        struct my_node *data = malloc(sizeof(struct my_node));
        get_str(data->str);
        data->num = get_uint();
        list_add_tail(&data->list, &head);
    }

    int cnt = 0;
    while(!list_empty(&head)) {
        struct my_node *data = list_first_entry(&head, struct my_node, list);
        if(data->num > q) {
            cnt += q;
            data->num -= q;
            list_move_tail(&data->list, &head);
        } else {
            cnt += data->num;
            put_str(data->str);
            putchar_unlocked(' ');
            put_uint(cnt);
            putchar_unlocked('\n');
            list_del(&data->list);
            free(data);
        }
    }
    return 0;
}



Measured memory_bytes: 3876