#include<bits/stdc++.h> usingnamespace std; constint N = 1e3 + 10; constint INF = 0x3f3f3f3f; int a[N], b[N]; int f[N][1 << 8][16]; voidchkmin(int &x, int v) { x = min(x, v); } intcal(int old, int new_) { if (old == 0) return0; return a[old] ^ a[new_]; } intmain() { int T; cin >> T; while (T--) {
int n; cin >> n; for (int i = 1; i <= n; ++i) cin >> a[i] >> b[i]; memset(f, INF, sizeof f); f[1][0][8 - 1] = 0; for (int i = 1; i <= n; ++i) { for (int st = 0; st < 1 << 8; ++st) { for (int k = -8; k <= 7; ++k) // old { if (f[i][st][8 + k] >= INF) continue; if (st & 1) chkmin(f[i + 1][st >> 1][8 + k - 1], f[i][st][8 + k]); else { int r = INF; for (int l = 0; l <= 7; ++l) { if(st & (1 << l)) continue; if (i + l > r) break; chkmin(r, i + l + b[i + l]); chkmin(f[i][st ^ (1 << l)][8 + l], f[i][st][8 + k] + cal(i + k, i + l)); } } } } } int ans = INF; for (int i = -8; i <= 0; ++i) chkmin(ans, f[n][1][8 + i]); cout << ans << endl; } }