#include #include #include #include #include #include using namespace std; int a[1005]; int b[1005]; struct zjc { int cost, pos, finsh; bool operator < (const zjc & a) const { return cost > a.cost; } }; int main() { int T; scanf("%d", &T); for (int tt = 0; tt < T; ++tt) { int n; scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%d%d", &a[i], &b[i]); } for (int i = 1; i < n; ++i) { if (a[i]>=a[0] && a[i]<= b[0]) { a[0] = a[i]; } else if (b[i]>=a[0] && b[i] <=b[0]) { b[0] = b[i]; } else break; } zjc aa,bb; aa.cost = 0; aa.pos = a[0]; aa.finsh=1; bb.cost = 0; bb.pos = b[0]; bb.finsh=1; priority_queue pp; pp.push(aa); pp.push(bb); map, int> mp; //mp[pair(0,a[0])] = 1; //mp[pair(0,a[0])] = 1; while(!pp.empty()) { zjc now = pp.top(); pp.pop(); // cout << now.cost << " " << now.pos <= lll && pos <= rrr) { pp.push(next); } else if (pos < lll) { int dis = lll - pos; next.cost += (dis+1)/2; next.pos = lll; pp.push(next); if (dis%2 && rrr>lll) { next.pos = lll+1; pp.push(next); } } else if (pos > rrr) { int dis = pos -rrr; next.cost += (dis+1)/2; next.pos = rrr; pp.push(next); if (dis%2 && rrr > lll) { next.pos = rrr -1; pp.push(next); } } } } return 0; }