#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vll>;
using vc = vector<char>;
using vvc = vector<vc>;
using pll = pair<ll, ll>;
ll mod = 1000000007;
ll INF = 1e15;
void yorn(bool a) {
  if (a)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
ll gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
ll lcm(long long a, long long b) { return a / gcd(a, b) * b; }
void fix_cout() { cout << fixed << setprecision(20); }
void chmax(ll &a, ll b) {
  if (a < b) a = b;
}
void chmin(ll &a, ll b) {
  if (a > b) a = b;
}
void solve() {
  string s;
  cin >> s;
  ll x;
  cin >> x;
  bool ok = true;
  ll n = s.size();
  for (int i = 0; i < n; i++) {
    if (s[i] == '1' && i - x < 0 && i + x >= n) {
      cout << -1 << endl;
      return;
    }
  }
  string ans(n, 'o');
  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';
    }
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      if (i - x < 0) {
        if (ans[i + x] == '0') {
          cout << -1 << endl;
          return;
        }
        ans[i + x] = '1';
      } else if (i + x >= n) {
        if (ans[i - x] == '0') {
          cout << -1 << endl;
          return;
        }
        ans[i - x] = '1';
      } else {
        if (ans[i - x] == '0' && ans[i + x] == '0') {
          cout << -1 << endl;
          return;
        }
        if (ans[i - x] == 'o') ans[i - x] = '1';
        if (ans[i + x] == 'o') ans[i + x] = '1';
      }
    }
  }
  for (int i = 0; i < n; i++)
    if (ans[i] == 'o') ans[i] = '0';
  cout << ans << endl;
}
int main() {
  ll t;
  cin >> t;
  while (t--) {
    solve();
  }
}
