Help me in solving BMCCP15A problem

My issue

can someone explain rem = n % m; this part
n = m;
m = rem;

My code

// Solution as follows

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

int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        int n, m, rem;
        scanf("%d %d", &n, &m);
        while (m != 0) {
            rem = n % m;
            n = m;
            m = rem;
        }
        printf("%d\n", n);
    }
    return 0;
}

Learning course: C for problem solving - 2
Problem Link: CodeChef: Practical coding for everyone

@bhundbuster
Its a algorithm to find the gcd of two number in log(n) time complexity.