malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int n, m;   const int maxn = 1e5 + 3;   int terminal[maxn];   struct spe {     int next, to;   };   spe edge[maxn << 1];   int head[maxn];   int visit[maxn][3];   int cnt = 1, tpoint = -1;   int nxt[maxn][2];   bool dp[maxn][3];   int cir = 0;   bool dfs(int s, int val) {     if (terminal[s] == 1) {       tpoint = s;       return val == 1;     }     if (visit[s][val] == 2) return dp[s][val];     bool temp = false;     visit[s][val] = 1;     for (int i = head[s]; i != -1; i = edge[i].next) {       int t = edge[i].to;       if (visit[t][!val] == 1) {         cir = 1;         continue;       }       if (dfs(t, !val) == true) {         nxt[s][val] = t;         temp = true;       }     }     visit[s][val] = 2;     dp[s][val] = temp;     return temp;   }   int main() {     memset(dp, 0, sizeof dp);     memset(visit, 0, sizeof visit);     memset(head, -1, sizeof head);     memset(edge, 0, sizeof edge);     memset(terminal, 0, sizeof terminal);     scanf( %d %d , &n, &m);     for (int i = 1; i <= n; i++)       for (int j = 0; j <= 1; j++) nxt[i][j] = i;     int counter = 0;     for (int i = 1; i <= n; i++) {       int k, item;       scanf( %d , &k);       if (k == 0) counter++, terminal[i] = 1;       while (k--) {         scanf( %d , &item);         edge[cnt].to = item;         edge[cnt].next = head[i];         head[i] = cnt++;       }     }     int start;     scanf( %d , &start);     bool ans = dfs(start, 0);     if (ans == true) {       printf( Win n );       int j = 0;       printf( %d , start);       while (nxt[start][j] != start) {         start = nxt[start][j];         j = !j;         printf(  %d , start);       }       printf(  n );     } else if (cir == 1)       printf( Draw n );     else       printf( Lose n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 100;   const int maxm = 100;   const int maxlen = maxn + maxm - 1;   struct point {     int ind, p;     bool operator<(const point& b) const { return p < b.p; }   } cell[maxlen];   char state[maxlen];   int n, m;   long long k;   int p;   long long dp[maxlen][maxlen];   long long Cnt() {     memset(dp, 0, sizeof(dp));     if (state[0] ==  ) ) return 0;     dp[0][1] = 1;     for (int i = 1; i <= (n - 1) + (m - 1); ++i) {       for (int j = 0; j <= (n - 1) + (m - 1); ++j) {         if (state[i] == 0) {           if (j > 0) dp[i][j] = dp[i - 1][j - 1];           dp[i][j] += dp[i - 1][j + 1];         } else if (state[i] ==  ( ) {           if (j > 0) dp[i][j] = dp[i - 1][j - 1];         } else           dp[i][j] = dp[i - 1][j + 1];         if (dp[i][j] > k) dp[i][j] = k + 1;       }     }     return dp[(n - 1) + (m - 1)][0];   }   int main() {     ios_base::sync_with_stdio(false);     cin >> n >> m >> k;     for (int i = 0; i <= (n - 1) + (m - 1); ++i)       cell[i].ind = i, cell[i].p = n * m + 1;     for (int i = 0; i < n; ++i)       for (int j = 0; j < m; ++j) {         cin >> p;         cell[i + j].p = min(cell[i + j].p, p);       }     sort(cell, cell + n + m - 1);     for (int i = 0; i <= (n - 1) + (m - 1); ++i) {       state[cell[i].ind] =  ( ;       long long num = Cnt();       if (num < k) {         k -= num;         state[cell[i].ind] =  ) ;       }     }     for (int i = 0; i < n; ++i) {       for (int j = 0; j < m; ++j) cout << state[i + j];       cout << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int x, y, z, a, b;     cin >> x >> y >> z;     a = max(x, y);     b = min(x, y);     if (b + z < a || z == 0) {       if (x > y)         cout <<  + ;       else if (x < y)         cout <<  - ;       else         cout <<  0 ;     } else if (b + z >= a)       cout <<  ? ;   }
#include <bits/stdc++.h>   const int mod = 1000000007, hmod = 10000019;   int n, a[1000010], p[1000010], f[1000010];   long long sz[1000010];   int g[1000010];   long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , a + i), sz[i] = a[i];     for (int i = 2; i <= n; i++) scanf( %d , p + i);     for (int i = n; i > 1; i--) sz[p[i]] += sz[i];     for (int i = 1; i <= n; i++) {       long long j = sz[1] / gcd(sz[1], sz[i]);       if (j <= n) g[j]++;     }     for (int i = n; i; i--)       for (int j = i * 2; j <= n; j += i) g[j] += g[i];     for (int i = n; i >= 1; i--)       if (g[i] >= i) {         f[i] = 1;         for (int j = i * 2; j <= n; j += i) (f[i] += f[j]) %= mod;       }     printf( %d n , f[1]);   }
#include <bits/stdc++.h>   using namespace std;   template <typename T>   inline void read(T &val) {     val = 0;     char c = getchar();     int f = 1;     while (!isdigit(c)) {       if (c ==  - ) f = -1;       c = getchar();     }     while (isdigit(c)) {       val = (val << 3) + (val << 1) + (c ^ 48);       c = getchar();     }     val *= f;   }   template <typename T>   void write_(T val) {     if (!val) return;     write_(val / 10);     putchar(val % 10 + 48);   }   template <typename T>   inline void write(T val) {     if (val == 0)       putchar(48);     else if (val < 0) {       putchar( - );       write_(-val);     } else       write_(val);   }   const int N = 4e5 + 5;   struct segment {     long long l, r;   } s[N];   int n;   long long ans0, ans1, tmp[N << 1], d_[N << 2], d[N << 2];   int main() {     ios::sync_with_stdio(false);     int T;     cin >> T;     while (T--) {       ans0 = 0;       ans1 = INT_MIN;       cin >> n;       for (register int i = 1; i <= n; i++) {         cin >> s[i].l >> s[i].r;         tmp[(i << 1) - 1] = s[i].l;         tmp[i << 1] = s[i].r;       }       sort(tmp + 1, tmp + (n << 1) + 1);       int tot = unique(tmp + 1, tmp + (n << 1) + 1) - tmp - 1;       for (register int i = 1; i <= n; i++) {         s[i].l = lower_bound(tmp + 1, tmp + tot + 1, s[i].l) - tmp;         s[i].r = lower_bound(tmp + 1, tmp + tot + 1, s[i].r) - tmp;         s[i].l <<= 1LL;         s[i].r <<= 1LL;         d[s[i].l]++;         d[s[i].r + 1]--;       }       tot <<= 1LL;       for (register long long i = 1; i <= tot + 5; i++) d[i] += d[i - 1];       for (register long long i = 0; i <= tot + 5; i++) ans0 += d[i] && !d[i + 1];       for (register long long i = 0; i <= tot + 4; i++)         if (d[i] == 1 && d[i + 1] != 1) d_[i]++;       for (register long long i = 1; i <= tot + 5; i++)         if (d[i] == 1 && d[i - 1] != 1) d_[i]++;       for (register long long i = 1; i <= tot + 5; i++) d_[i] += d_[i - 1];       for (register int i = 1; i <= n; i++) {         long long t = d_[s[i].r] - d_[s[i].l - 1];         t /= 2;         t -= (d[s[i].r] == 1) + (d[s[i].l] == 1);         ans1 = max(ans1, t);       }       cout << ans0 + ans1 << endl;       for (register long long i = 0; i <= tot + 10; i++) d[i] = d_[i] = 0;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   stack<int> a[27];   stack<int> b[27];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     int n;     cin >> n;     string f, s;     cin >> f >> s;     for (int i = 0; i < f.size(); ++i) {       if (f[i] ==  ? ) {         a[26].push(i + 1);       } else {         a[(int)(f[i] -  a )].push(i + 1);       }     }     for (int i = 0; i < s.size(); ++i) {       if (s[i] ==  ? ) {         b[26].push(i + 1);       } else {         b[(int)(s[i] -  a )].push(i + 1);       }     }     int ans = 0;     vector<pair<int, int> > anss;     for (int i = 0; i < 27; ++i) {       while ((a[i].size() != 0 && b[i].size() != 0) ||              (a[i].size() != 0 && b[26].size() != 0) ||              (a[26].size() != 0 && b[i].size() != 0)) {         if (a[i].size() != 0 && b[i].size() != 0) {           int c1, c2;           c1 = a[i].top();           c2 = b[i].top();           ans++;           a[i].pop();           b[i].pop();           anss.push_back({c1, c2});         } else if (a[i].size() != 0 && b[i].size() == 0) {           if (b[26].size() != 0) {             int c1, c2;             c1 = a[i].top();             c2 = b[26].top();             ans++;             a[i].pop();             b[26].pop();             anss.push_back({c1, c2});           }         } else if (a[i].size() == 0 && b[i].size() != 0) {           if (a[26].size() != 0) {             int c1, c2;             c1 = a[26].top();             c2 = b[i].top();             ans++;             a[26].pop();             b[i].pop();             anss.push_back({c1, c2});           }         }       }     }     cout << ans << endl;     for (int i = 0; i < anss.size(); ++i) {       cout << anss[i].first <<     << anss[i].second << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   inline int read() {     int x = 0, f = 1;     char c = getchar();     while (!isdigit(c)) {       if (c ==  - ) f = -1;       c = getchar();     }     while (isdigit(c)) {       x = (x << 1) + (x << 3) + (c ^ 48);       c = getchar();     }     return f == 1 ? x : -x;   }   const int N = 3e5 + 4;   long long ans = 0;   int n, a[N], mx[N], mn[N], cnt[N << 1];   inline void solve(int l, int r) {     if (l == r) return ++ans, void();     int mid = l + r >> 1;     mx[mid] = mn[mid] = a[mid];     mx[mid + 1] = mn[mid + 1] = a[mid + 1];     for (int i = mid + 2; i <= r; i++) {       mx[i] = (mx[i - 1] > a[i] ? mx[i - 1] : a[i]);       mn[i] = (mn[i - 1] > a[i] ? a[i] : mn[i - 1]);     }     for (int i = mid - 1; i >= l; i--) {       mx[i] = (mx[i + 1] > a[i] ? mx[i + 1] : a[i]);       mn[i] = (mn[i + 1] > a[i] ? a[i] : mn[i + 1]);     }     int ll = mid, rr = mid;     for (int i = mid + 1, x; i <= r; i++) {       x = i - mx[i] + mn[i];       if (x >= l && x <= mid && mx[x] < mx[i] && mn[x] > mn[i]) ans++;       while (ll >= l && mx[ll] < mx[i]) {         cnt[N + mn[ll] - ll]++;         ll--;       }       while (rr > ll && mn[rr] > mn[i]) {         cnt[N + mn[rr] - rr]--;         rr--;       }       ans += cnt[N + mx[i] - i];     }     while (rr != ll) {       cnt[N + mn[rr] - rr]--;       rr--;     }     ll = rr = mid + 1;     for (int i = mid, x; i >= l; i--) {       x = mx[i] - mn[i] + i;       if (x > mid && x <= r && mx[x] < mx[i] && mn[x] > mn[i]) ans++;       while (rr <= r && mx[rr] < mx[i]) {         cnt[mn[rr] + rr]++;         rr++;       }       while (ll < rr && mn[ll] > mn[i]) {         cnt[mn[ll] + ll]--;         ll++;       }       ans += cnt[mx[i] + i];     }     while (ll != rr) {       cnt[mn[ll] + ll]--;       ll++;     }     solve(l, mid);     solve(mid + 1, r);   }   int main() {     n = read();     for (int i = 1, u, v; i <= n; i++) {       u = read();       v = read();       a[u] = v;     }     solve(1, n);     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, num, sum = 0;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> num;       sum += num;     }     num = 0;     for (int i = sum + 1; i < sum + 6; i++) {       if (i % (n + 1) != 1) num++;     }     cout << num;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int t, n, m, cnt, xmin, xmax, ymin, ymax, head, tail, g[250 + 2][250 + 2],       f[250 + 2][250 + 2], qx[250 * 250], qy[250 * 250];   bool checkType1() {     if (tail != 4 * (xmax - xmin)) return false;     for (int i = xmin; i <= xmax; ++i) {       if (f[i][ymin] != cnt) return false;       if (f[i][ymax] != cnt) return false;     }     for (int j = ymin; j <= ymax; ++j) {       if (f[xmin][j] != cnt) return false;       if (f[xmax][j] != cnt) return false;     }     return true;   }   bool checkType2() {     if ((xmin + xmax) % 2 || (ymin + ymax) % 2) return false;     int xmid = (xmax + xmin) / 2, ymid = (ymax + ymin) / 2,         rang = (xmax - xmin) / 2;     if (tail != 4 * rang) return false;     for (int x = 0; x <= rang; ++x) {       int y = rang - x;       if (f[xmid - x][ymid - y] != cnt) return false;       if (f[xmid - x][ymid + y] != cnt) return false;       if (f[xmid + x][ymid - y] != cnt) return false;       if (f[xmid + x][ymid + y] != cnt) return false;     }     return true;   }   int main() {     for (scanf( %d , &t); t; --t) {       scanf( %d%d , &n, &m);       memset(g, 0, sizeof(g));       memset(f, 0, sizeof(f));       for (int i = 1; i <= n; ++i) {         char b[250];         scanf( %s , b);         for (int j = 1; j <= m; ++j) g[i][j] = b[j - 1] -  0 ;       }       int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1},           dy[8] = {-1, 0, 1, -1, 1, -1, 0, 1}, ans = cnt = 0;       for (int i = 1; i <= n; ++i)         for (int j = 1; j <= m; ++j)           if (g[i][j] && !f[i][j]) {             f[i][j] = ++cnt;             head = tail = 0;             xmin = xmax = i;             ymin = ymax = j;             qx[tail] = i;             qy[tail] = j;             tail++;             while (head != tail) {               for (int k = 0; k != 8; ++k) {                 int xx = qx[head] + dx[k], yy = qy[head] + dy[k];                 if (g[xx][yy] && !f[xx][yy]) {                   f[xx][yy] = cnt;                   if (xx < xmin) xmin = xx;                   if (xx > xmax) xmax = xx;                   if (yy < ymin) ymin = yy;                   if (yy > ymax) ymax = yy;                   qx[tail] = xx;                   qy[tail] = yy;                   tail++;                 }               }               head++;             }             if (tail <= 1 || xmax - xmin != ymax - ymin) continue;             if (checkType1() || checkType2()) ans++;           }       printf( %d n , ans);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   set<pair<long long, long long> > st;   map<long long, long long> tp;   long long ans[100013];   int main() {     long long n, k;     cin >> n >> k;     vector<pair<long long, long long> > v(n);     for (int i = 0; i < n; i++) {       cin >> v[i].first;       v[i].first = -v[i].first;       tp[v[i].first] = i;     }     for (int i = 0; i < n; i++) {       cin >> v[i].second;       v[i].second = -v[i].second;       st.insert({v[i].second, v[i].first});     }     sort(v.begin(), v.end());     for (int i = 0; i < n; i++) {       long long an = -v[i].second, j = 0;       for (auto it = st.begin(); it != st.end() && j < k; it++, j++) {         if (it->second == v[i].first) {           j--;           continue;         } else           an += (-(it->first));       }       st.erase(st.find({v[i].second, v[i].first}));       ans[tp[v[i].first]] = an;     }     for (int i = 0; i < n; i++) {       cout << ans[i] <<    ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int a[4], b[4];   int get(int *a) {     int ans;     cout <<  ?   << a[0] <<     << a[1] <<     << a[2] <<     << a[3] << endl;     cin >> ans;     return ans;   }   int get1(int a, int b, int c, int d) {     int ans;     cout <<  ?   << a <<     << b <<     << c <<     << d << endl;     cin >> ans;     return ans;   }   void solve(int *a) {     int l = a[0], r = a[2], mid;     while (l < r) {       mid = (l + r) / 2;       a[2] = mid;       if (get(a)) {         r = mid;       } else {         l = mid + 1;       }     }     a[2] = r;     l = a[0], r = a[2];     while (l < r) {       mid = (l + r) / 2;       a[0] = mid + 1;       if (get(a)) {         l = mid + 1;       } else {         r = mid;       }     }     a[0] = l;     l = a[1], r = a[3];     while (l < r) {       mid = (l + r) / 2;       a[3] = mid;       if (get(a)) {         r = mid;       } else {         l = mid + 1;       }     }     a[3] = r;     l = a[1], r = a[3];     while (l < r) {       mid = (l + r) / 2;       a[1] = mid + 1;       if (get(a)) {         l = mid + 1;       } else {         r = mid;       }     }     a[1] = r;   }   int main() {     ios_base::sync_with_stdio(false);     int n;     cin >> n;     b[0] = b[1] = a[0] = a[1] = 1;     a[2] = a[3] = b[2] = b[3] = n;     solve(a);     if (a[2] != n && get1(a[2] + 1, 1, n, n)) {       b[0] = a[2] + 1;     } else if (a[3] != n && get1(1, a[3] + 1, n, n)) {       b[1] = a[3] + 1;     } else if (a[0] != 1 && get1(1, 1, a[0] - 1, n)) {       b[2] = a[0] - 1;     } else if (a[1] != 1 && get1(1, 1, n, a[1] - 1)) {       b[3] = a[1] - 1;     }     solve(b);     cout <<  !  ;     for (int i = 0; i < 4; i++) {       cout << a[i] <<    ;     }     for (int i = 0; i < 4; i++) {       cout << b[i] <<    ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, x, a, ans, r[2001], t[2001][2001];   bool f[2001][2001];   int main() {     scanf( %d , &n);     for (int _ = 1; _ <= n; _++)       while (scanf( %d: , &x)) {         r[_]++;         for (int i = 1; i < x; i++) scanf( %d, , &a), t[_][a] = x;         scanf( %d , &a), t[_][a] = x;         if (getchar() !=  - ) break;       }     for (int i = 1; i <= n; i++)       for (int j = i + 1; j <= n; j++)         if (t[i][j] + t[j][i] == n) f[i][j] = f[j][i] = true, ans++;     if (ans != (n - 1)) return printf( -1 n ), 0;     for (int i = 1; i <= n; i++) {       a = 0;       for (int j = 1; j <= n; j++)         if (f[i][j]) a++;       if (a != r[i]) return printf( -1 n ), 0;     }     printf( %d n , ans);     for (int i = 1; i <= n; i++)       for (int j = i + 1; j <= n; j++)         if (f[i][j]) printf( %d %d n , i, j);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     string s;     cin >> s;     int count = 0;     while (s.size() != 1) {       int sum = 0;       for (int i = 0; i < s.size(); i++) sum += s[i] - 48;       string res =   ;       while (sum) {         int digit = sum % 10;         sum /= 10;         res += (char)(digit + 48);       }       s = res;       count++;     }     cout << count;   }
#include <bits/stdc++.h>   #pragma GCC optimize(3,  Ofast ,  inline )   #pragma GCC target( avx,avx2 )   using namespace std;   template <class t>   inline t read(t &x) {     char c = getchar();     bool f = 0;     x = 0;     while (!isdigit(c)) f |= c ==  - , c = getchar();     while (isdigit(c)) x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();     if (f) x = -x;     return x;   }   template <class t, class... A>   inline void read(t &x, A &...a) {     read(x);     read(a...);   }   template <class t>   inline void write(t x) {     if (x < 0)       putchar( - ), write(-x);     else {       if (x > 9) write(x / 10);       putchar( 0  + x % 10);     }   }   long long A, B, C, S, ansA, ansB, ansC, ansS, pw[505][505], V, n, v[10005];   void dfs2(long long x, long long b, long long a) {     if (1.0 * b * b > V / a) return;     if (x > n) {       B = max(B, b);       return;     }     for (long long i = v[x]; ~i; i--) dfs2(x + 1, b * pw[x][i], a);   }   void dfs1(long long x, long long a) {     if (1.0 * a * a * a > V) return;     if (x > n) {       long long bc = V / a, lim = 4 * a * sqrt(bc) + 2 * bc;       if (lim >= ansS) return;       B = 0;       A = a;       dfs2(1, 1, A);       C = V / A / B;       S = (A * B + B * C + A * C) << 1;       if (S < ansS) {         ansS = S;         ansA = A;         ansB = B;         ansC = C;       }       return;     }     for (long long i = v[x]; ~i; i--) {       v[x] -= i;       dfs1(x + 1, a * pw[x][i]);       v[x] += i;     }   }   void doit() {     read(n);     V = 1;     for (long long i = 1, x, y; i <= n; i++) {       read(x, y);       v[i] = y;       pw[i][0] = 1;       for (long long j = 1; j <= y; j++) pw[i][j] = pw[i][j - 1] * x;       V *= pw[i][y];     }     ansS = 1ll << 62;     dfs1(1, 1);     write(ansS);     putchar(   );     write(ansA);     putchar(   );     write(ansB);     putchar(   );     write(ansC);     puts(  );   }   signed main() {     long long t;     read(t);     while (t--) doit();   }
#include <bits/stdc++.h>   using namespace std;   long long mul(long long a, long long b) {     if (!b) return 1;     long long k = mul(a, b / 2);     if (b & 1) return k * k % 998244353 * a % 998244353;     return k * k % 998244353;   }   int n, k, N;   long long A[200010], B[200010], C[200010], a, b, c, sum;   long long f[200010], g[200010], h[200010];   void FWT(long long *v, int dir) {     long long inv2 = mul(2, 998244353 - 2);     for (int i = 1; i < N; i <<= 1)       for (int j = 0; j < N; j += 2 * i)         for (int k = 0; k < i; k++) {           long long x = v[j + k], y = v[j + k + i];           v[j + k] = (x + y) * (dir == -1 ? inv2 : 1) % 998244353;           v[j + k + i] = (x - y + 998244353) * (dir == -1 ? inv2 : 1) % 998244353;         }   }   int main() {     ios::sync_with_stdio(false);     cin >> n >> k >> a >> b >> c;     N = (1 << k);     for (int i = 0; i < n; i++) {       cin >> A[i] >> B[i] >> C[i];       f[B[i] ^ A[i]]++, g[C[i] ^ A[i]]++, h[B[i] ^ C[i]]++;       sum ^= A[i];     }     FWT(f, 1), FWT(g, 1), FWT(h, 1);     long long ans = 1;     long long X = (a + b + c) % 998244353,               Y = (a + b - c + 998244353) % 998244353,               Z = (a - b + c + 998244353) % 998244353,               W = (a - b - c + 2 * 998244353) % 998244353;     long long inv2 = mul(2, 998244353 - 2);     for (int i = 0; i < N; i++) {       long long x = (n + f[i] + g[i] + h[i]) % 998244353 * inv2 % 998244353 *                     inv2 % 998244353,                 y = ((n + f[i]) % 998244353 * inv2 % 998244353 - x + 998244353) %                     998244353,                 z = ((n + g[i]) % 998244353 * inv2 % 998244353 - x + 998244353) %                     998244353,                 w = ((n + h[i]) % 998244353 * inv2 % 998244353 - x + 998244353) %                     998244353;       f[i] = mul(X, x) * mul(Y, y) % 998244353 * mul(Z, z) % 998244353 *              mul(W, w) % 998244353;     }     FWT(f, -1);     for (int i = 0; i < N; i++) cout << f[sum ^ i] <<    ;     cout << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int t;     cin >> t;     while (t--) {       string n;       int s;       cin >> n >> s;       reverse(n.begin(), n.end());       char temp[] =  0 ;       n.insert(n.size(), temp);       int sum = 0;       for (int i = 0; i < n.size(); i++) sum += n[i] - 48;       char ans[25];       for (int i = 0; i < 25; i++) ans[i] = 0;       ans[0] =  0 ;       int i = 0;       while (sum > s) {         int dig = (n[i] - 48);         int car = 0;         sum -= dig;         n[i] =  0 ;         if (dig > 0) {           ans[i] = 10 - dig + 48;           car = 1;         } else           ans[i] =  0 ;         for (int j = i + 1; car; j++) {           int t = (n[j] - 48 + car);           if (t == 10)             sum -= n[j] - 48;           else             sum++;           n[j] = (t % 10) + 48;           car = t / 10;         }         i++;       }       int size = 0;       while (ans[++size])         ;       reverse(ans, ans + size);       cout << ans <<   n ;     }   }