description: Mr. Chanek has a new game called Dropping Balls. Initially, Mr. Chanek has a grid a of size n × m
  
  Each cell (x,y) contains an integer a_{x,y} denoting the direction of how the ball will move.
  
    * a_{x,y}=1 — the ball will move to the right (the next cell is (x, y + 1)); 
    * a_{x,y}=2 — the ball will move to the bottom (the next cell is (x + 1, y)); 
    * a_{x,y}=3 — the ball will move to the left (the next cell is (x, y - 1)). 
  
  
  
  Every time a ball leaves a cell (x,y), the integer a_{x,y} will change to 2. Mr. Chanek will drop k balls sequentially, each starting from the first row, and on the c_1, c_2, ..., c_k-th (1 ≤ c_i ≤ m) columns.
  
  Determine in which column each ball will end up in (position of the ball after leaving the grid).
  
  Input
  
  The first line contains three integers n, m, and k (1 ≤ n, m ≤ 1000, 1 ≤ k ≤ 10^5) — the size of the grid and the number of balls dropped by Mr. Chanek.
  
  The i-th of the next n lines contains m integers a_{i,1},a_{i,2},…,a_{i,m} (1 ≤ a_{i,j} ≤ 3). It will satisfy a_{i, 1} ≠ 3 and a_{i, m} ≠ 1.
  
  The next line contains k integers c_1, c_2, …, c_k (1 ≤ c_i ≤ m) — the balls' column positions dropped by Mr. Chanek sequentially.
  
  Output
  
  Output k integers — the i-th integer denoting the column where the i-th ball will end.
  
  Examples
  
  Input
  
  
  5 5 3
  1 2 3 3 3
  2 2 2 2 2
  2 2 2 2 2
  2 2 2 2 2
  2 2 2 2 2
  1 2 1
  
  
  Output
  
  
  2 2 1 
  
  
  Input
  
  
  1 2 2
  1 3
  1 2
  
  
  Output
  
  
  1 2 
  
  Note
  
  In the first example, the first ball will drop as follows. Note that the cell (1, 1) will change direction to the bottom direction.
  
  <image>
  
  The second and third balls will drop as follows. 
  
  <image>
  
  All balls will be dropped from the first row and on the c_1, c_2, ..., c_k-th columns respectively. A ball will stop dropping once it leaves the grid.
solution: #include <bits/stdc++.h>
  #pragma comment(linker, "/STACK:360777216")
  using LL = long long;
  using ld = long double;
  const int INF = 2 * 1000 * 1000 * 1000;
  LL MOD = 1e9 + 7;
  LL LINF = (LL)4e18;
  double EPS = 1e-7;
  using namespace std;
  LL gcd(LL a, LL b) { return b ? gcd(b, a % b) : a; }
  LL lcm(LL a, LL b) { return a / gcd(a, b) * b; }
  const LL max_er = 1e2;
  vector<LL> min_div(max_er + 1, 0);
  vector<int> pp(max_er + 1, 1);
  vector<LL> res(max_er + 1, -1);
  vector<LL> primes;
  void eratosfen() {
    pp[0] = pp[1] = 0;
    for (LL i = 2; i <= max_er; ++i) {
      if (!pp[i]) continue;
      primes.push_back(i);
      min_div[i] = i;
      for (LL j = i * i; j <= max_er; j += i) {
        pp[j] = 0;
        if (!min_div[j]) min_div[j] = i;
      }
    }
  }
  vector<pair<int, int>> divs;
  void get_divs(LL n) {
    while (n != 1) {
      LL cur = min_div[n];
      LL cnt = 0;
      while (n % cur == 0) {
        n /= cur;
        cnt++;
      }
      divs.push_back({cur, cnt});
    }
  }
  LL T[1][4 * 200];
  void build(int idx, vector<int>& a, int v, int tl, int tr) {
    if (tl == tr)
      T[idx][v] = a[tl] == 0;
    else {
      int tm = (tl + tr) / 2;
      build(idx, a, v * 2, tl, tm);
      build(idx, a, v * 2 + 1, tm + 1, tr);
      T[idx][v] = T[idx][v * 2] + T[idx][v * 2 + 1];
    }
  }
  void update(int idx, int v, int tl, int tr, int pos, int val) {
    if (tl == tr) {
      T[idx][v] = val;
    } else {
      int tm = (tl + tr) / 2;
      if (pos <= tm)
        update(idx, 2 * v, tl, tm, pos, val);
      else
        update(idx, 2 * v + 1, tm + 1, tr, pos, val);
      T[idx][v] = T[idx][2 * v] + T[idx][2 * v + 1];
    }
  }
  int get(int idx, int v, int tl, int tr, int l, int r) {
    if (l > r) return 0;
    if (tl == l && tr == r) return T[idx][v];
    int tm = (tl + tr) / 2;
    return get(idx, 2 * v, tl, tm, l, min(tm, r)) +
           get(idx, 2 * v + 1, tm + 1, tr, max(tm + 1, l), r);
  }
  LL binpow(LL a, LL n, LL mod) {
    if (n == 0) return 1ll;
    if (n % 2 == 1)
      return (binpow(a, n - 1, mod) % mod * a) % mod;
    else {
      LL b = binpow(a, n / 2, mod) % mod;
      return (b * b) % mod;
    }
  }
  int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout << fixed << setprecision(10);
    srand(time(nullptr));
    int n, m, k;
    cin >> n >> m >> k;
    vector<set<pair<int, int>>> v(m);
    vector<vector<int>> pol(n, vector<int>(m));
    for (int(i) = 0; (i) < n; (i)++) {
      for (int(j) = 0; (j) < m; (j)++) {
        cin >> pol[i][j];
      }
    }
    for (int(i) = 0; (i) < m; (i)++) {
      for (int(j) = 0; (j) < n; (j)++) {
        if (pol[j][i] != 2) v[i].insert({j, pol[j][i]});
      }
    }
    for (int(ha) = 0; (ha) < k; (ha)++) {
      int x;
      cin >> x;
      x--;
      int y = 0;
      while (1) {
        if (v[x].size() == 0 || y > v[x].rbegin()->first) {
          cout << x + 1 << " ";
          break;
        } else {
          for (auto it = v[x].begin(); it != v[x].end(); ++it) {
            if (it->first >= y) {
              y = it->first;
              int cl = it->second;
              v[x].erase(it);
              if (cl == 1)
                x++;
              else
                x--;
              break;
            }
          }
        }
      }
    }
    return 0;
  }
  