// Arup Guha
// 10/4/2021
// Solution to COP 3502 Program #3: Winning the Lottery

#include <stdio.h>
#include <stdlib.h>

#define MAXGROUPS 10

// Node to store one person.
typedef struct node {
    int personID;
    struct node* next;
} node;

// How we store a circular linked list.
typedef struct circularLL {
    node* front;
    node* back;
    node* cur;
    int size;
} circularLL;

// Node Functions
node* createNode(int val);
node* insertFront(node* listPtr, int value);

// Linked List Functions
circularLL* updateList(int n);
int removeNext(circularLL* list, int skip);
void doPhase1(circularLL* list, int numRemove, int skip);
void freeList(circularLL* list);

int main(void) {

    int numCases;
    scanf("%d", &numCases);

    // Process each case.
    for (int loop=0; loop<numCases; loop++) {

        // Read in the number of groups.
        int numGroups;
        scanf("%d", &numGroups);

        // Create room for each pointer.
        circularLL** groups = malloc(numGroups*sizeof(circularLL*));

        // Go through each group.
        for (int i=0; i<numGroups; i++) {

            // Read in the group information.
            int people, skip, threshold;
            scanf("%d%d%d", &people, &skip, &threshold);

            // First create the list.
            groups[i] = updateList(people);

            // Print header.
            printf("Group #%d:\n", i+1);

            // Do the phase!
            doPhase1(groups[i], people-threshold, skip);
        }

        // Our default answer.
        int res = groups[0]->front->personID;
        int resGroup = 1;

        // Now see if anything is better.
        for (int i=1; i<numGroups; i++) {
            if (groups[i]->front->personID < res) {
                res = groups[i]->front->personID;
                resGroup = i+1;
            }
        }

        // Ta da!
        printf("Lottery winner is person %d from group %d.\n", res, resGroup);

        // Free memory for nodes and pointers to lists.
        for (int i=0; i<numGroups; i++) {
            freeList(groups[i]);
            free(groups[i]);
        }

        // Now free the array.
        free(groups);
    }

    return 0;
}

// Creates a node storing value and returns a pointer to it.
node* createNode(int value) {
    node* tmp = malloc(sizeof(node));
    tmp->personID = value;
    tmp->next = NULL;
    return tmp;
}

// Inserts a node storing value in front of the list pointed to by
// listPtr and returns a pointer to the new front of the list.
node* insertFront(node* listPtr, int value) {
    node* tmp = createNode(value);
    tmp->next = listPtr;
    return tmp;
}

// Returns a pointer to a circular linked list with n nodes, values
// 1 to n, with 1 at the front. n > 1, so the resulting list has at
// least 2 values.
circularLL* updateList(int n) {

    circularLL* myList = malloc(sizeof(circularLL));

    // Hard code last two.
    myList->back = createNode(n);
    myList->front = createNode(n-1);
    myList->front->next = myList->back;

    // Now loop through the rest.
    for (int i=n-2; i>=1; i--)
        myList->front = insertFront(myList->front, i);

    // We need the current point to start at the end.
    myList->cur = myList->back;

    // This is what makes it circular.
    myList->back->next = myList->front;

    // Set up the size.
    myList->size = n;

    // return the pointer to the list.
    return myList;
}

// Pre-condition: The circular linked list pointed to by list contains at least 2 elements.
// Post-condition:Skips skip # of nodes from the current location and deletes the
// next node, and then returns the value that was stored at that node.
int removeNext(circularLL* list, int skip) {

    // We play, duck, duck...skip over the people we won't remove.
    for (int i=0; i<skip; i++)
        list->cur = list->cur->next;

    // The node to delete.
    node* delnode = list->cur->next;

    // The node we patch to.
    node* patch = delnode->next;

    // If we were to delete the front node, the new front is patch.
    if (delnode == list->front)
        list->front = patch;

    // If we were to delete the back node, the new back is cur.
    if (delnode == list->back)
        list->back = list->cur;

    // The value we will return.
    int retval = delnode->personID;

    // We can free this memory now.
    free(delnode);

    // Patch it up!
    list->cur->next = patch;

    // Update size.
    list->size = list->size - 1;

    // Now we can return.
    return retval;
}

// Executes phase 1 on the group pointed to by list. numRemove is
// how many people will be removed and skip is the skip number for
// the group. It's guaranteed that numRemove is less than the size
// of the list.
void doPhase1(circularLL* list, int numRemove, int skip) {

    // Just repeat numRemove number of times!
    for (int i=0; i<numRemove; i++)
        printf("%d\n", removeNext(list, skip));
}

void freeList(circularLL* list) {

    // How many nodes we have to free.
    int delCount = list->size;

    // Go through and free each one.
    for (int i=0; i<delCount; i++) {
        node* tmp = list->front->next;
        free(list->front);
        list->front = tmp;
    }

    // Update to be empty.
    list->front = NULL;
    list->back = NULL;
    list->cur = NULL;
    list->size = 0;
}
