#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using ld = long double;
using pll = pair<ll, ll>;
using pii = pair<int, int>;
using vll = vector<ll>;
using vi = vector<int>;
const ll mod = (ll)(1e9) + 7LL;
const ll M = 988244353LL;
void solve() {
  string s;
  cin >> s;
  int x;
  cin >> x;
  int n = s.length();
  vector<int> confirm(n, 0);
  string ans = string(n, '0');
  bool flag = true;
  for (int i = 0; i < n; i++) {
    if (s[i] == '0') {
      if (i >= x) {
        if (confirm[i - x] == 1 && ans[i - x] == '1') {
          flag = false;
          break;
        } else {
          ans[i - x] = '0';
          confirm[i - x] = 1;
        }
      }
      if (i + x < n) {
        ans[i + x] = '0';
        confirm[i + x] = 1;
      }
    } else {
      if (i >= x) {
        if (confirm[i - x] == 1 && ans[i - x] == '0') {
          if (i + x < n) {
            ans[i + x] = '1';
            confirm[i + x] = 1;
          } else {
            flag = false;
            break;
          }
        } else {
          confirm[i - x] = 1;
          ans[i - x] = '1';
        }
      } else if (i + x < n) {
        confirm[i + x] = 1;
        ans[i + x] = '1';
      } else {
        flag = false;
        break;
      }
    }
  }
  if (flag) {
    cout << ans << endl;
  } else {
    cout << -1 << endl;
  }
}
int main() {
  ll tc = 1;
  cin >> tc;
  while (tc--) {
    solve();
  }
  return 0;
}
