#include <bits/stdc++.h>
using namespace std;
int main() {
  int t, x, n;
  string s, ans;
  cin >> t;
  while (t--) {
    cin >> s >> x;
    n = s.length();
    ans = string(n, '1');
    for (int i = 0; i < n; i++) {
      if (s[i] == '0') {
        if (i - x >= 0) ans[i - x] = '0';
        if (i + x < n) ans[i + x] = '0';
      }
    }
    bool isone;
    bool ispossible = true;
    for (int i = 0; i < n; i++) {
      isone = false;
      if (i - x >= 0 && ans[i - x] == '1') isone = true;
      if (i + x < n && ans[i + x] == '1') isone = true;
      if (isone ^ (s[i] - '0')) {
        cout << -1 << endl;
        ispossible = false;
        break;
      }
    }
    if (ispossible) cout << ans << endl;
  }
  return 0;
}
