#include <assert.h>
#include<stdio.h>
#include <vector>
#include <algorithm>

#define MAX 1000005

std::vector<int> mines[MAX];
int n;
int strt[MAX];  // Starting of the interval at field #i.

void cleanup() {
    for (int i = 0; i <= n+1; ++i) {
        mines[i].clear();
        strt[i] = 0;
    }
}

void read() {
    int m;
    scanf("%d%d", &n, &m);
    for(int a, b; m--; ) {
        scanf("%d%d", &a, &b);
        assert (a >= 1 && a <= n);
        if (b < a) continue;  // There is no way we will reach field `a` before time `b`, so recording this mine is pointless.
        mines[a].push_back(b);
    }
    for (int i = 1; i <= n; ++i)
        std::sort(mines[i].rbegin(), mines[i].rend());
}

int bomb(const int field, const int start, const int end) {
    // Find a bomb in field #field, between `start` and `end` time.
    auto& v = mines[field];
    while (!v.empty() && v.back() < start) v.pop_back();
    if (v.empty() || v.back() > end) {
        return 0;
    }
    return v.back();
}

int solve() {
    // `pos` tracks the position you are currently on.
    // `time` tracks the end of the time interval you must spend at the current position (tracked by `pos`).
    int pos = 0, time = 0;  
    strt[pos] = 0;  // `strt[i]` tracks the start of the time interval you must spend at position `i`.
    while (pos < n + 1) {
        const int b = bomb(pos, strt[pos], time);
        if (b) {
            // There is a bomb in this field during this range. It detonates at time `b`.
            // Take one step back, and make sure to stay there until time `b`.
            pos = pos - 1;
            time = b;
        } else {
            // No bomb in this field during the interval you intend to spend here.
            // Advance one step, and increase the time by 1.
            pos = pos + 1;
            time = time + 1;
            assert(strt[pos] < time);
            strt[pos] = time;
        }
    }
    return time;
}

int main() {
    int TC;
    scanf("%d",&TC);
    while(TC--) {
        read();
        printf("%d\n", solve());
        cleanup();
    }
    return 0;
}